我创建了两个应用程序。
一个应用程序是消息接收者 ( app1 ),另一个应用程序 ( app2 ) 用于根据消息执行其他任务。
第一个应用程序 (app1) 收到一条消息,创建通知并显示在顶部。当用户单击通知时,它会调用另一个应用程序(app2)来根据消息执行其他任务。
如果应用程序 (app2) 未运行,则应启动它。如果它已经在运行,则应显示该实例并完成要完成的任务。
我正在使用以下代码:
protected void displayNotification() {
Notification notification = new Notification(icon, tickerText, when);
Bundle xtra = new Bundle();
Intent ntent = new Intent(Intent.ACTION_SEND);
ntent.setClassName("com.example.mytestapp",
"com.example.mytestapp.MainActivity");
xtra.putString("id", "8610B0DD");
xtra.putParcelable("message", msg);
ntent.putExtras(xtra);
ntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ntent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
ntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, contentTitle, contentText,
pendingIntent);
final int button_Click = 1;
nm.notify(button_Click, notification);
}
这工作正常,但它创建另一个应用程序(app2)的多个实例。
有没有办法防止创建这个多个副本?