我有一个有趣的问题。我在我的android应用程序中实现了一个服务实现,它与服务器同步,如果有新的订单,它会显示一个通知和一个带有SHO/DISMISS按钮的对话框。因为无论当前正在运行的活动如何,我都需要显示对话框,因此当出现新订单时,我已将其作为从服务开始的另一项活动实施。有更多的startActivity调用时,对话框的活动只启动一次
的活动不设置任何看法,只是创建一个AlertDialog。 SHOW按钮启动OrdersListActivity,DISMISS按钮只是关闭对话框。一切正常,但只在第一次。如果我解散对话框,并且另一个命令来了,它会再次显示。但是,如果我点击SHOW按钮,它将显示OrdersList,但是当另一个订单稍后出现时,对话框将不会显示。尽管根据logcat,活动已启动。我已经搞了几个小时了,你能帮我吗?谢谢。
这里是从我的服务类中的方法(进行通知,发送一个广播并启动对话活性):
公共无效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延伸活动{
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月10日至28日:00:38.074:INFO/ActivityManager(63):开始:意向{FLG = 0x10000000的CMP = PL。从PID 1001
更改标志FLAG_ACTIVITY_CLEAR_TOP –
大,真正的工作。我想我必须仔细看看活动标志。再次感谢。 –
如果有效。我将我的评论作为答案提出,请接受它:) –