我想在我的应用程序之外创建一个alertdialog。
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();
但是,context
不是活动,此类为extends BroadcastReceiver
。
当我按下通知时,发生错误,
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选项卡中弹出应用程序之外的对话框。
答案 0 :(得分:7)
我在我的应用程序中使用相同的功能,我使用一个活动作为弹出消息,如下面的
@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();
}
}
在弹出活动设计中,UI就像对话框一样,并在Android Manifest.xml中添加它
<activity android:name=".PopupActivity"
android:theme="@android:style/Theme.Dialog"
android:label="@string/label"
></activity>
您可以根据自己的规格自定义用户界面。它非常适合我。我希望它有所帮助。