我正在尝试使用“取消”按钮从进度通知中取消上传服务。
但我不能让这个工作。
已创建通知
//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();
通过通知按钮收听意图。
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) {
}
}
服务中没有收到任何内容。 +可能有多种服务。
答案 0 :(得分:2)
将PendingIntent.getBroadcast()
更改为PendingIntent.getService()
。
如果在此更改后服务仍未启动,请尝试使用setContentIntent
而不是addAction
构建通知。
检查您的清单,确认BaseUploadService
已正确声明。