获取异常来自服务显示警告对话框时 - 无法添加窗口 - 令牌null不是一个应用程序令牌、对话框、应用程序、异常

2023-09-04 12:09:14 作者:倾一池温柔 散一场暖阳

从服务显示警告对话框时获取异常。

Get Exception when showing Alert Dialog from Service.

以下是code在我的服务等级:我试图表现出AlertDialog

Following is the code in my Service class: I am trying to show an AlertDialog.

但我得到的错误:无法添加窗口 - 令牌null不是一个应用程序

But I get the error: Unable to add window -- token null is not for an application

我还附上了我的日志快照这个问题。

I am also attaching snapshot of my Log to this Question.

 if(!mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)&& !mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){

                Log.d("TrackingService","H: Exception after this");

                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setMessage("Your GPS seems to be disabled, do you want to enable it?")
                .setCancelable(false)
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(final DialogInterface dialog, final int id) {
                        startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
                    }
                })
                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    public void onClick(final DialogInterface dialog,  final int id) {
                        dialog.cancel();
                    }
                });
                AlertDialog alert = builder.create();
                alert.show();
            }

推荐答案

您应该保留一些东西在你的心中,同时创造你的GUI:

You should keep some of the things in your mind while creating your gui:

所有费时的非GUI工作应在后台进行(使用的AsyncTask,服务,主题等)。

All time consuming non gui work should be done in background (using asyncTask, Services, Threads etc).

GUI(如视图和其他图形对象)始终都应创建和更新来自UIthreads。

GUI(like views and other graphical objects) should always be created and updated from UIthreads.

所以,创建和显示对话框是一个UI工作,所以应该在UI线程。要运行任何code在UI线程,你可以使用不同的方法,如:

So create and displaying dialog is a UI work so it should be in UI thread. To run any code in UI thread you can use different methods like:

您可以使用runOnUiThread在UI线程中运行的一块code。 您可以使用的MessageHandler。 您可以使用广播发送者和接收者。

我觉得对于你的情况runOnUiThread最好所以使用

I think for your case runOnUiThread is best So use

runOnUiThread (new Runnable() {
                public void run ()
                {
                    // WRITE YOUR PIECE OF CODE HERE TO UPDATE OR CREATE GUI.
                }
            });

要理解这个概念这可以帮助你:

To understand the concept this can help you:

http://developer.android.com/guide/components/流程 - 和 - threads.html

要创建广播,你可以使用:

To create broadcast you can use:

第1步:创建一个从那里你开始你的服务在您的活动的广播接收机

Step 1: Create a broadcast receiver in your activity from where you are starting your service.

   BroadcastReceiver receiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals("create_dialog")) {
                // create your dialog here.
            }
        }
    };

第2步:创建广播接收器后,注册您的接收器

Step 2: Register your receiver after creating your broadcast receiver.

IntentFilter filter = new IntentFilter("create_dialog");
registerReceiver(receiver, filter);

第三步:发送广播从您的服务以显示对话框

Step 3: Send broadcast from your service to display dialog.

Intent intent = new Intent("create_dialog");
intent.putExtra("dialog_data", data_object to display in dialog);
SendBroadcast(intent);

我希望这可以帮助你。

I hope this can help you.