无法删除Android上的通知<;5

无法删除Android上的通知<;5,android,notifications,Android,Notifications,我创建通知。为此,我使用以下服务: public class NFCUploadService extends Service implements MeasureDataPostCallback { private static final int IMAGE_SEND_NFC_SERVICE_ID = 100; private SessionLastMeasureDataPreferences sessionLastMeasureDataPreferences = null;

我创建通知。为此,我使用以下
服务

public class NFCUploadService extends Service implements MeasureDataPostCallback {
    private static final int IMAGE_SEND_NFC_SERVICE_ID = 100;
    private SessionLastMeasureDataPreferences sessionLastMeasureDataPreferences = null;

    @Override
    public void onCreate() {
        super.onCreate();

        this.sessionLastMeasureDataPreferences = new SessionLastMeasureDataPreferences(this);
    }

    @Override
    public void onDestroy() {
        SessionNFCUploadService.getInstance().invalidateInstance();

        super.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if(intent.getStringExtra(Globals.KEY_EMAIL) != null) {
            SessionNFCUploadService.getInstance().setEmail(intent.getStringExtra(Globals.KEY_EMAIL));
            SessionNFCUploadService.getInstance().setPassword(intent.getStringExtra(Globals.KEY_PASSWORD));
            SessionNFCUploadService.getInstance().setYear(intent.getStringExtra(Globals.KEY_YEAR));
            SessionNFCUploadService.getInstance().setMonth(intent.getStringExtra(Globals.KEY_MONTH));
            SessionNFCUploadService.getInstance().setDay(intent.getStringExtra(Globals.KEY_DAY));
            SessionNFCUploadService.getInstance().setHour(intent.getStringExtra(Globals.KEY_HOUR));
            SessionNFCUploadService.getInstance().setMinute(intent.getStringExtra(Globals.KEY_MINUTE));
            SessionNFCUploadService.getInstance().setValue(intent.getStringExtra(Globals.KEY_VALUE));
            SessionNFCUploadService.getInstance().setMealTime(intent.getStringExtra(Globals.KEY_MEALTIME));
            SessionNFCUploadService.getInstance().setMood(intent.getStringExtra(Globals.KEY_MOOD));

            if(intent.getStringExtra(Globals.KEY_FILE_PATH) != null) {
                SessionNFCUploadService.getInstance().setImageFile(new File(intent.getStringExtra(Globals.KEY_FILE_PATH)));
            } else {
                SessionNFCUploadService.getInstance().setImageFile(null);
            }
        } 

        if (intent.getAction().equals(ForegroundServiceTags.STARTFOREGROUND_ACTION.getValue()) || intent.getAction().equals(ForegroundServiceTags.REPEAT_ACTION.getValue())) {
            Intent notificationIntent = new Intent(this, MeasureDataFragment.class);
            notificationIntent.setAction(ForegroundServiceTags.MAIN_ACTION.getValue());
            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

            NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle();
            style.addLine(this.getResources().getString(R.string.text_email) + ": " + SessionNFCUploadService.getInstance().getEmail());
            style.addLine(this.getResources().getString(R.string.text_date) + ": " + Helper.getDateTimePattern(SessionNFCUploadService.getInstance().getDay(), SessionNFCUploadService.getInstance().getMonth(), SessionNFCUploadService.getInstance().getYear(), SessionNFCUploadService.getInstance().getHour(), SessionNFCUploadService.getInstance().getMinute()));
            style.addLine(this.getResources().getString(R.string.text_measure_value) + ": " + SessionNFCUploadService.getInstance().getValue());

            NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
            builder.setContentTitle(this.getResources().getString(R.string.text_measuredata_transfer));
            builder.setTicker(this.getResources().getString(R.string.text_measuredata_transfer_new));
            builder.setWhen(System.currentTimeMillis());
            builder.setSmallIcon(R.drawable.ic_launcher);
            builder.setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_launcher));
            builder.setContentIntent(pendingIntent);
            builder.setOngoing(true);
            builder.setStyle(style);
            Notification notification = builder.build();

            this.startForeground(IMAGE_SEND_NFC_SERVICE_ID, notification);
            this.startTask(SessionNFCUploadService.getInstance().getEmail(), SessionNFCUploadService.getInstance().getPassword(), SessionNFCUploadService.getInstance().getYear(), SessionNFCUploadService.getInstance().getMonth(), SessionNFCUploadService.getInstance().getDay(), SessionNFCUploadService.getInstance().getHour(), SessionNFCUploadService.getInstance().getMinute(), SessionNFCUploadService.getInstance().getValue(), SessionNFCUploadService.getInstance().getMealTime(), SessionNFCUploadService.getInstance().getMood(), SessionNFCUploadService.getInstance().getImageFile());
        } else if (intent.getAction().equals(ForegroundServiceTags.CANCEL_ACTION.getValue())) {
            this.stopForeground(true);
            this.stopSelf();
        } else if (intent.getAction().equals(ForegroundServiceTags.STOPFOREGROUND_ACTION.getValue())) {
            this.stopForeground(true);
            this.stopSelf();
        }

        return START_NOT_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onMeasureDataPostCompleted(StatusMeasureDataPost status, MeasureDataPostPOJO measureDataPostPOJO) {
        Intent notificationIntent = new Intent(this, MeasureDataFragment.class);
        notificationIntent.setAction(ForegroundServiceTags.MAIN_ACTION.getValue());
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

        Intent repeatIntent = new Intent(this, NFCUploadService.class);
        repeatIntent.setAction(ForegroundServiceTags.REPEAT_ACTION.getValue());

        PendingIntent pendingRepeatIntent = PendingIntent.getService(this, 0, repeatIntent, 0);

        Intent cancelIntent = new Intent(this, NFCUploadService.class);
        cancelIntent.setAction(ForegroundServiceTags.CANCEL_ACTION.getValue());

        PendingIntent pendingCancelIntent = PendingIntent.getService(this, 0, cancelIntent, 0);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setContentTitle(this.getResources().getString(R.string.text_measuredata_transfer));
        builder.setTicker(this.getResources().getString(R.string.text_measuredata_transfer_new));
        builder.setWhen(System.currentTimeMillis());
        builder.setSmallIcon(R.drawable.ic_launcher);
        builder.setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_launcher));
        builder.setContentIntent(pendingIntent);

        switch(status) {
        case ERROR_CONNECTION_FAILED:
            builder.setContentText(this.getResources().getString(R.string.text_connection_failed));
            builder.setOngoing(true);
            builder.addAction(R.drawable.ic_action_repeat, this.getResources().getString(R.string.text_repeat), pendingRepeatIntent);
            builder.addAction(R.drawable.ic_action_cancel, this.getResources().getString(R.string.text_cancel), pendingCancelIntent);
            break;
        case ERROR_DB_CONNECTION_FAILED:
            builder.setContentText(this.getResources().getString(R.string.text_db_connection_failed));
            builder.setOngoing(true);
            builder.addAction(R.drawable.ic_action_repeat, this.getResources().getString(R.string.text_repeat), pendingRepeatIntent);
            builder.addAction(R.drawable.ic_action_cancel, this.getResources().getString(R.string.text_cancel), pendingCancelIntent);
            break;
        case ERROR_IO_EXCEPTION:
            builder.setContentText(this.getResources().getString(R.string.text_io_exception));
            builder.setOngoing(true);
            builder.addAction(R.drawable.ic_action_repeat, this.getResources().getString(R.string.text_repeat), pendingRepeatIntent);
            builder.addAction(R.drawable.ic_action_cancel, this.getResources().getString(R.string.text_cancel), pendingCancelIntent);
            break;
        case ERROR_JSON_EXCEPTION:
            builder.setContentText(this.getResources().getString(R.string.text_json_exception));
            builder.setOngoing(true);
            builder.addAction(R.drawable.ic_action_repeat, this.getResources().getString(R.string.text_repeat), pendingRepeatIntent);
            builder.addAction(R.drawable.ic_action_cancel, this.getResources().getString(R.string.text_cancel), pendingCancelIntent);
            break;
        case ERROR_PROTOCOL_EXCEPTION:
            builder.setContentText(this.getResources().getString(R.string.text_protocol_exception));
            builder.setOngoing(true);
            builder.addAction(R.drawable.ic_action_repeat, this.getResources().getString(R.string.text_repeat), pendingRepeatIntent);
            builder.addAction(R.drawable.ic_action_cancel, this.getResources().getString(R.string.text_cancel), pendingCancelIntent);
            break;
        case ERROR_WRONG_PARAMETERS:
            builder.setContentText(this.getResources().getString(R.string.text_wrong_parameters));
            builder.setOngoing(true);
            builder.addAction(R.drawable.ic_action_repeat, this.getResources().getString(R.string.text_repeat), pendingRepeatIntent);
            builder.addAction(R.drawable.ic_action_cancel, this.getResources().getString(R.string.text_cancel), pendingCancelIntent);
            break;
        case ERROR_MEASUREDATA_ALREADY_EXISTS:
            builder.setContentText(this.getResources().getString(R.string.text_measuredata_already_exists));

            this.stopForeground(false);
            break;
        case MEASUREDATA_TRANSFERED:
            builder.setContentText(this.getResources().getString(R.string.text_data_saved));

            String email = measureDataPostPOJO.getUser().getEmail();

            MeasureDataListEntry measureData = this.sessionLastMeasureDataPreferences.getMeasureData(email);
            MeasureDataListEntry measureDataListEntry = new MeasureDataListEntry();

            measureDataListEntry.setValue(measureDataPostPOJO.getUser().getValue());
            measureDataListEntry.setTime(measureDataPostPOJO.getUser().getTime());

            if(measureData.compareTo(measureDataListEntry) > 0) {
                this.sessionLastMeasureDataPreferences.setMeasureData(email, measureDataListEntry);
            }

            this.stopForeground(false);
            break;
        }

        Notification notification = builder.build();

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(IMAGE_SEND_NFC_SERVICE_ID, notification);
    }

    private void startTask(String email, String password, String year, String month, String day, String hour, String minute, String value, String mealTime, String mood, File imageFile) {
        MeasureDataPostTask task = new MeasureDataPostTask(email, password, year, month, day, hour, minute, value, mealTime, mood, imageFile);
        task.setCallback(this);
        task.execute(this);
    }
}
到目前为止,我只测试了android 5.0及更高版本的设备,一切正常。当一个通知弹出时,我可以把它刷走删除它

