如何在非活动的新活动中打开对话框?我有一个运行倒数计时器的服务,当倒计时器结束时,我希望它在主活动中打开一个对话框。我似乎无法弄清楚这一点。我已经尝试过正常设置警报对话框但没有工作,我尝试发送含有额外内容和捆绑包的意图但仍然没有运气。我在Google上搜索了一些帮助,并厌倦了this link的解决方案。
@Override
public void onFinish() {
Log.i(TAG, "Timer finished");
cdt.start();
showNotification();
addpoints();
savepref();
Intent intent = new Intent(BroadcastService.this, MainActivity.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.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
};
cdt.start();
}
此处的主要活动代码:
setContentView(R.layout.activity_main);
startService(new Intent(this, BroadcastService.class));
Log.i(TAG, "Started service");
Bundle b = getIntent().getExtras();
if (b != null && b.containsKey("alert_icon_res_id")) {
int icon = b.getInt("alert_icon_res_id");
String title = b.getString("alert_title");
String message = b.getString("alert_message");
new AlertDialog.Builder(this).setIcon(icon)
.setTitle(title).setMessage(message)
.setPositiveButton("Close", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).create().show();
}
答案 0 :(得分:0)
在OnFinish
中,您尚未将捆绑包放入意图中。
试试这个:
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); // add this line
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);