检查通知是否处于活动状态

Raf*_*ehl 1 android android-notifications android-pendingintent

我正在建立一个使用切换按钮启动的通知:

    toggleLigar.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(toggleLigar.isChecked()){
                NotificationUtil.cancelaAll(TelaCriaDesp.this);

                CharSequence tickerText = "Despertador ativado!";
                CharSequence title = "Você não vai perder seu ponto!";
                CharSequence message = "O despertador " + despertador.getNomeDesp() + " está ativo!";
                Intent intent = new Intent(TelaCriaDesp.this, TelaCriaDesp.class);

                NotificationUtil.criaNotification(TelaCriaDesp.this, tickerText, title, message,
                                                  despertador.getDesp_id(), intent);
            } else {
                NotificationUtil.cancelaNotification(TelaCriaDesp.this, despertador.getDesp_id());
            }
        }
    });
Run Code Online (Sandbox Code Playgroud)

现在我想根据通知状态将切换按钮分配为ON或OFF,如果它处于活动状态(正在显示),则处于ON状态,处于非活动状态则处于OFF状态...我已经尝试过:

    public static boolean testaNotification(int id, Context context){
    Intent intent = new Intent(context, TelaCriaDesp.class);
    //intent.setAction(Intent.);
    PendingIntent teste = PendingIntent.getActivity(context, id, intent,
                                                PendingIntent.FLAG_NO_CREATE);
    return teste  != null;
}
Run Code Online (Sandbox Code Playgroud)

但它不起作用,它总是返回NULL ...

这是我的通知创建:

   public class NotificationUtil {

@SuppressWarnings("deprecation")
public static void criaNotification(Context context, CharSequence tickerText, CharSequence title,
                                    CharSequence message, int id, Intent intent){

    PendingIntent aoSelecionar = PendingIntent.getActivity(context, 0, intent, 0);

    Notification notification = null;
    int apiLevel = Build.VERSION.SDK_INT;

    if(apiLevel >= 11){
        Builder montaNotification = new Notification.Builder(context)
                                                    .setContentTitle(tickerText)
                                                    .setContentText(message)
                                                    .setSmallIcon(R.drawable.icon)
                                                    .setContentIntent(aoSelecionar);

        //montaNotification.setAutoCancel(true);
        montaNotification.setOngoing(true);

        if(apiLevel >= 17){
            notification = montaNotification.build();
        } else {
            notification = montaNotification.getNotification();
        }
    } else {
        notification = new Notification(R.drawable.icon, tickerText, System.currentTimeMillis());

        notification.setLatestEventInfo(context, title, message, aoSelecionar);
        //notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.flags |= Notification.FLAG_ONGOING_EVENT;
    }

    NotificationManager manager = (NotificationManager) 
                                  context.getSystemService(Activity.NOTIFICATION_SERVICE);

    manager.notify(id, notification);

}
Run Code Online (Sandbox Code Playgroud)

ak9*_*k93 5

从API 23开始,您可以使用NotificationManager方法getActiveNotifications()。这将以数组的形式返回由您的应用启动的活动通知的列表。

因此,要检查特定通知是否处于活动状态,您可以遍历此数组的元素并比较通知ID /标记。

在此处阅读有关此方法的更多信息: NotificationManager.getActiveNotifications()