2012-01-12 44 views
5

我正在创建一个android服务,它将在设备引导过程完成后开始运行。在我的服务中,我正在创建一项任务。根据某些条件,此任务将随时启动或停止。我的意图是每当我开始我的任务时,我想在状态栏中显示一个图标,以知道我的任务正在运行,就像蓝牙图标在打开时显示一样。当我的任务正在运行时在状态栏中显示一个图标

+0

可能将要推出的活动[如何在应用程序运行时在状态栏中显示图标,包括在背景中?]的副本(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

回答

0

您可以像使用 Custom title with image

自定义标题栏,检查偏好设置您的服务是启用或禁用,并设置自定义标题。但在此之前请阅读:http://developer.android.com/guide/topics/ui/notifiers/notifications.html

+0

自定义标题与状态栏图标不同。实际上,在我的应用程序中没有任何活动,它的一项服务在设备启动后自动启动。是否有可能使用自定义标题没有在应用程序中的活动?无论如何感谢您的答案。 – 2012-01-12 13:16:55

+0

是的,它可能在没有任何活动的情况下启动服务......只需将您的服务作为前台启动即可。你会提供一个通知对象,以便开始服务 – waqaslam 2012-01-12 13:25:06

7

您需要一个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:当用户单击该通知
+0

我认为通知不同于状态栏图标。我提到像状态栏上显示的蓝牙图标。我认为你的答案不适合我的要求。无论如何感谢回答我的问题。 – 2012-01-24 09:41:52

+0

你的意思是像用户永远不会与之交互的图标?像3G图标,同步和耳机? – iTurki 2012-01-24 09:44:55

+0

是的,应用程序可以吗? – 2012-01-24 09:45:54

相关问题