通知栏中的持久服务图标

Nic*_*ick 25 service notifications android

我试图找出如何显示我的服务正在运行持久通知图标.我发现的许多示例只向通知栏发送了一条可忽略的消息.我想要一个持久性图标,当您下拉通知栏时,它会显示该服务正在运行,您可以单击它以打开应用程序以关闭服务.

任何人都可以指向我的资源或教程如何实现这一点任何APK都没关系,但希望使用4.0及更高版本.

谢谢.

Mar*_*kes 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);
}
Run Code Online (Sandbox Code Playgroud)

  • 什么是`mNotificationManager`? (2认同)
  • 嗨,谢谢你标记我的第一个答案,是的再次使用mNotification管理器并使用与mNotificationManager.notify中相同的ID取消(在我的例子中我用1来保持简单)mNotificationManager.cancel(1); (2认同)

Jon*_*Jon 21

我知道这是一个老问题,但它首先出现在谷歌搜索结果页面上,所以我会添加信息来帮助其他人.

持久通知

诀窍是将.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());
    }
}
Run Code Online (Sandbox Code Playgroud)

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();
}
Run Code Online (Sandbox Code Playgroud)

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);
}
Run Code Online (Sandbox Code Playgroud)

任何人都可以指向我的资源或教程

http://developer.android.com/training/notify-user/build-notification.html