android - 停止来自 BroadcastReceiver 的通知

标签 android broadcastreceiver

我试图在用户滑动通知时停止服务,我可以检测到滑动但无法停止服务。这是下面的代码。检测到滑动,我在广播接收器中收到 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/

相关文章:

android - 在 BroadcastReceiver 中使用 getWindowManager()

java - 使用 baksmali 进行解码 - 缺少 java 类 android 实现?

android - 是否有任何 Twitter 演示示例可以在 Twitter 中使用 OAuth 在 Twitter 上发布照片?

android - 如何在Sqlite数据库中添加所有电话联系人?

android - BroadcastReceiver 不接收 TIME_SET、DATE_CHANGED 操作

android - 如何隐藏来电的默认屏幕

android - 如何使 TextView 中特定单词的出现可点击

android - 获取正确大小的 android 状态栏并将其应用于 HTML?

java - 如何处理网络状态变化时的在线媒体播放器(网络在 WiFi 或移动数据之间切换)?

java - BroadcastReceiver 意外调用了 onReceive()