我正在尝试找出如何展示我的服务正在运行的持续通知图标。我发现的许多示例只向通知栏发送可消除消息。我想要一个持久的图标,当你下拉通知栏时,它会显示服务正在运行,并且你可以点击它打开应用程序来关闭服务。
有人能够指导我在如何实现这一点方面的资源或教程吗?任何APK都可以,但希望与4.0及更高版本兼容。
谢谢。
有人能够指导我在如何实现这一点方面的资源或教程吗?任何APK都可以,但希望与4.0及更高版本兼容。
谢谢。
除了更改标志(Flag)之外,它应该与可取消的消息相同。
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);
}
我知道这是一个老问题,但它在谷歌搜索结果页面上排名第一,因此我将添加信息以帮助其他人。
关键是要将 .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
mNotificationManager
是什么? - Nick