Android 如何从BroadcastReceiver API 26取消服务+;
我想取消用户操作上的服务(即通知上的取消按钮) 它在API<26上工作,但在26+上不工作。如果可能的话,我正在寻求帮助来解决这个问题 这是我的密码: 活动Android 如何从BroadcastReceiver API 26取消服务+;,android,android-service,android-notifications,Android,Android Service,Android Notifications,我想取消用户操作上的服务(即通知上的取消按钮) 它在API
Intent-serviceentent=newintent(这是DownloadService.class);
serviceIntent.putExtra(“fileTitle”,myFile.name);
ContextCompat.startForegroundService(此为serviceIntent);
我的服务(JobIntentService)
@覆盖
public void onCreate(){
super.onCreate();
createNotificationChannel();
Intent closeIntent=新的Intent(这个,NotificationReceiver.class);
closeIntent.setAction(“取消按钮”)
.setFlags(Intent.FLAG_RECEIVER_front | Intent.FLAG_RECEIVER_REPLACE_PENDING);
PendingEvent actionIntent=PendingEvent.getBroadcast(此,0,closeIntent,PendingEvent.FLAG_CANCEL_当前);
nBuilder=new NotificationCompat.Builder(此,通道ID)
.setSmallIcon(android.R.drawable.stat\u系统下载)
.setTicker(“”)
.setContentTitle(文件标题)
.setContentText(“下载开始”)
.setProgress(100,0,false)
.addAction(android.R.drawable.ic\u菜单\u关闭\u清除\u取消,“取消”,actionIntent);
}
@凌驾
public int-onStartCommand(@Nullable-Intent-Intent-Intent,int-flags,int-startId){
if(isPostorEqualOS(构建版本\代码.O)){
startForeground(通知ID,nBuilder.build());
排队工作(此,意图);
}
返回super.onStartCommand(intent、flags、startId);
}
我的接收器
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if("cancel_button".equals(action)){
Intent stopService = new Intent(context, DownloadService.class);
boolean stop = context.stopService(stopService);
}
}
原来
JobIntentService
不是设计用来取消的
有关更多信息:
我将我的服务改为
IntentService
,它正在工作。原来JobIntentService
不是设计用来取消的
有关更多信息:
我将我的服务改为
IntentService
,它正在工作。你说的“不工作”是什么意思?我的意思是我看到我的接收器中的布尔值被调用并返回true,但是,该服务没有进入“onDestroy方法”中。在按下按钮的那一刻,您如何确切地知道您的服务已创建并正在运行?我猜你的前台通知正在显示?当然,您还没有绑定到服务?我用这个注释来帮助我弄清楚,我在context.stopService(stopService)
之后调用了这个函数,但是很明显,前台通知正在显示,对吗?在调用stopService()
之后它会继续显示?你说的“不工作”是什么意思?我的意思是,我看到我的接收器中的布尔值被调用并返回true,但服务不会进入ondestory
方法你如何确切地知道你的服务是在按下按钮的那一刻创建和运行的?我猜你的前台通知正在显示?当然,您还没有绑定到服务?我用这个注释来帮助我弄清楚,我在context.stopService(stopService)
之后调用了这个函数,但是很明显,前台通知正在显示,对吗?在调用stopService()
之后,它会继续显示吗?