如何在另一个活动中打开警报对话框(How to open an alert dialog in an another activity)

系统教程 行业动态 更新时间:2024-06-14 16:52:52
如何在另一个活动中打开警报对话框(How to open an alert dialog in an another activity)

如何从非活动的新活动中打开对话框? 我有一个运行倒数计时器的服务,当倒数计时器结束时,我想让它在主要活动中打开一个对话框。 我似乎无法弄清楚这一点。 我通常尝试设置警告对话框不起作用,我试着用extras和bundles发送意图,但仍然没有运气。 我在谷歌搜索帮助,厌倦了这个链接的解决方案。

@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(); }

How do I open a dialog in a new activity from a non-activity? I have a service that runs a countdown timer and when the count down timer finishes i would like for it to open a dialog in the main activity. I cant seem to figure this out. I've tried normally setting up the alert dialog which didn't work and I've tried sending intents with extras and bundles and still no luck. I've searched on Google for help and tired the solution from 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(); }

Main activity code here:

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(); }

最满意答案

在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);

In OnFinish you have not put the bundle in the intent.

Try this:

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);

更多推荐

本文发布于:2023-04-05 12:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/0b3766bb7389323f1f376091c8a04e85.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:警报   对话框   活动中   如何在   dialog

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!