2011-02-10 55 views
0

嘗試在每次啓動完成後創建一個彈出窗口以獲得一些確認。能夠獲得BOOT_COMPLETED消息,但在打開Alertdialog時崩潰,下面是我使用的代碼行。請建議啓動POPUP已完成

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 

回答

1

一個對話框,需要一個活動環境。你有一個服務。您的選擇是:

  • 使用敬酒。
  • 創建一個活動並給它一個對話框主題。
  • 可能有第三個選項來創建或獲取合適的上下文,但我不知道其中之一。
+0

它基於第二個選項「創建活動併爲其提供對話框主題」。謝謝.. – jhlee 2011-02-10 07:36:08