Reputation: 167
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
Upvotes: 3
Views: 7815
Reputation: 1543
Try this Context context=youractivity.this; Then use context instead of getApplicationContext()
Upvotes: 0
Reputation: 1774
Try this AlertDialog.Builder adb=new AlertDialog.Builder(getParent());
Upvotes: 0
Reputation: 1795
Instead of getApplicationContext()
, just use ActivityName.this
Upvotes: 2
Reputation: 5795
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.
Upvotes: 0
Reputation: 1305
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
}
});
}
Upvotes: 0