gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:28:15 27 4
gpt4 key购买 nike

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

最佳答案

你需要一个 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 : 当用户点击通知时将启动的 Activity

关于android - 当我的任务运行时在状态栏中显示一个图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8835518/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com