如何在android中完成服务类的活动?

时间:2014-09-15 05:49:56

标签: android

我正在开发一个应用程序,我想从后台服务器获取数据。我在后台使用service获取数据。现在我想处理网络异常,如果没有网络我想显示一个警告对话框并完成应用程序。在这里,我有一个问题,即完成服务类活动 显示异常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();
            }               
        }
    }
}

2 个答案:

答案 0 :(得分:19)

排队

((Activity) mContext).finish();

mContext来自new InitialRequestData(InitialRequestService.this).execute();

它是InitialRequestService,而不是Activity,所以你得到一个ClassCastExcetption。

您需要将Activity实例传递给Service。但我建议您在BroadcastReceiver

中向此活动发送InitialRequestService个活动
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);
    }
}

希望它有所帮助。

答案 1 :(得分:0)

我认为你必须从服务传递简单的意图,如下所示。

Intent myIntent = new Intent(First.this,Secound.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Bundle myKillerBundle = new Bundle();
myKillerBundle.putInt("kill",1);
myIntent.putExtras(myKillerBundle);
getApplication().startActivity(myIntent);

然后在 Secound.class

onCreate(Bundle bundle){
if(this.getIntent().getExtras().getInt("kill")==1)
    finish();
}

否则请使用 BroadcastReceiver 。请参阅下面的示例。

How to close the activity from the service?

希望它有效。