3

In my android app I am using alarm manager in mainactivity. What I am doing is at a particular time I need to show a diologue box showing whether to login or not.

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();

            }
       }

}

The alarm is receiving at correct time but application crashes by showing the error in logcat as

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
1
  • 3
    you can't do that. you need an activity to display an alert. (as mentioned in the error). Alternatively, you can display an activity that has a dialog theme. Commented Mar 31, 2014 at 18:34

5 Answers 5

2

Instead of getApplicationContext(), just use ActivityName.this

Sign up to request clarification or add additional context in comments.

Comments

0

you cannot handle ui changes from non ui thread. call activity function (showdialog()) from onReceive method of alarm manager.

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

Comments

0

Create a Constructor, where you can get Activity. Like this -

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

Now, use this activity as argument instead of using arg0, which is your context.

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

Because dialog can't be shown using just a context. You need to provide an Activity for that.

Comments

0

Try this AlertDialog.Builder adb=new AlertDialog.Builder(getParent());

Comments

0

Try this Context context=youractivity.this; Then use context instead of getApplicationContext()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.