Android 如何在其他活动中显示警报对话框?

Android 如何在其他活动中显示警报对话框?,android,Android,我想向主活动类getversion方法显示一个警报对话框非活动类,但每当我通过该方法调用AlertDialog时,它就会生成以下文本错误 public void AlertUpgrade(Activity activity) { Log.e("AlertUpgrade", "Communicator."); AlertDialog.Builder builder = new AlertDialog.Builder(activity); builder.setMessag

我想向主活动类getversion方法显示一个警报对话框非活动类,但每当我通过该方法调用
AlertDialog
时,它就会生成以下文本错误

public void AlertUpgrade(Activity activity) {

    Log.e("AlertUpgrade", "Communicator.");
    AlertDialog.Builder builder = new AlertDialog.Builder(activity);
    builder.setMessage("  Click OK to Upgrade Now ? ")
        .setPositiveButton("OK" +
            "", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int id) {

            Log.e("onClick", "AlertUpgrade");
            Intent marketIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=gsip.webgalaxy"));
            marketIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET|Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
            app_context.startActivity(marketIntent);  
        }
    }).setOnKeyListener(new OnKeyListener() {

        @Override
        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
            if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
                return false;
            }

            return true;
        }
    });
    builder.create().show();  
}

public void AlertUpdate(Activity activity) {

    Log.e("AlertUpdate", "Communicator.....");
    AlertDialog.Builder builder = new AlertDialog.Builder(activity);
    builder.setMessage("There is newer version of this application available, click OK to update now?").setPositiveButton("OK" +
        "", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int id) {

            Log.e("onClick", "AlertUpdate");
            Intent marketIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("url"));
            marketIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET|Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
            app_context. startActivity(marketIntent);  
        }
    }).setNegativeButton("Remind Later", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int id) {

            // User cancelled the dialog
        }

    }).setOnKeyListener(new OnKeyListener() {

        @Override
        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
               if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
                   return false;
               }

               return true;
           }
       });
    builder.create().show(); 
}
错误日志

04-03 09:04:44.007: E/AndroidRuntime(2249): java.lang.RuntimeException: Unable to start activity ComponentInfo{gsip.webgalaxy/gsip.webgalaxy.ui.MainActivity}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
04-03 09:04:44.007: E/AndroidRuntime(2249):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at android.os.Handler.dispatchMessage(Handler.java:102)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at android.os.Looper.loop(Looper.java:136)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at java.lang.reflect.Method.invokeNative(Native Method)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at java.lang.reflect.Method.invoke(Method.java:515)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at dalvik.system.NativeStart.main(Native Method)
04-03 09:04:44.007: E/AndroidRuntime(2249): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
04-03 09:04:44.007: E/AndroidRuntime(2249):     at android.view.ViewRootImpl.setView(ViewRootImpl.java:540)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:259)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at android.app.Dialog.show(Dialog.java:286)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at gsip.webgalaxy.ui.Communicator.getversion(Communicator.java:226)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at gsip.webgalaxy.ui.MainActivity.onCreate(MainActivity.java:460)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at android.app.Activity.performCreate(Activity.java:5231)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-03 09:04:44.007: E/AndroidRuntime(2249):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-03 09:04:44.007: E/AndroidRuntime(2249):     ... 11 more
04-03 09:09:44.177: I/Process(2249): Sending signal. PID: 2249 SIG: 9

对话框需要
活动
上下文
。如果您是从非活动类显示它,请将对
活动的引用作为参数传递。

一种可能是在启动另一个
活动时指定一些额外内容,然后在目标
活动中截取这些额外内容,并相应地显示
警报对话框

像这样的

来源
活动

  Intent intent = new Intent(this, TargetActivity.class);
  Bundle bundle = new Bundle();
  bundle.putInt("alert_icon_res_id", android.R.drawable.ic_dialog_info);
  bundle.putString("alert_title", "Some Title");
  bundle.putString("alert_message", "Some message");
  intent.putExtras(bundle);
  startActivity(intent);
@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  Bundle b = getIntent().getExtras();
  if (b != null && b.containsKey("alert_icon_res_id")) {
    int icon = b.getInt("alert_icon_res_id");
    int title = b.getString("alert_title");
    int message = b.getString("alert_message");

    new AlertDialog.Builder(this).setIcon(icon)
      .setTitle(title).setMessage(message)
      .setPositiveButton(R.string.edit_dialog_ok, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
          dialog.dismiss();
        }
      }).create().show());
   }
}
目标
活动

  Intent intent = new Intent(this, TargetActivity.class);
  Bundle bundle = new Bundle();
  bundle.putInt("alert_icon_res_id", android.R.drawable.ic_dialog_info);
  bundle.putString("alert_title", "Some Title");
  bundle.putString("alert_message", "Some message");
  intent.putExtras(bundle);
  startActivity(intent);
@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  Bundle b = getIntent().getExtras();
  if (b != null && b.containsKey("alert_icon_res_id")) {
    int icon = b.getInt("alert_icon_res_id");
    int title = b.getString("alert_title");
    int message = b.getString("alert_message");

    new AlertDialog.Builder(this).setIcon(icon)
      .setTitle(title).setMessage(message)
      .setPositiveButton(R.string.edit_dialog_ok, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
          dialog.dismiss();
        }
      }).create().show());
   }
}

我记得,有一种活动类型旨在为您提供一个看起来像警报的新活动。您可以使用此选项并激发意图来打开您的特殊目的活动。我已经很久没有为自己编写Android代码了,无法记住具体细节,但我知道我已经完成了,而且非常简单。