我正在尝试使用“取消”按钮从我的进度通知中取消上传服务。
但我无法让它工作。
通知已创建
//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/