2014-03-31 29 views
2

在我的android应用程序中,我在mainactivity中使用了警报管理器。我正在做的是在特定的时间,我需要显示一个显示是否登录的对话框。

AlarmReceiver2.java

public class AlarmReceiver2 extends BroadcastReceiver { 

    @Override 
    public void onReceive(final Context arg0, Intent arg1) { 

     Toast.makeText(arg0, "Alarm received!", Toast.LENGTH_LONG).show(); 

      DatabaseHandler1 db = new DatabaseHandler1(arg0); 

      int count = db.getRowCount(); 
      if(count == 0){ 
       AlertDialog.Builder adb=new AlertDialog.Builder(arg0); 
       adb.setTitle("TNO"); 
       adb.setMessage("login?"); 
       adb.setNegativeButton("Cancel", null); 

        adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialog, int which) { 

          Intent i = new Intent(); 
         i.setClassName("com.androidhive.pushnotifications", "com.androidhive.pushnotifications.LoginActivity"); 

         i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
         arg0.startActivity(i); 

          }}); 
        adb.show(); 

      } 
     } 

} 

报警是在正确的时间,但应用程序崩溃通过显示在logcat的错误接收为

03-31 14:29:36.899: E/AndroidRuntime(1262): FATAL EXCEPTION: main 
03-31 14:29:36.899: E/AndroidRuntime(1262): java.lang.RuntimeException: Unable to start receiver com.androidhive.pushnotifications.AlarmReceiver2: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 
03-31 14:29:36.899: E/AndroidRuntime(1262):  at android.app.ActivityThread.handleReceiver(ActivityThread.java:2431) 
03-31 14:29:36.899: E/AndroidRuntime(1262):  at android.app.ActivityThread.access$1500(ActivityThread.java:141) 
03-31 14:29:36.899: E/AndroidRuntime(1262):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1332) 
03-31 14:29:36.899: E/AndroidRuntime(1262):  at android.os.Handler.dispatchMessage(Handler.java:99) 
03-31 14:29:36.899: E/AndroidRuntime(1262):  at android.os.Looper.loop(Looper.java:137) 
03-31 14:29:36.899: E/AndroidRuntime(1262):  at android.app.ActivityThread.main(ActivityThread.java:5103) 
03-31 14:29:36.899: E/AndroidRuntime(1262):  at java.lang.reflect.Method.invokeNative(Native Method) 
03-31 14:29:36.899: E/AndroidRuntime(1262):  at java.lang.reflect.Method.invoke(Method.java:525) 
03-31 14:29:36.899: E/AndroidRuntime(1262):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
03-31 14:29:36.899: E/AndroidRuntime(1262):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
03-31 14:29:36.899: E/AndroidRuntime(1262):  at dalvik.system.NativeStart.main(Native Method) 
03-31 14:29:36.899: E/AndroidRuntime(1262): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 
03-31 14:29:36.899: E/AndroidRuntime(1262):  at android.view.ViewRootImpl.setView(ViewRootImpl.java:563) 
03-31 14:29:36.899: E/AndroidRuntime(1262):  at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:269) 
03-31 14:29:36.899: E/AndroidRuntime(1262):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69) 
03-31 14:29:36.899: E/AndroidRuntime(1262):  at android.app.Dialog.show(Dialog.java:281) 
03-31 14:29:36.899: E/AndroidRuntime(1262):  at android.app.AlertDialog$Builder.show(AlertDialog.java:951) 
03-31 14:29:36.899: E/AndroidRuntime(1262):  at com.androidhive.pushnotifications.AlarmReceiver2.onReceive(AlarmReceiver2.java:44) 
03-31 14:29:36.899: E/AndroidRuntime(1262):  at android.app.ActivityThread.handleReceiver(ActivityThread.java:2424) 
03-31 14:29:36.899: E/AndroidRuntime(1262):  ... 10 more 
+3

你不能那样做。您需要一个活动来显示警报。 (如错误中所述)。或者,您可以显示具有对话主题的活动。 – njzk2

回答

0

试试这个AlertDialog.Builder adb=new AlertDialog.Builder(getParent());

0

你不能处理的UI变化从非ui线程。 从警报管理器的onReceive方法调用活动函数(showdialog())。

public void showdialog() 
{ 
    yourActivity.this.runOnUiThread(new Runnable(){ 
    public void run(){ 
     // Create a Alert dialog and show it 
    } 
    }); 
} 
0

创建Constructor,在那里你可以得到活动。这样的 -

Activity activity; 
public AlarmReceiver2 (Activity activity){ 
     this.activity = activity; 
} 

现在,用这个activity作为参数,而不是使用arg0,这是你的context

AlertDialog.Builder adb=new AlertDialog.Builder(arg0);

因为不能仅使用context显示对话框。你需要为此提供一个Activity

2

相反的getApplicationContext(),只是使用ActivityName.this

0

试试这个上下文的背景下= youractivity.this; 然后使用上下文而不是getApplicationContext()

相关问题