【问题标题】:Persistent service icon in notification bar通知栏中的持久服务图标
【发布时间】:2012-04-14 12:53:30
【问题描述】:

我正在尝试弄清楚如何使用持久通知图标显示我的服务正在运行。我发现的许多示例仅将dismissable message 发送到通知栏。我想要一个永久图标,当您拉下通知栏时,它会显示服务正在运行,您可以单击它来打开应用程序以关闭服务。

任何人都可以向我指出有关如何完成此任务的资源或教程,任何 APK 都可以,但希望使用 4.0 及更高版本。

谢谢。

【问题讨论】:

    标签: android service notifications


    【解决方案1】:

    它应该与可关闭的消息相同,但您更改了标志。

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

    【讨论】:

    • mNotificationManager 是什么?
    • 抱歉,我从现有应用程序的 Android 通知服务中获取代码,我只是在我的应用程序开始重新用于其他通知时创建它。 private NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    • 谢谢,在您的回答之上,我也有帮助:stackoverflow.com/questions/4300291/…
    • 你知道销毁服务时如何清除flag吗?
    • 您好,感谢您标记我的第一个答案,是的,再次使用 mNotification 管理器并使用与 mNotificationManager.notify 中相同的 id 取消(在我的示例中,我使用 1 来保持简单) mNotificationManager.cancel( 1);
    【解决方案2】:

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

    持续通知

    诀窍是将.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 之外我做了所有事情,我假设稍后可以为退出按钮完成
    猜你喜欢
    • 1970-01-01
    • 2017-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-31
    • 1970-01-01
    相关资源
    最近更新 更多