0

每次启动完成后尝试创建一个弹出窗口以获得一些确认。能够得到 BOOT_COMPLETED 消息,但是在打开 Alertdialog 时它崩溃了,下面是我正在使用的代码行。请建议

public class BootReceiver extends BroadcastReceiver {

    private static final String TAG = "BootReceiver";

    @Override
    public void onReceive(Context context, Intent intent) 
        {             
            String action = intent.getAction();         
            Log.d(TAG, "BroadcastReceiver:" + action);          
            if (action.equals(Intent.ACTION_BOOT_COMPLETED)) 
            {               

                Log.d(TAG, "ACTION_BOOT_COMPLETETED Receved" ); 
                CharSequence text = "BOOT COMPLETED!";
                int duration = Toast.LENGTH_SHORT;
                Toast.makeText(context,text,Toast.LENGTH_SHORT).show();

                AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
                alertDialog.setMessage("Message");
                alertDialog.setTitle("CONSENT");
                alertDialog.setIcon(android.R.drawable.ic_dialog_alert);
                alertDialog.setCancelable(false);
                alertDialog.setPositiveButton("AGREE", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    Log.d(TAG, " AGREE CLICKED"  );

                } }); 
                alertDialog.setNegativeButton("DISAGREE", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Log.d(TAG, " DISAGREE CLICKED"  );

                     } }); 
                alertDialog.show();

            }           
        }    
    }

回溯:

I/ActivityManager(  109): Start proc com.android.mms for broadcast com.android.m
ms/.transaction.MmsSystemEventReceiver: pid=311 uid=10015 gids={3003, 1015}
E/AndroidRuntime(  246): FATAL EXCEPTION: main
E/AndroidRuntime(  246): java.lang.RuntimeException: Unable to start receiver co
m.android.settings.BootReceiver: android.view.WindowManager$BadTokenExceptio
n: Unable to add window -- token null is not for an application
E/AndroidRuntime(  246):        at android.app.ActivityThread.handleReceiver(Act
ivityThread.java:1862)
E/AndroidRuntime(  246):        at android.app.ActivityThread.access$2400(Activi
tyThread.java:124)
E/AndroidRuntime(  246):        at android.app.ActivityThread$H.handleMessage(Ac
tivityThread.java:1035)
E/AndroidRuntime(  246):        at android.os.Handler.dispatchMessage(Handler.ja
va:99)
E/AndroidRuntime(  246):        at android.os.Looper.loop(Looper.java:126)
E/AndroidRuntime(  246):        at android.app.ActivityThread.main(ActivityThrea
d.java:3897)
E/AndroidRuntime(  246):        at java.lang.reflect.Method.invokeNative(Native
Method)
E/AndroidRuntime(  246):        at java.lang.reflect.Method.invoke(Method.java:4
91)
E/AndroidRuntime(  246):        at com.android.internal.os.ZygoteInit$MethodAndA
rgsCaller.run(ZygoteInit.java:841)
E/AndroidRuntime(  246):        at com.android.internal.os.ZygoteInit.main(Zygot
eInit.java:599)
E/AndroidRuntime(  246):        at dalvik.system.NativeStart.main(Native Method)

E/AndroidRuntime(  246): Caused by: android.view.WindowManager$BadTokenException
: Unable to add window -- token null is not for an application
E/AndroidRuntime(  246):        at android.view.ViewRoot.setView(ViewRoot.java:4
34)
E/AndroidRuntime(  246):        at android.view.WindowManagerImpl.addView(Window
ManagerImpl.java:181)
E/AndroidRuntime(  246):        at android.view.WindowManagerImpl.addView(Window
ManagerImpl.java:95)
E/AndroidRuntime(  246):        at android.app.Dialog.show(Dialog.java:269)
E/AndroidRuntime(  246):        at android.app.AlertDialog$Builder.show(AlertDia
log.java:853)
E/AndroidRuntime(  246):        at com.android.settings.BootReceiver.onRecei
ve(XtraBootReceiver.java:53)
E/AndroidRuntime(  246):        at android.app.ActivityThread.handleReceiver(Act
ivityThread.java:1855)
E/AndroidRuntime(  246):        ... 10 more
4

1 回答 1

1

一个对话框需要一个活动上下文。你有服务。您的选择是:

  • 使用吐司。
  • 创建一个活动并给它一个对话框主题。
  • 可能有第三种选择来创建或获取合适的上下文,但我不知道有一个。
于 2011-02-10T02:31:55.277 回答