我正在试图弄清楚如何使用持久通知图标显示我的服务正在运行。我发现的许多示例只会向通知栏发送dismissable message。我想要一个持久性图标,当您下拉通知栏时,它会显示该服务正在运行,您可以单击该按钮打开该应用程序以关闭该服务。
任何人都可以向我指出如何完成此任务的资源或教程任何APK都可以,但希望使用4.0及更高版本。
感谢。
答案 0 :(得分:36)
除非您更改标记,否则它应与可忽略的消息相同。
Notification.FLAG_ONGOING_EVENT
而不是
Notification.FLAG_AUTO_CANCEL
当点击通知时,您发送的意图就会运行,因此您确保Activity执行您想要的任何任务。
private void showRecordingNotification(){
Notification not = new Notification(R.drawable.icon, "Application started", System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, main.class), Notification.FLAG_ONGOING_EVENT);
not.flags = Notification.FLAG_ONGOING_EVENT;
not.setLatestEventInfo(this, "Application Name", "Application Description", contentIntent);
mNotificationManager.notify(1, not);
}
答案 1 :(得分:20)
我知道这是一个老问题,但它首先出现在谷歌搜索结果页面上,所以我会添加信息来帮助其他人。
诀窍是将.setOngoing添加到NotificationCompat.Builder
打开应用并关闭服务的按钮需要PendingIntent
此示例显示了一个持久通知,其中包含一个退出应用程序的关闭按钮。
MyService
:
private static final int NOTIFICATION = 1;
public static final String CLOSE_ACTION = "close";
@Nullable
private NotificationManager mNotificationManager = null;
private final NotificationCompat.Builder mNotificationBuilder = new NotificationCompat.Builder(this);
private void setupNotifications() { //called in onCreate()
if (mNotificationManager == null) {
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP),
0);
PendingIntent pendingCloseIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP)
.setAction(CLOSE_ACTION),
0);
mNotificationBuilder
.setSmallIcon(R.drawable.ic_notification)
.setCategory(NotificationCompat.CATEGORY_SERVICE)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setContentTitle(getText(R.string.app_name))
.setWhen(System.currentTimeMillis())
.setContentIntent(pendingIntent)
.addAction(android.R.drawable.ic_menu_close_clear_cancel,
getString(R.string.action_exit), pendingCloseIntent)
.setOngoing(true);
}
private void showNotification() {
mNotificationBuilder
.setTicker(getText(R.string.service_connected))
.setContentText(getText(R.string.service_connected));
if (mNotificationManager != null) {
mNotificationManager.notify(NOTIFICATION, mNotificationBuilder.build());
}
}
MainActivity
必须处理近似意图。
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
String action = intent.getAction();
if (action == null) {
return;
}
switch (action) {
case MyService.CLOSE_ACTION:
exit();
break;
}
}
private void exit() {
stopService(new Intent(this, MyService.class));
finish();
}
AnotherActivity
必须完成并向MainActivity
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
String action = intent.getAction();
if (action == null) {
return;
}
switch (action) {
case MyService.CLOSE_ACTION:
exit();
break;
}
}
/**
* Stops started services and exits the application.
*/
private void exit() {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setAction(Stn1110Service.CLOSE_ACTION);
startActivity(intent);
}
任何人都可以向我指出资源或教程
http://developer.android.com/training/notify-user/build-notification.html