gpt4 book ai didi

android - 停止来自 BroadcastReceiver 的通知

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

我试图在用户滑动通知时停止服务,我可以检测到滑动但无法停止服务。这是下面的代码。检测到滑动,我在广播接收器中收到 toast 消息,但服务没有停止,为什么?

private void errorNotification( String order_id) {

NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.app_favicon_inside);
Intent intent = new Intent(this,SplashScreenAct.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

String message = "There seems to be a connection problem. Please check your network connection and try again";

Intent intent2 = new Intent();
intent2.setAction("com.kitchenvilla.stopnotification");
PendingIntent eraseIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, intent2, 0);

Notification.Builder mBuilder = new Notification.Builder(this)
.setSmallIcon(R.drawable.app_favicon)
.setContentTitle("GingerBuds")
.setLargeIcon(bitmap)
.setSortKey(order_id)
.setAutoCancel(true)
.setContentIntent(contentIntent)
.setDeleteIntent(eraseIntent)
.setContentText(message);

Notification notification ;

if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
notification = mBuilder.build();
} else {
notification = mBuilder.getNotification();
}
// notification.defaults |= Notification.DEFAULT_SOUND;
// notification.defaults |= Notification.DEFAULT_VIBRATE;
mNotificationManager.notify(Integer.parseInt(order_id), notification);
}

BroadcastReceiver 是

public class StopNotification extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("swipe", "notification swiped event");
Toast.makeText(context, "swipe detected", Toast.LENGTH_SHORT).show();
context.stopService(new Intent(context,NotificationService.class));
}
}

最佳答案

尝试取消绑定(bind)您的服务,如果它绑定(bind)在某个地方(例如,在 Activity 的 onStop 方法中)。

关于android - 停止来自 BroadcastReceiver 的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35528474/

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