关闭应用程序后显示对话框

时间:2014-02-24 09:22:54

标签: android dialog handler alarmmanager

我想在关闭申请后显示一个对话框。 (例如,10秒后。) 我用过警报:

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

    }

我的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);
  }



}

错误:

  

02-24 12:38:53.796:E / AndroidRuntime(14711):致命异常:主要   02-24 12:38:53.796:E / AndroidRuntime(14711):   android.view.WindowManager $ BadTokenException:无法添加窗口 -   token null不适用于应用程序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(原生方法)

没有对话,一切都是正确的。

2 个答案:

答案 0 :(得分:2)

final Dialog dialog = new Dialog(con)

在此行con null尝试传递Activity引用,如

final Dialog dialog = new Dialog(YourActivity.this)

答案 1 :(得分:2)

首先:您正在向Dialog构造函数发送空上下文。这就是你得到这个错误的原因。 第二:您应该显示通知,而不是来自后台服务的对话框。 Dialog会中断用户及其糟糕的用户体验。

第三次:您将此命名为警报服务并使用接收器对其进行扩展,这有点令人困惑。

不建议从服务中显示对话框。如果有更新,则从后台向用户显示通知。

感谢任何反馈。

感谢。