在BroadcastReceiver中使用ProgressDialog的异常 - 令牌null不适用于应用程序

时间:2014-12-15 15:29:53

标签: android broadcastreceiver

UserStream.java -

public class UserStream extends BroadcastReceiver {

@Override
    public void onReceive(Context context, Intent intent) 
    {
         if(action.equals("com.aser.main.ReceiverConnection")){
            Log.i(TAG, "Received intent from UserInteraction service");

            if(intent.getExtras().get("key").equals("startDialog"))
            {
                System.out.println("the wokring HUD started...");
                connectionDialog = new ProgressDialog(context);
                connectionDialog.setMessage("Working ...");
                connectionDialog.setIndeterminate(false);
                connectionDialog.setCancelable(false);
                connectionDialog.show();  //exception here
            }
    }

注意:

我的调用类是一个IntentService类,代码如下 -

Intent intent = new Intent();
intent.setAction("com.aser.main.ReceiverConnection");
intent.putExtra("key","startDialog");
sendBroadcast(intent);

错误日志:

12-15 20:48:17.179: E/ACRA(5237): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
12-15 20:48:17.179: E/ACRA(5237):   at android.view.ViewRootImpl.setView(ViewRootImpl.java:588)
12-15 20:48:17.179: E/ACRA(5237):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:326)
12-15 20:48:17.179: E/ACRA(5237):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
12-15 20:48:17.179: E/ACRA(5237):   at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
12-15 20:48:17.179: E/ACRA(5237):   at android.app.Dialog.show(Dialog.java:277)
12-15 20:48:17.179: E/ACRA(5237):   at com.aser.main.UserStream.onReceive(UserStream.java:73)

1 个答案:

答案 0 :(得分:0)

您无法在广播接收器中显示视图组件。要显示进度对话框,请启动活动并显示在那里。