然而。。。我无法在装有安卓4.4.2的Galaxy Ace设备上删除这些通知。它也不适用于Android 4.4.4的三星活动标签。没有删除通知的按钮,而且“滑动删除”也不起作用。要删除通知,我需要关闭应用程序


有人知道为什么android版本<5.0不能使用它,以及我如何修复它吗?

摆脱
setconsuming(true)
。通过该呼叫,您表示您不希望
通知
能够被清除

不可能是正确的,因为我可以清除android>=5.0上的通知。当我将“正在进行”设置为false时,通知将自动消失,我真的不希望这样。@Mulgard:“这不可能是正确的,因为我可以在android>=5.0上清除通知”--除了设备错误之外,您无法在任何安卓版本上清除
setconsusting()
通知。演示了这一点,因为只有在完成进度后才能注销通知。我刚刚在Nexus 4和Nexus 5(5.1)上进行了测试,
setContinuous()
正如我所描述的那样工作。但这真的很奇怪,因为我还有一个搭载安卓5.0.2的Nexus 4、搭载安卓5.1的galaxy tab s和搭载安卓5.1的三星galaxy s6。在所有设备上,我都可以用我上面发布的代码刷走通知。@Mulgard:试试我链接到的示例。如果您得到与我相同的结果,那么在
通知的配置中还有其他东西会给您意外的Android 5.x结果。