Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.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_Android Notifications_Android Pendingintent - Fatal编程技术网

Android通知行为

Android通知行为,android,android-notifications,android-pendingintent,Android,Android Notifications,Android Pendingintent,我有一个应用程序,其中包括一个内置的消息系统。(所以不是短信、电子邮件等)。我使用数据库(本地和远程)在用户之间保存消息,syncAdapter确保将记录从本地同步到远程,反之亦然 在syncAdapter中,当新消息到达我的平板电脑时,我也会显示一个通知。 我使用以下代码生成它: String text_notificare_titlu = "Message from " + self.getSursanume(); String text_notificare_continut = self

我有一个应用程序,其中包括一个内置的消息系统。(所以不是短信、电子邮件等)。我使用数据库(本地和远程)在用户之间保存消息,syncAdapter确保将记录从本地同步到远程,反之亦然

在syncAdapter中,当新消息到达我的平板电脑时,我也会显示一个通知。 我使用以下代码生成它:

String text_notificare_titlu = "Message from " + self.getSursanume();
String text_notificare_continut = self.getTitlu();
final NotificationManager mgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification note = new Notification(R.drawable.new_50, "New message for my app!", System.currentTimeMillis());
note.defaults |= Notification.DEFAULT_SOUND;
note.defaults |= Notification.DEFAULT_VIBRATE;
note.defaults |= Notification.FLAG_AUTO_CANCEL;
// This pending intent will open after notification click
PendingIntent xx = PendingIntent.getActivity(context, i, new Intent(context, MesajeActivity.class), 0);
note.setLatestEventInfo(context, text_notificare_titlu, text_notificare_continut, xx);
note.number = i;
mgr.notify(NOTIFY_ME_ID, note);
现在,我的问题是:

  • 自动取消不起作用。当我点击通知时,我 希望在启动意图后通知消失,但它不会消失
  • 我希望在通知单击上显示的意图是一个特定的意图(保存收件箱的意图),因此MesajeActivity.class。它确实会出现,但如果我的应用被最小化(隐藏),当我单击通知时,新的收件箱活动将显示在前一个收件箱活动的顶部。这就像有两个不同的活动在彼此之上。 因此,如果:
    • 我的应用程序已启动,且用户处于收件箱活动(MesajeActivity.class)中
    • 一段时间后,显示器关闭
    • 时间一到,syncAdapter就会启动,
      • 查找新邮件
      • 创建通知
    • 用户单击通知
    • 此时将显示收件箱活动
    • 当我单击“上一步”时,收件箱活动将消失,并显示另一个收件箱活动(以前打开的应用程序的收件箱活动)
  • 你明白了吗?还是我应该试着解释得更清楚些

    如何解决这两个问题

    谢谢你

    如果你把

    note.flags|= Notification.FLAG_AUTO_CANCEL;
    
    manager.notify
    调用之前,当您单击通知时,该调用会使通知消失

    如果你把

    note.flags|= Notification.FLAG_AUTO_CANCEL;
    
    manager.notify
    调用之前,当您单击通知时,该调用会使通知消失

    如果你把

    note.flags|= Notification.FLAG_AUTO_CANCEL;
    
    manager.notify
    调用之前,当您单击通知时,该调用会使通知消失

    如果你把

    note.flags|= Notification.FLAG_AUTO_CANCEL;
    

    manager.notify
    调用之前,当您单击通知时,该调用会使通知消失

    我将生成通知的代码替换为:

    Intent intent = new Intent(context, MesajeActivity.class);
            PendingIntent pIntent = PendingIntent.getActivity(context, i, intent, 0); 
            // Build notification
            // Actions are just fake
            Notification noti = new Notification.Builder(context)
                    .setContentTitle("Message from "+ self.getSursanume())
                    .setContentText(self.getTitlu()).setSmallIcon(R.drawable.new_50)
                    .setContentIntent(pIntent)
                    .addAction(R.drawable.new_50, self.getTitlucont(), pIntent)
                    .build();
            NotificationManager notificationManager;
            notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
            // hide the notification after its selected
            noti.flags |= Notification.FLAG_AUTO_CANCEL;
            noti.defaults |= Notification.DEFAULT_VIBRATE;
            noti.sound = Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.htc4);
            noti.number = i;
            notificationManager.notify(0, noti);
    
    现在,它自动取消。看来我没有正确地设置旗子。我这样做不是为了我?!?!?而不是没有 然而,我的第二个问题仍然悬而未决


    如何只显示一条消息活动?

    我将生成通知的代码替换为:

    Intent intent = new Intent(context, MesajeActivity.class);
            PendingIntent pIntent = PendingIntent.getActivity(context, i, intent, 0); 
            // Build notification
            // Actions are just fake
            Notification noti = new Notification.Builder(context)
                    .setContentTitle("Message from "+ self.getSursanume())
                    .setContentText(self.getTitlu()).setSmallIcon(R.drawable.new_50)
                    .setContentIntent(pIntent)
                    .addAction(R.drawable.new_50, self.getTitlucont(), pIntent)
                    .build();
            NotificationManager notificationManager;
            notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
            // hide the notification after its selected
            noti.flags |= Notification.FLAG_AUTO_CANCEL;
            noti.defaults |= Notification.DEFAULT_VIBRATE;
            noti.sound = Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.htc4);
            noti.number = i;
            notificationManager.notify(0, noti);
    
    现在,它自动取消。看来我没有正确地设置旗子。我这样做不是为了我?!?!?而不是没有 然而,我的第二个问题仍然悬而未决


    如何只显示一条消息活动?

    我将生成通知的代码替换为:

    Intent intent = new Intent(context, MesajeActivity.class);
            PendingIntent pIntent = PendingIntent.getActivity(context, i, intent, 0); 
            // Build notification
            // Actions are just fake
            Notification noti = new Notification.Builder(context)
                    .setContentTitle("Message from "+ self.getSursanume())
                    .setContentText(self.getTitlu()).setSmallIcon(R.drawable.new_50)
                    .setContentIntent(pIntent)
                    .addAction(R.drawable.new_50, self.getTitlucont(), pIntent)
                    .build();
            NotificationManager notificationManager;
            notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
            // hide the notification after its selected
            noti.flags |= Notification.FLAG_AUTO_CANCEL;
            noti.defaults |= Notification.DEFAULT_VIBRATE;
            noti.sound = Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.htc4);
            noti.number = i;
            notificationManager.notify(0, noti);
    
    现在,它自动取消。看来我没有正确地设置旗子。我这样做不是为了我?!?!?而不是没有 然而,我的第二个问题仍然悬而未决


    如何只显示一条消息活动?

    我将生成通知的代码替换为:

    Intent intent = new Intent(context, MesajeActivity.class);
            PendingIntent pIntent = PendingIntent.getActivity(context, i, intent, 0); 
            // Build notification
            // Actions are just fake
            Notification noti = new Notification.Builder(context)
                    .setContentTitle("Message from "+ self.getSursanume())
                    .setContentText(self.getTitlu()).setSmallIcon(R.drawable.new_50)
                    .setContentIntent(pIntent)
                    .addAction(R.drawable.new_50, self.getTitlucont(), pIntent)
                    .build();
            NotificationManager notificationManager;
            notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
            // hide the notification after its selected
            noti.flags |= Notification.FLAG_AUTO_CANCEL;
            noti.defaults |= Notification.DEFAULT_VIBRATE;
            noti.sound = Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.htc4);
            noti.number = i;
            notificationManager.notify(0, noti);
    
    现在,它自动取消。看来我没有正确地设置旗子。我这样做不是为了我?!?!?而不是没有 然而,我的第二个问题仍然悬而未决


    怎样才能只显示一条消息?

    我有点不确定您所说的第二部分到底是什么意思。但是,对于挂起的意图,请尝试以下方法,pendingent pIntent=pendingent.getActivity(context,i,intent,pendingent.FLAG_UPDATE_CURRENT); i、 e.用PendingEvent.FLAG_UPDATE_当前替换0

    如果这不能解决问题,请将launchMode行添加到清单文件中的MessageActivity声明中,如下所示。 android:launchMode=“singleTask”


    关于singleTask的更多信息,请参阅。我有点不确定您所说的第二部分到底是什么意思。但是,对于挂起的意图,请尝试以下方法,pendingent pIntent=pendingent.getActivity(context,i,intent,pendingent.FLAG_UPDATE_CURRENT); i、 e.用PendingEvent.FLAG_UPDATE_当前替换0

    如果这不能解决问题,请将launchMode行添加到清单文件中的MessageActivity声明中,如下所示。 android:launchMode=“singleTask”


    关于singleTask的更多信息,请参阅。我有点不确定您所说的第二部分到底是什么意思。但是,对于挂起的意图,请尝试以下方法,pendingent pIntent=pendingent.getActivity(context,i,intent,pendingent.FLAG_UPDATE_CURRENT); i、 e.用PendingEvent.FLAG_UPDATE_当前替换0

    如果这不能解决问题,请将launchMode行添加到清单文件中的MessageActivity声明中,如下所示。 android:launchMode=“singleTask”


    关于singleTask的更多信息,请参阅。我有点不确定您所说的第二部分到底是什么意思。但是,对于挂起的意图,请尝试以下方法,pendingent pIntent=pendingent.getActivity(context,i,intent,pendingent.FLAG_UPDATE_CURRENT); i、 e.用PendingEvent.FLAG_UPDATE_当前替换0

    如果这不能解决问题,请将launchMode行添加到清单文件中的MessageActivity声明中,如下所示。 android:launchMode=“singleTask”


    有关单件任务的更多信息,请参阅。

    我认为你似乎是对的。。。起初我以为你没有看到我的代码,后来我才意识到你在谈论一件不同的事情。我的代码中有note.defaults |=Notification.FLAG_AUTO_CANCEL和您建议的
    note.flags |=Notification.FLAG_AUTO_CANCEL。对我的另一个问题有什么想法吗?你似乎是对的我想。。。起初我以为你没有看到我的代码,后来我才意识到你在谈论一件不同的事情。我的代码中有note.defaults |=Notification.FLAG_AUTO_CANCEL
    和您建议的
    note.flags |=Notification.FLAG_AUTO_CANCEL。你知道我的另一个问题吗?你看起来