Android 当我的任务正在运行时,在状态栏中显示图标
我正在创建一个android服务,它将在设备启动过程完成后开始运行。在我的服务中,我创建了一个任务。此任务将根据某些条件随时启动或停止。我的目的是,每当我开始我的任务时,我都希望在状态栏中显示一个图标,以知道我的任务正在像蓝牙一样运行。当它打开时,图标将显示出来。您可以使用自定义标题栏,如 并检查您的服务是启用还是禁用的首选项设置,并设置自定义标题。但在此之前,请阅读:您需要一份工作。代码正在说话:) 为您服务:Android 当我的任务正在运行时,在状态栏中显示图标,android,statusbar,Android,Statusbar,我正在创建一个android服务,它将在设备启动过程完成后开始运行。在我的服务中,我创建了一个任务。此任务将根据某些条件随时启动或停止。我的目的是,每当我开始我的任务时,我都希望在状态栏中显示一个图标,以知道我的任务正在像蓝牙一样运行。当它打开时,图标将显示出来。您可以使用自定义标题栏,如 并检查您的服务是启用还是禁用的首选项设置,并设置自定义标题。但在此之前,请阅读:您需要一份工作。代码正在说话:) 为您服务: private NotificationManager mNM; private
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\u图标
:通知标题R.string.local\u服务\u已启动
:通知最新信息(子标题)R.string.local\u service\u标签
:当用户单击通知时将启动的活动MainActivity.class