如何创建alertdialog在应用程序之外?应用程序、alertdialog

2023-09-04 23:41:22 作者:Arrogant 傲慢

我想创建一个alertdialog超出了我的申请。

I want to create an alertdialog outside of my application.

AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle(Config_ConstantVariable.latest);
    builder.setMessage(title);
    builder.setIcon(R.drawable.push_logo);
    builder.setCancelable(false)
            .setPositiveButton(Config_ConstantVariable.alertbtnyes,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            Intent intent = new Intent(context,
                                    Main_ParticularNewsDetail.class);
                            Bundle bundle = new Bundle();
                            intent.putExtra("newsid", payload);
                            intent.putExtras(bundle);
                            context.startActivity(intent);
                        }
                    })
            .setNegativeButton(Config_ConstantVariable.alertbtnno,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });
    AlertDialog alert = builder.create();
    alert.show();

不过,上下文不是一个活动,这个类是扩展的BroadcastReceiver

However, the context is not an activity and this class is extends BroadcastReceiver.

当我推通知,发生错误,

When I push notification, an error occured,

06-18 18:38:08.629: E/AndroidRuntime(2402): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

我看到的WhatsApp,可以蹦出三星Galaxy Tab的应用程序之外的对话框。

I saw WhatsApp that can pop out the dialog outside the application in samsung galaxy tab.

推荐答案

我是用我的应用程序,其中我用一个活动作为一个弹出消息中包含

I am using the same functionality in my app where i used one activity as a pop up message like below

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


    try {
         Bundle bundle = intent.getExtras();
         String message = bundle.getString("alarm_message");

         Intent newIntent = new Intent(context, PopupActivity.class);
         newIntent.putExtra("alarm_message", message);
         newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
         context.startActivity(newIntent);
        } catch (Exception e) { 
         e.printStackTrace();

        }
}

在弹出的活动设计,如对话框中的用户界面和Android中的Manifest.xml添加此

In the Popup Activity design the UI like the dialog box and add this in Android Manifest.xml

 <activity android:name=".PopupActivity"
             android:theme="@android:style/Theme.Dialog"
             android:label="@string/label"
             ></activity>

您可以自定义根据您的specification.Its工作完美的我的UI。我希望它能帮助。

You can customise the UI based on your specification.Its working perfectly for me. I hope it helps.