android - 从通知按钮取消服务

标签 android service notifications android-service

我正在尝试使用“取消”按钮从我的进度通知中取消上传服务。

但我无法让它工作。

通知已创建

//Intent class changes from first activity then to service when it's uploading.
Intent cancel = new Intent(intentClass, BaseUploadService.class);
PendingIntent cancelUploadIntent = PendingIntent.getBroadcast(intentClass, 0, cancel, PendingIntent.FLAG_CANCEL_CURRENT);

NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder
.setSmallIcon(R.drawable.notificationicon)
.setContentTitle(title)
.setContentText(filename)
.setAutoCancel(totalAmount > uploadedAmount)
.setProgress((int) totalAmount, (int) uploadedAmount, false)
.addAction(R.string.assignment_icon_group, intentClass.getString(R.string.cancel), cancelUploadIntent)
;
return builder.build();

监听通知按钮的 Intent 。

public class BaseUploadService extends IntentService {

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        LogUtils.debug("onStartCommand - BaseUploadService");
        stopSelf();
        return super.onStartCommand(intent, flags, startId);
    }

    public BaseUploadService(String name) {
        super(name);
    }

    @Override
    protected void onHandleIntent(Intent intent) {

    }
}

服务中什么也没有收到。 + 可以有多个服务。

最佳答案

PendingIntent.getBroadcast() 更改为 PendingIntent.getService()

如果更改后服务仍未启动,请尝试使用 setContentIntent 而不是 addAction 构建通知。

检查您的 list 以确认 BaseUploadService 已正确声明。

关于android - 从通知按钮取消服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34088494/

相关文章:

sql - 当我的数据库更新时通知我的 WCF 服务

javascript - 键入时进行 TextInput 验证

javascript - 需要播放/停止按钮 javascript 帮助

android - 如何将 android 项目从 API 18 升级到 21

architecture - ServiceBus 架构的优缺点

java - 将数据从 Java RESTful 服务器推送到 Android 手机以进行通知

android - 从 AppWidgetProvider 启动 WorkManager 任务会导致无休止的 onUpdate 调用

java - 如何将列表发布到 Advanced Rest Client

android - 如何制作服务Android编程

ios - 在交互式通知中使用变量而不是字符串