Android 停止后台服务通知

Android 停止后台服务通知,android,Android,我有一个后台服务,我想显示一个通知,允许用户停止它 在android SDK文档中,它表示活动通常用于启动活动。所以我想知道我是否需要创建一个活动来停止服务,或者当用户选择通知时,我是否可以直接停止服务 那么,该公司打算如何回拨该服务来停止它呢 谢谢,你不能从这样的服务开始一项活动。您可以做的是创建对服务中活动的回调,并让回调启动新活动。但是有一个通知意味着你不必通过服务。单击通知时,可以启动在向通知提供的意图中指定的活动。这真的很简单 请阅读有关通知的参考文档以获取示例 所以我想知道我是否需要

我有一个后台服务,我想显示一个通知,允许用户停止它

在android SDK文档中,它表示活动通常用于启动活动。所以我想知道我是否需要创建一个活动来停止服务,或者当用户选择通知时,我是否可以直接停止服务

那么,该公司打算如何回拨该服务来停止它呢


谢谢,

你不能从这样的服务开始一项活动。您可以做的是创建对服务中活动的回调,并让回调启动新活动。但是有一个通知意味着你不必通过服务。单击通知时,可以启动在向通知提供的意图中指定的活动。这真的很简单

请阅读有关通知的参考文档以获取示例

所以我想知道我是否需要创建一个活动来停止服务,或者当用户选择通知时,我是否可以直接停止服务


您不能通过
通知
直接停止服务。您可以使用
意图启动服务,该意图包含操作字符串或额外内容,或服务在
onStartCommand()
中看到的内容,并触发它调用
stopSelf()
问题已经很老了,但由于代码仍然没有解决方案,我只是将我的代码作为解决问题的示例:

您不能通过通知直接停止服务。你可以 使用包含操作字符串或额外字符串的意图启动服务 或者服务在onStartCommand()中看到并触发它的内容 调用stopSelf()

这是正确的解决方案,让我们开始编写代码(这些代码都在ExampleService类中):

现在意图有了额外的数据(“销毁代码”->666)。请注意,我们已经创建了两个PendingEvent:ClosePendingEvent(停止服务)和TabPendingEvent(启动活动)

现在我们有了代码来检查是否有破坏性代码。最后一步是使用按钮创建通知:

// set attributes for notification //
            final NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "channelID_2");
            Notification notification = builder.setOngoing(true)
                    .setSmallIcon(R.drawable.example)
                    .setContentTitle(getText(R.string.notificationTitle))
                    .setContentText(getText(R.string.notificationText))
                    .setPriority(NotificationCompat.PRIORITY_HIGH)
                    .setCategory(NotificationCompat.CATEGORY_MESSAGE)
                    .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                    .setContentIntent(tabPendingIntent) //this is when notification is clicked which only opens ExampleActivity
                    .addAction(R.drawable.example, getString(R.string.notificationButtonText), closePendingIntent) // here is our closePendingIntent with the destroyCode .addAction is "the onClickListener for the notification button"//
                        .build();
                startForeground(2, notification);
在onCreate中,您将启动您的服务

    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O)
        startForegroundService();
    else
        startForeground(1, new Notification());

    // Toast Message that service has started
    Toast.makeText(this, R.string.serviceStarted, Toast.LENGTH_SHORT).show();

就这样

也许前面的讨论会很有用:您能帮我提供一个关于您所说内容的示例代码吗。我对你上面解释的答案有点困惑。感谢调用
pendingent.getService()
以使
destroyCode
extra到达服务的
onStartCommand()
// set attributes for notification //
            final NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "channelID_2");
            Notification notification = builder.setOngoing(true)
                    .setSmallIcon(R.drawable.example)
                    .setContentTitle(getText(R.string.notificationTitle))
                    .setContentText(getText(R.string.notificationText))
                    .setPriority(NotificationCompat.PRIORITY_HIGH)
                    .setCategory(NotificationCompat.CATEGORY_MESSAGE)
                    .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                    .setContentIntent(tabPendingIntent) //this is when notification is clicked which only opens ExampleActivity
                    .addAction(R.drawable.example, getString(R.string.notificationButtonText), closePendingIntent) // here is our closePendingIntent with the destroyCode .addAction is "the onClickListener for the notification button"//
                        .build();
                startForeground(2, notification);
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O)
        startForegroundService();
    else
        startForeground(1, new Notification());

    // Toast Message that service has started
    Toast.makeText(this, R.string.serviceStarted, Toast.LENGTH_SHORT).show();