2

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.

warvariuc
  • 57,116
  • 41
  • 173
  • 227
user3103823
  • 135
  • 1
  • 3
  • 13
  • You need `Activity` reference to show `Dialog`. instead here You can start `Activity` with theme as `Theme.Dialog`... – Gopal Gopi Feb 24 '14 at 09:26

2 Answers2

2
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)
kalyan pvs
  • 14,486
  • 4
  • 41
  • 59
  • MainActivity.this or OnAlarmReceive.this ? I've made OnAlarmReceive class separate. – user3103823 Feb 24 '14 at 09:33
  • 1
    You cant display dialog from Separate receiver class..If you want take it as inner class or Start one transparent Activity from receiver and show.. – kalyan pvs Feb 24 '14 at 09:35
  • check this for more http://stackoverflow.com/questions/17906037/broadcastreceiver-onreceive-open-dialog – kalyan pvs Feb 24 '14 at 09:36
2

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.

varun bhardwaj
  • 1,522
  • 13
  • 24
  • 1
    This is correct, and even if he did want to show a dialog, he should first re-open an activity (even with an empty content), since the current one can be finished by the time it should have been shown. – android developer Feb 24 '14 at 12:29