Question

I would like to display a dialog after closing application. (For example, after 10 seconds.) I've used a Alarm :

MainActivity :

@Override
protected void onStop() {
    // TODO Auto-generated method stub
    setupAlarm(10);
    super.onStop();
}


private void setupAlarm(int seconds) {
      AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
      Intent intent = new Intent(getBaseContext(), OnAlarmReceive.class);
      PendingIntent pendingIntent = PendingIntent.getBroadcast(
         MainActivity.this, 0, intent,
         PendingIntent.FLAG_UPDATE_CURRENT);

      // Getting current time and add the seconds in it
      Calendar cal = Calendar.getInstance();
      cal.add(Calendar.SECOND, seconds);

      alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);

    }

My broadcastReceiver:

public class OnAlarmReceive extends BroadcastReceiver {
 Context con = null ;


  private final Handler handler = new Handler() {

      @Override
    public void handleMessage(Message msg) {
        // TODO Auto-generated method stub
            final Dialog dialog = new Dialog(con);
            dialog.setContentView(R.layout.custom_dialog_update);
            dialog.setTitle("Title...");

            // set the custom dialog components - text, image and button
            TextView text = (TextView) dialog.findViewById(R.id.text);
            text.setText("Android custom dialog example!");
            ImageView image = (ImageView) dialog.findViewById(R.id.image);
            image.setImageResource(R.drawable.ic_launcher);

            Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
            // if button is clicked, close the custom dialog
            dialogButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });

            dialog.show();            


        super.handleMessage(msg);
    }

  };
  @Override
  public void onReceive(Context context, Intent intent) {

     // Start the MainActivity
//       Intent i = new Intent(context, MainActivity.class);
//       i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//       context.startActivity(i);
    //  Toast.makeText(context, "Please Updete Your Clip2iNi", Toast.LENGTH_LONG).show();

      con = context ;
      handler.sendEmptyMessage(0);
  }



}

Errors:

02-24 12:38:53.796: E/AndroidRuntime(14711): FATAL EXCEPTION: main 02-24 12:38:53.796: E/AndroidRuntime(14711): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 02-24 12:38:53.796: E/AndroidRuntime(14711): at android.view.ViewRootImpl.setView(ViewRootImpl.java:757) 02-24 12:38:53.796: E/AndroidRuntime(14711): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:265) 02-24 12:38:53.796: E/AndroidRuntime(14711): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69) 02-24 12:38:53.796: E/AndroidRuntime(14711): at android.app.Dialog.show(Dialog.java:282) 02-24 12:38:53.796: E/AndroidRuntime(14711): at com.example.ex56.OnAlarmReceive$1.handleMessage(OnAlarmReceive.java:43) 02-24 12:38:53.796: E/AndroidRuntime(14711): at android.os.Handler.dispatchMessage(Handler.java:99) 02-24 12:38:53.796: E/AndroidRuntime(14711): at android.os.Looper.loop(Looper.java:137) 02-24 12:38:53.796: E/AndroidRuntime(14711): at android.app.ActivityThread.main(ActivityThread.java:5328) 02-24 12:38:53.796: E/AndroidRuntime(14711): at java.lang.reflect.Method.invokeNative(Native Method) 02-24 12:38:53.796: E/AndroidRuntime(14711): at java.lang.reflect.Method.invoke(Method.java:511) 02-24 12:38:53.796: E/AndroidRuntime(14711): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 02-24 12:38:53.796: E/AndroidRuntime(14711): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 02-24 12:38:53.796: E/AndroidRuntime(14711): at dalvik.system.NativeStart.main(Native Method)

Without Dialog, everything is correct.

No correct solution

OTHER TIPS

final Dialog dialog = new Dialog(con)

In this line con is null try to pass Activity reference like

final Dialog dialog = new Dialog(YourActivity.this)

First: You are sending a null context in to the Dialog Constructor. That is why you are getting this error. Second: You should show a notification instead of a dialog from background service. Dialog will interrupt user and its a bad user experience.

Third: You named this as a alarm service and extending it with a receiver its a bit confusing.

Anyways showing a dialog from a service is not recommended. Show notification to user from background if there is some update.

Any feedback is appreciated.

Thanks.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top