显示异常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); }}
希望能帮助到你.
总结以上是内存溢出为你收集整理的如何在android中完成服务类的活动?全部内容,希望文章能够帮你解决如何在android中完成服务类的活动?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)