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

发布于 2024-12-26 13:21:51 字数 134 浏览 1 评论 0原文

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

I am creating an android service which will start running after the device boot process completed. In my service i am creating a task. This task will be started or stopped at any time depending on some conditions. My intention is whenever i started my task, i want to display an icon in status bar to know that my task is running like bluetooth icon will be displayed when it is turned ON.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

星軌x 2025-01-02 13:21:51

您需要一个通知。代码在说话:)

在您的服务中:

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 :将要执行的 Activity当用户单击通知时启动

You need a Notification. Code is talking :)

In your Service:

private NotificationManager mNM;
private int NOTIFICATION = 10002; //Any unique number for this notification

To show the 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);
}

and to hide it, you simply do this:

mNM.cancel(NOTIFICATION); //The same unique notification number.

Here are some clarifications:

  • R.drawable.status_icon : Notification icon
  • R.string.local_service_started : Notification title
  • R.string.local_service_label : Notification latest info (sub-title)
  • MainActivity.class : The Activity that will be launched when the user click the notification
只为一人 2025-01-02 13:21:51

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

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

You can use a Custom title bar like
Custom title with image

And Check the preference setting that your service is enable or disable and set custom title. but before doing this please Read : http://developer.android.com/guide/topics/ui/notifiers/notifications.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文