使用通知将数据传递给应用程序
我创建了两个应用程序。
一个应用程序是消息接收器 (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)的多个实例。
有什么办法可以防止创建多个副本吗?
I have created two applications.
One application is message receiver (app1) and another application (app2) is for doing other tasks based on the message.
First application (app1) receives a message, creates the notification and shows up in the top.
When user clicks the notification, it invokes the another application (app2) to do the other tasks based on the message.
If the application (app2) is not running, it should be started. If it is already running, the instance should be displayed and tasks to be done.
I am using following code:
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);
}
This works fine but it creates multiple instances of another application (app2).
Is there any way to prevent creating this multiple copies?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试过为活动的启动模式设置“singleTask”或“singleInstance”?
http://developer.android.com/guide/topics/manifest /activity-element.html#lmode
Have you tried setting "singleTask" or "singleInstance" for the launchMode of the activity?
http://developer.android.com/guide/topics/manifest/activity-element.html#lmode
与此代码完美配合。
Works perfect with this code.