gpt4 book ai didi

android - 发送 PendingIntent 后不调用 onReceive()

转载 作者:行者123 更新时间:2023-11-30 01:19:30 25 4
gpt4 key购买 nike

我想在同一个服务类中接收 PendingIntent。我在 onReceive 方法中添加了一个日志,但它从未显示在 LogCat 中。

安卓 list :

<service android:name=".UpdateService" android:exported="true">
<intent-filter>
<action android:name="monitoringStop" />
</intent-filter>
</service>`

更新服务:

public class UpdateService extends Service {
public static final String BROADCAST_NAME = "monitoringStop";
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//Never reached
}
};

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//registering
IntentFilter filter = new IntentFilter();
filter.addAction(UpdateService.BROADCAST_NAME);
registerReceiver(receiver, filter);

//creating Intent + PendingIntent
Intent stopIntent = new Intent(this, UpdateService.class);
stopIntent.setAction(BROADCAST_NAME);
PendingIntent pendingIntent =
PendingIntent.getBroadcast(this, 1, stopIntent, PendingIntent.FLAG_ONE_SHOT);

//adding PendingIntent to action
NotificationCompat.Action stopService
= new NotificationCompat.Action(R.drawable.stop, getString(R.string.stop), pendingIntent);

NotificationCompat.Builder notificationBuilder
= new NotificationCompat.Builder(this)//settingText etc.
.addAction(stopService);

startForeground(1,notificationBuilder.build());
return Service.START_STICKY;
}
}

最佳答案

您应该调用 stopIntent.setAction 而不是 intent.setAction。

您的服务中不需要广播接收器。您只需调用 PedingIntent.getService 即可调用 onStartCommand 方法。在此方法中,您可以对 Intent 使用react。

关于android - 发送 PendingIntent 后不调用 onReceive(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37341135/

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