我正在创建一个android服务,它将在设备引导过程完成后开始运行。在我的服务中,我正在创建一项任务。根据某些条件,此任务将随时启动或停止。我的意图是每当我开始我的任务时,我想在状态栏中显示一个图标,以知道我的任务正在运行,就像蓝牙图标在打开时显示一样。当我的任务正在运行时在状态栏中显示一个图标
回答
您可以像使用 Custom title with image
自定义标题栏,检查偏好设置您的服务是启用或禁用,并设置自定义标题。但在此之前请阅读:http://developer.android.com/guide/topics/ui/notifiers/notifications.html
自定义标题与状态栏图标不同。实际上,在我的应用程序中没有任何活动,它的一项服务在设备启动后自动启动。是否有可能使用自定义标题没有在应用程序中的活动?无论如何感谢您的答案。 – 2012-01-12 13:16:55
是的,它可能在没有任何活动的情况下启动服务......只需将您的服务作为前台启动即可。你会提供一个通知对象,以便开始服务 – waqaslam 2012-01-12 13:25:06
您需要一个Notification。代码在谈论:)
在您的服务:
private NotificationManager mNM;
private int NOTIFICATION = 10002; //Any unique number for this notification
要显示通知:
private void showNotification() {
// In this sample, we'll use the same text for the ticker and the expanded notification
CharSequence text = getText(R.string.local_service_started);
// Set the icon, scrolling text and timestamp
Notification notification = new Notification(R.drawable.status_icon, text, System.currentTimeMillis());
// The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), 0);
// Set the info for the views that show in the notification panel.
notification.setLatestEventInfo(this, getText(R.string.local_service_label), text, contentIntent);
// Send the notification.
mNM.notify(NOTIFICATION, notification);
}
,并隐藏它,你只需做到这一点:
mNM.cancel(NOTIFICATION); //The same unique notification number.
这里是一些澄清:
R.drawable.status_icon
:通知图标R.string.local_service_started
:通知标题R.string.local_service_label
:通知最新信息(小标题)MainActivity.class
:当用户单击该通知
我认为通知不同于状态栏图标。我提到像状态栏上显示的蓝牙图标。我认为你的答案不适合我的要求。无论如何感谢回答我的问题。 – 2012-01-24 09:41:52
你的意思是像用户永远不会与之交互的图标?像3G图标,同步和耳机? – iTurki 2012-01-24 09:44:55
是的,应用程序可以吗? – 2012-01-24 09:45:54
- 1. 我怎么能显示一个JavaScript的加载栏来显示正在运行的javascript的状态
- 2. Qt正常状态栏在临时状态后不显示
- 3. Eclipse正在显示处于等待状态的每个任务
- 4. 当我在多任务栏中杀死应用程序并运行程序时,它的状态与我正在关闭应用程序的状态相同
- 5. 如何在应用程序运行时在状态栏中显示图标,包括在背景中?
- 6. 通知图标不显示在棒棒糖的状态栏中
- 7. 任务栏中的UWP App图标无法正常显示
- 8. 如何在Android状态栏通知中显示正确的当前时间?
- 9. 在macvim的状态行中显示`expandtab`的当前状态
- 10. 如何显示在状态栏上一个通知图标多个通知
- 11. 如何在我的iPhone/iPad的状态栏中显示播放图标
- 12. MPMoviePlayerController在播放音频时不会在状态栏中显示播放图标
- 13. 显示一个没有显示在任务栏上的ContextMenuStrip
- 14. 显示任务运行状态的HTML5/CSS3布局
- 15. 当另一个任务正在运行时运行进度条android
- 16. 如何在任务栏中显示子窗口图标
- 17. 状态栏中的图标
- 18. 在状态栏中显示异常
- 19. IndexOutofBoundException在我的地图类正在运行时显示
- 20. 通知不会显示在状态栏小图标
- 21. 在任务栏无图标
- 22. 的ClickOnce安装/升级显示在任务栏没有图标
- 23. Android:阅读正在运行+已暂停状态任务列表
- 24. 在显示状态栏之前获取高度状态栏?
- 25. 在ListView中显示正在运行的进程图标
- 26. 在Emacs的标题/状态栏中显示参数
- 27. 用python显示mac状态栏中的图标
- 28. 当视图状态改变时运行一个函数Flex
- 29. 任务板不显示状态为“正在进行”的工作项
- 30. 在Android状态栏中放置一个图标
可能将要推出的活动[如何在应用程序运行时在状态栏中显示图标,包括在背景中?]的副本(http://stackoverflow.com/questions/3973208/how-to-show-an-icon-in-the- status-bar-when-application-is-running-in-in) – 2016-02-12 19:22:50