我的任务运行时在状态栏中显示图标

Yug*_*abu 5 android statusbar

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

iTu*_*rki 7

你需要一个通知.代码正在谈:)

在您的服务中:

private NotificationManager mNM;
private int NOTIFICATION = 10002; //Any unique number for this notification
Run Code Online (Sandbox Code Playgroud)

要显示通知:

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);
}
Run Code Online (Sandbox Code Playgroud)

并隐藏它,你只需这样做:

mNM.cancel(NOTIFICATION); //The same unique notification number.
Run Code Online (Sandbox Code Playgroud)

以下是一些说明:

  • R.drawable.status_icon :通知图标
  • R.string.local_service_started :通知标题
  • R.string.local_service_label :通知最新信息(副标题)
  • MainActivity.class :用户单击通知时将启动的活动


小智 0

您可以使用自定义标题栏,例如 带有图像的自定义标题

并检查您的服务启用或禁用的首选项设置并设置自定义标题。但在执行此操作之前,请阅读: http ://developer.android.com/guide/topics/ui/notifiers/notifications.html