Остановить уведомление от BroadcastReceiver

Я пытаюсь остановить службу, когда пользователь проводит пальцем по уведомлению, я могу обнаружить салфетку, но не могу остановить службу. Вот приведенный ниже код. Свайп обнаружен, и я получил всплывающее сообщение в широковещательном приемнике, но служба не останавливается, почему?

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));
    }
}

person Tarun Voora    schedule 20.02.2016    source источник


Ответы (2)


Попробуйте unbind свою службу, если она где-то привязана (например, в методе onStop действия).

person iamkaan    schedule 20.02.2016

Используйте этот код, чтобы остановить службу.

PackageManager pm = getPackageManager();
int flag = (PackageManager.COMPONENT_ENABLED_STATE_DISABLED);
ComponentName cn = new ComponentName(context, yourService.class);
pm.setComponentEnabledSetting(Cn, flag, PackageManager.DONT_KILL_APP);
person razer_gh    schedule 21.02.2016