Android 如果已显示通知,则不显示通知

Android 如果已显示通知,则不显示通知,android,notifications,android-notifications,Android,Notifications,Android Notifications,在我的应用程序中,我希望在某些情况下显示通知 当通知处于活动状态时,我不想再次创建通知 我的应用程序中有活动识别功能,当检测到我在车内时,它会每秒发出通知 如果至少存在一个活动通知,如何防止新生成通知 以下是我尝试的代码: Intent closeIntent; Intent showIntent; if (isStart){ closeIntent = new Intent(this, SwitchButtonListener1.clas

在我的应用程序中,我希望在某些情况下显示通知

当通知处于活动状态时,我不想再次创建通知

我的应用程序中有活动识别功能,当检测到我在车内时,它会每秒发出通知

如果至少存在一个活动通知,如何防止新生成通知

以下是我尝试的代码:

Intent closeIntent;
        Intent showIntent;
        if (isStart){
            closeIntent = new Intent(this, SwitchButtonListener1.class);
        } else {
            closeIntent = new Intent(this, SwitchButtonListener2.class);
        }

        closeIntent.setAction("No");
        PendingIntent pendingIntentClose = PendingIntent.getBroadcast(this, 0,
                closeIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Action closeAction = new NotificationCompat.Action(R.drawable.btn_close_gray, "No", pendingIntentClose);

        if (isStart){
            showIntent = new Intent(this, SwitchButtonListener1.class);
        } else {
            showIntent = new Intent(this, SwitchButtonListener2.class);
        }

        showIntent.setAction("Yes");
        PendingIntent pendingIntentShow = PendingIntent.getBroadcast(this, 0,
                showIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Action showAction = new NotificationCompat.Action(R.drawable.ic_tick, "Yes", pendingIntentShow);

        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setWhen(System.currentTimeMillis())
                .setAutoCancel(true)
                .setSmallIcon(R.drawable.ic_stat_milebox)
                .setContentTitle(title)
                .setContentText(message)
                .addAction(showAction)
                .addAction(closeAction);
        builder.setSound(alarmSound);

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

        mNotificationManager.notify(100, builder.build());

您可以尝试以下草图:

public class MediaNotificationManager extends BroadcastReceiver {


private final NotificationManager mNotificationManager;

private Context ctx;
private boolean mStarted = false;

public MediaNotificationManager(Context ctx) {
    mCtx = ctx;
    mNotificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
    // Cancel all notifications to handle the case where the Service was killed and
    // restarted by the system.
    mNotificationManager.cancelAll();
}

/**
 * Posts the notification and starts tracking the session to keep it
 * updated. The notification will automatically be removed if the session is
 * destroyed before {@link #stopNotification} is called.
 */
public void startNotification() {
    if (!mStarted) {

        // The notification must be updated after setting started to true
        Notification notification = createNotification();
        if (notification != null) {
            mStarted = true;
        }
    }
}

/**
 * Removes the notification and stops tracking the session. If the session
 * was destroyed this has no effect.
 */
public void stopNotification() {
    if (mStarted) {
        mStarted = false;
        try {
            mNotificationManager.cancel(NOTIFICATION_ID);
        } catch (IllegalArgumentException ex) {
            // ignore if the receiver is not registered.
        }
    }
}

@Override
public void onReceive(Context context, Intent intent) {
    final String action = intent.getAction();
    LogHelper.d(TAG, "Received intent with action " + action);
    switch (action) {
        //do something with this.            
    }
}

private Notification createNotification() {
  //create and return the notification
}
}

要了解更多信息,请阅读以下内容:
我在代码中也使用了此通知:

您可以尝试以下草图:

public class MediaNotificationManager extends BroadcastReceiver {


private final NotificationManager mNotificationManager;

private Context ctx;
private boolean mStarted = false;

public MediaNotificationManager(Context ctx) {
    mCtx = ctx;
    mNotificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
    // Cancel all notifications to handle the case where the Service was killed and
    // restarted by the system.
    mNotificationManager.cancelAll();
}

/**
 * Posts the notification and starts tracking the session to keep it
 * updated. The notification will automatically be removed if the session is
 * destroyed before {@link #stopNotification} is called.
 */
public void startNotification() {
    if (!mStarted) {

        // The notification must be updated after setting started to true
        Notification notification = createNotification();
        if (notification != null) {
            mStarted = true;
        }
    }
}

/**
 * Removes the notification and stops tracking the session. If the session
 * was destroyed this has no effect.
 */
public void stopNotification() {
    if (mStarted) {
        mStarted = false;
        try {
            mNotificationManager.cancel(NOTIFICATION_ID);
        } catch (IllegalArgumentException ex) {
            // ignore if the receiver is not registered.
        }
    }
}

@Override
public void onReceive(Context context, Intent intent) {
    final String action = intent.getAction();
    LogHelper.d(TAG, "Received intent with action " + action);
    switch (action) {
        //do something with this.            
    }
}

private Notification createNotification() {
  //create and return the notification
}
}

要了解更多信息,请阅读以下内容:
我在代码中也使用了此通知:

这是服务需要的吗?。我的意思是,如果您有一个服务,并且该服务以某种方式与此通知类相连接。@andrei此代码应该在该服务中。这是服务以某种方式需要的吗?。我的意思是,如果您有一个服务,并且该服务以某种方式与此通知类连接。@andrei此代码应该在该服务中