我有一个应用程序,我想在应用程序运行时向通知栏显示我的应用程序图标,并且我还希望当用户点击通知栏中显示的应用程序图标 我的应用程序将被打开。这个怎么做?请帮忙!从android通知栏中启动应用程序
9
A
回答
14
要创建一个状态栏通知,这样做在你onCreate方法:
获取对NotificationManager参考:
String ns = Context.NOTIFICATION_SERVICE; NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
实例化的通知:
int icon = R.drawable.notification_icon; CharSequence tickerText = "Hello"; long when = System.currentTimeMillis(); Notification notification = new Notification(icon, tickerText, when);
定义通知的扩展消息和意图:
Context context = getApplicationContext(); CharSequence contentTitle = "My notification"; CharSequence contentText = "Hello World!"; Intent notificationIntent = new Intent(this, MyClass.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
传递通知到NotificationManager:
private static final int HELLO_ID = 1; mNotificationManager.notify(HELLO_ID, notification);
就是这样。您的用户现在已收到通知。
0
您需要发布包含意图启动您的应用程序的挂起意图的通知。 请参阅http://developer.android.com/guide/topics/ui/notifiers/notifications.html以了解如何执行此操作,以及http://javablogs.com/Jump.action?id=628173您可能陷入的陷阱。
3
几点建议:
- ,如果你想在通知栏图标,你必须发送一些通知。
- 通过单击通知图标无法启动应用程序。它可以通过点击通知来启动,如果用户下拉通知栏,则该通知将可用。为此,您需要创建PendingIntent。
7
已接受的答案已弃用。以下是从google的documentation显示对话框的方法。
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable
.logo_listy).setContentTitle("My notification").setContentText("Hello World!");
Intent resultIntent = new Intent(this, ResultActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(ResultActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
+0
工程太棒了。可能有助于某人使其成为“持续”通知:通知noti = mBuilder.build(); noti.flags = Notification.FLAG_ONGOING_EVENT; – Artur 2014-02-20 00:58:44
相关问题
- 1. 从通知中启动应用程序
- 2. Android:应用程序启动通知
- 3. Android,使用parse.com推送通知,自动启动应用程序
- 4. 通过Android推送通知启动应用程序
- 5. iPhone应用程序中的通知栏
- 6. 从通知栏安装Android应用程序
- 7. 从Android应用程序启动未知活动
- 8. Air iOS/Android推送通知记得应用程序启动
- 9. 在应用程序启动时触发Android通知
- 10. 单击Xtify通知以启动android应用程序| Appcelerator
- 11. Android通知重新启动应用程序,但想要恢复
- 12. Android:从通知栏中删除通知
- 13. 从android通知栏中删除通知
- 14. 从状态栏通知启动webview类
- 15. Android:发送通知到PHP服务器通知栏到android应用程序
- 16. 在Android应用程序中删除通知栏
- 17. 从远程通知启动
- 18. 从推送通知的警报视图启动应用程序
- 19. 从数据通知启动应用程序
- 20. 从网站启动Android应用程序
- 21. 从网络启动Android应用程序
- 22. 隐藏Android应用程序从启动
- 23. Android:从应用程序内启动Firefox
- 24. 如何在警报通知中自动启动应用程序?
- 25. 从其他应用程序中删除通知栏中的通知
- 26. Android应用程序启动
- 27. 启动Android应用程序
- 28. 单击通知时从头开始重新启动Android应用程序
- 29. GCM用于从Salesforce发送Android移动应用程序通知
- 30. 如何从Java Web应用程序启动Android应用程序?
现在已弃用new Notification()和setLatestEventInfo。查看我的答案或转到谷歌的文档,http://developer.android.com/guide/topics/ui/notifiers/notifications.html,更新的方式来显示通知。 – GLee 2013-12-04 23:05:09