Как создать alertdialog за пределами приложения?
Я хочу создать 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, который может вывести диалог вне приложения на вкладке галактики Samsung.
Ответы
Ответ 1
Я использую ту же функциональность в своем приложении, где я использовал одно действие как всплывающее сообщение, как показано ниже
@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();
}
}
В проекте Popup Activity пользовательский интерфейс, как диалоговое окно, и добавьте его в Android Manifest.xml
<activity android:name=".PopupActivity"
android:theme="@android:style/Theme.Dialog"
android:label="@string/label"
></activity>
Вы можете настроить пользовательский интерфейс на основе ваших спецификаций. Он отлично работает для меня. Надеюсь, это поможет.