本文介绍了如何完成Android中服务类的活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,因为我想在后台从服务器获取数据.我正在使用service在后台获取数据.现在,我想处理网络异常,如果没有网络,我想显示一个警报对话框并完成应用程序.在这里,我有一个问题,即将结束服务类的活动显示异常ClassCastExcetption ..这里引发的异常((((Activity)mContext).finish())我尝试了很多,但是我无法做到这一点,请任何人解决我的问题

I am developing one application in that i want to get data from server in background.I am getting data in background using service.Now i want to handle network exception,if no network i want to show one alert dialog and finish application. Here i have problem that is finishing activity in service classshows exception ClassCastExcetption..Exception raised here(((Activity) mContext).finish())i tried so much but i unable get this please any one solve my problem

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 来自 new InitialRequestData(InitialRequestService.this).execute();

它是 InitialRequestService ,而不是 Activity ,因此您获得了ClassCastExcetption.

it is the InitialRequestService, not Activity,so u get a ClassCastExcetption.

您需要将Activity实例传递给Service.但是我希望在您的 InitialRequestService 中将这样的 BroadcastReceiver 发送给Activity:

You need to pass the Activity instance to Service. But I perfer to send a BroadcastReceiver to Activity like this in your 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);
    }
}

希望有帮助.

这篇关于如何完成Android中服务类的活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-29 17:48
查看更多