我正在开发一个应用程序,我想从后台服务器获取数据.我在后台使用service获取数据.现在我想处理网络异常,如果没有网络我想显示一个警告对话框并完成应用程序.在这里,我有一个问题,即完成服务类活动
显示异常ClassCastExcetption..Exception在这里提出(((Activity)mContext).finish())我尝试了很多,但我无法得到这个请任何人解决我的问题
显示异常ClassCastExcetption..Exception在这里提出(((Activity)mContext).finish())我尝试了很多,但我无法得到这个请任何人解决我的问题
public class InitialRequestService extends Service{ @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); new InitialRequestData(InitialRequestService.this).execute(); } public class InitialRequestData extends AsyncTask<String,Void,Void> { public InitialRequestData(Context context) { this.mContext = context; } @Override protected void onPreExecute() { // TODO Auto-generated method stub super.onPreExecute(); isNetworkAvailable = CheckNetWork.isConnectedToInternet(mContext); } @Override protected Void doInBackground(String... params) { // } @Override protected void onPostExecute(FeatureResult result) { // TODO Auto-generated method stub super.onPostExecute(result); if(result==null || isNetworkAvailable==false){ /*String message="Unable to connect to the server,please try again"; FieldsValdations.AlertErrorMessage(mContext,message,"Network failure");*/ final AlertDialog alert = new AlertDialog.Builder(mContext).create(); alert.setTitle("Network failure"); alert.setMessage("Unable to connect to the server,please try again"); alert.setButton("OK",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int which) { alert.dismiss(); ((Activity) mContext).finish(); } }); alert.setCancelable(false); alert.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); alert.show(); } } } }
解决方法
排队
((Activity)mContext).finish();
mContext来自新的InitialRequestData(InitialRequestService.this).execute();
它是InitialRequestService,而不是Activity,所以你得到一个ClassCastExcetption.
您需要将Activity实例传递给Service.但我更喜欢在您的InitialRequestService中将BroadcastReceiver发送到这样的Activity:
alert.setButton("OK",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int which) { alert.dismiss(); // modify here LocalBroadcastManager localBroadcastManager = LocalBroadcastManager .getInstance(InitialRequestService.this); localBroadcastManager.sendBroadcast(new Intent( "com.durga.action.close")); } });
在要关闭的活动中:
public class YourActivity extends Activity{ LocalBroadcastManager mLocalBroadcastManager; BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context,Intent intent) { if(intent.getAction().equals("com.durga.action.close")){ finish(); } } }; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mLocalBroadcastManager = LocalBroadcastManager.getInstance(this); IntentFilter mIntentFilter = new IntentFilter(); mIntentFilter.addAction("com.durga.action.close"); mLocalBroadcastManager.registerReceiver(mBroadcastReceiver,mIntentFilter); } protected void onDestroy() { super.onDestroy(); mLocalBroadcastManager.unregisterReceiver(mBroadcastReceiver); } }
希望能帮助到你.