如何从非活动的新活动中打开对话框? 我有一个运行倒数计时器的服务,当倒数计时器结束时,我想让它在主要活动中打开一个对话框。 我似乎无法弄清楚这一点。 我通常尝试设置警告对话框不起作用,我试着用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);更多推荐
发布评论