在通知栏中持久显示服务图标

27
我正在尝试找出如何展示我的服务正在运行的持续通知图标。我发现的许多示例只向通知栏发送可消除消息。我想要一个持久的图标,当你下拉通知栏时,它会显示服务正在运行,并且你可以点击它打开应用程序来关闭服务。
有人能够指导我在如何实现这一点方面的资源或教程吗?任何APK都可以,但希望与4.0及更高版本兼容。
谢谢。
2个回答

37

除了更改标志(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);
}

2
mNotificationManager 是什么? - Nick
1
抱歉,我从现有的应用程序中获取了代码,它是Android通知服务,当我的应用程序开始被重复使用以进行其他通知时,我只是创建了它。 private NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); - Martin Sykes
谢谢,我也觉得你提供的这个链接很有用:https://dev59.com/cOo6XIcBkEYKwwoYTS1D - Nick
你知道如何在销毁服务时清除标志吗? - Nick
2
谢谢您审核我的第一个答案,是的,请再次使用mNotification Manager,并使用与mNotificationManager.notify中相同的id进行取消(在我的示例中,我使用1来保持简单)。mNotificationManager.cancel(1); - Martin Sykes

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());
    }
}

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


我没有从这段代码中看到任何通知,除了“AnotherActivity”之外,我假设可以稍后为退出按钮完成。 - Petro

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接