Android 通知打开后,停止暂停服务
我希望服务在单击通知后执行stopForeground和stopSelf,然后运行PendingEvent 我试过使用一个BroadcastReceiver,在调试过程中我检查过,它从未被调用过。我也将其添加到清单中Android 通知打开后,停止暂停服务,android,broadcastreceiver,android-service,Android,Broadcastreceiver,Android Service,我希望服务在单击通知后执行stopForeground和stopSelf,然后运行PendingEvent 我试过使用一个BroadcastReceiver,在调试过程中我检查过,它从未被调用过。我也将其添加到清单中 Intent intentHide = new Intent(this, StopServiceReceiver.class); PendingIntent hide = PendingIntent.getBroadcast(this, (int) System.currentTi
Intent intentHide = new Intent(this, StopServiceReceiver.class);
PendingIntent hide = PendingIntent.getBroadcast(this, (int) System.currentTimeMillis(), intentHide, PendingIntent.FLAG_CANCEL_CURRENT);
将其添加到生成器中
builder.setContentIntent(hide);
而广播Rec是分开进行的-
public class StopServiceReceiver extends BroadcastReceiver {
public static final int REQUEST_CODE = 333;
@Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, TimerService.class);
context.stopService(service);
}
}
舱单-
<receiver
android:name=".StopServiceReceiver"
android:enabled="true"
android:process=":remote" />
这是行不通的。通知和服务都处于活动状态
问题-我应该使用addContent而不是setContentIntent吗?如果是,那么参数应该是什么?
我有什么毛病吗?这样的实施可能会有什么问题?多谢各位 我在通知中遇到了同样的问题 这段代码工作得很好
void creatnotifiaction()
{
public static final String STOP = "com.example.android";
public static final int REQUEST_CODE = 333;
filter = new IntentFilter();
filter.addAction(STOP);
Intent intentHide = new Intent(STOP);
PendingIntent hide = PendingIntent.getBroadcast(this,REQUEST_CODE,intentHide,PendingIntent.FLAG_CANCEL_CURRENT);
registerReceiver(broadcastReceiver, filter);
}
不需要在同一类中单独使用广播接收器
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@SuppressLint("ResourceAsColor")
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
Log.d("notification", "Received intent with action " + action);
switch (action) {
case STOP:
//your code to stop notifications or service.
break;
}
});
如果对你有用,请告诉我。
谢谢…祝你编码愉快