gpt4 book ai didi

android - 使用 NotificationCompat .addAction 按钮停止前台服务

转载 作者:行者123 更新时间:2023-12-05 00:09:55 25 4
gpt4 key购买 nike

我将如何使用它在类中创建的通知来停止前台服务?

Intent stopnotificationIntent = new Intent(this, HelloIntentService.class);
//Not sure which is the current action to set
stopnotificationIntent.setAction("ACTION.STOPFOREGROUND_ACTION");
PendingIntent pIntent = PendingIntent.getService(this, 0, stopnotificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId)            
.addAction(0, "Cancel", pIntent);

startForeground(1111, notificationBuilder.build());

enter image description here

该按钮是从 .addAction 创建的我想停止当前的前台服务

最佳答案

String ACTION_STOP_SERVICE= "STOP";

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

if (ACTION_STOP_SERVICE.equals(intent.getAction())) {
Log.d(Statics.LOG_TAG,"called to cancel service");
stopSelf();
}

Intent stopSelf = new Intent(this, HelloIntentService.class);
stopSelf.setAction(ACTION_STOP_SERVICE);

PendingIntent pStopSelf = PendingIntent
.getService(this, 0, stopSelf
,PendingIntent.FLAG_CANCEL_CURRENT); // That you should change this part in your code

notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentText("Bla Bla Bla")
.setSmallIcon(R.drawable.ic_notification)
.setContentIntent(pendingNotificationIntent)
.addAction(R.drawable.xxxx,"Close", pStopSelf)
.setSound(null)
.build();

startForeground(REQUEST_CODE, notification);
}

关于android - 使用 NotificationCompat .addAction 按钮停止前台服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59885221/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com