【发布时间】:2011-10-28 09:30:27
【问题描述】:
我有一个有趣的问题。我的 android 应用程序中有一个服务实现,它与服务器同步,如果有新订单,它会显示一个通知和一个带有 SHO/DISMISS 按钮的对话框。因为无论当前正在运行的活动如何,我都需要显示对话框,所以我已将其实现为另一个活动,即在出现新订单时从服务启动此活动。
Activity 没有设置任何视图,只是创建了一个 AlertDialog。 SHOW 按钮启动 OrdersListActivity,而 DISMISS 按钮只是关闭对话框。一切正常,但仅限于第一次。如果我关闭对话框,并且另一个订单来了,它会再次显示。但是,如果我单击 SHOW 按钮,它会显示 OrdersList,但稍后有另一个订单时,不会显示对话框。虽然,根据 logcat,活动已启动。我已经搞砸了几个小时了,你能帮帮我吗?谢谢。
这是我的 Service 类中的方法(发出通知、发送广播并启动 Dialog 活动):
public void notifyNewOrder() {
Context context = getApplicationContext();
Intent notificationIntent = new Intent(this, OrdersService.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
newOrderNotification.setLatestEventInfo(this, getString(R.string.notification_text), getString(R.string.notification_text), contentIntent);
notificationManager.notify(NOTIFICATION_ID, newOrderNotification);
Intent intent = new Intent(NEW_ORDER_RECEIVED);
sendBroadcast(intent);
Intent dialog = new Intent(context, NotificationDialog.class);
dialog.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(dialog);
}
NotificationDialog活动类的代码如下:
公共类 NotificationDialog 扩展 Activity {
private static final int DIALOG_NEW_ORDER = 0;
private static final String LOG_TAG = "NotificationDialog";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(LOG_TAG, "onCreate - creating activity...");
}
@Override
public void onResume() {
super.onResume();
Log.d(LOG_TAG, "onResume - starting the dialog...");
showDialog(DIALOG_NEW_ORDER);
}
@Override
protected Dialog onCreateDialog(int id) {
Dialog dialog;
switch(id) {
case DIALOG_NEW_ORDER:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.notification_dialog_new_order)
.setCancelable(false)
.setPositiveButton(R.string.notification_dialog_show, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
/*
* Cancel the notification
*/
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(OrdersService.NOTIFICATION_ID);
/*
* Go to the list
*/
Intent listIntent = new Intent(getApplicationContext(), OrdersListActivity.class);
startActivity(listIntent);
dialog.dismiss();
finish();
}
})
.setNegativeButton(R.string.notification_dialog_dismiss, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
/*
* Cancel the notification
*/
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(OrdersService.NOTIFICATION_ID);
dialog.dismiss();
finish();
}
});
dialog = builder.create();
break;
default:
dialog = null;
}
return dialog;
}
}
我在 NotificationDialog 活动的 onCreate 和 onResume 方法中添加了一些日志,发现如下:
- 第一次启动 NotificationDialog 活动时,会打印出那些调试消息
- 第二次,以及其他所有时间,它们都不会出现,尽管 logcat 说:10-28 10:00:38.074: INFO/ActivityManager(63): Starting: Intent { flg=0x10000000 cmp=pl .mymeal.orders/.services.NotificationDialog } 来自 pid 1001
【问题讨论】:
-
将标志更改为 FLAG_ACTIVITY_CLEAR_TOP
-
太好了,真的很管用。我想我必须仔细看看活动标志。再次感谢。
-
如果可行的话。我将我的评论作为答案,请接受:)
标签: android service android-activity dialog android-intent