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

前端之家收集整理的这篇文章主要介绍了如何在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();
            }               
        }
    }
}

解决方法

排队

((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相关文章