Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 来自广播接收器的停止通知_Android_Broadcastreceiver - Fatal编程技术网

Android 来自广播接收器的停止通知

Android 来自广播接收器的停止通知,android,broadcastreceiver,Android,Broadcastreceiver,当用户刷卡通知时,我试图停止服务,我可以检测到刷卡,但无法停止服务。下面是代码。检测到刷卡,我在广播接收器中收到toast消息,但服务并没有停止,为什么 private void errorNotification( String order_id) { NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

当用户刷卡通知时,我试图停止服务,我可以检测到刷卡,但无法停止服务。下面是代码。检测到刷卡,我在广播接收器中收到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);
}
广播接收机是

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

如果您的服务绑定到某个地方(例如,在活动的
顶部
方法中),请尝试
解除绑定。

使用此代码停止服务

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