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

Android 如何使用通过通知传递的意图更新活动

Android 如何使用通过通知传递的意图更新活动,android,android-notifications,Android,Android Notifications,我已经创建了一个自定义通知,但是当我使用“主页”按钮暂停活动并且通知到达时,我按下通知,它将创建一个新的活动,并且不会继续预览活动,当我按下“上一步”按钮时,它将转到同一窗口中的预览活动。我尝试了singleTop、singleTask、singleIntent,它可以工作,但在消息输入时不会更新活动,就像预览活动处于暂停状态一样。我怎样才能修好它 如果在恢复活动或销毁暂停的活动或可能重新启动活动时没有解决方案,有没有办法做到这一点 public void CustomNotification(

我已经创建了一个自定义通知,但是当我使用“主页”按钮暂停活动并且通知到达时,我按下通知,它将创建一个新的活动,并且不会继续预览活动,当我按下“上一步”按钮时,它将转到同一窗口中的预览活动。我尝试了
singleTop、singleTask、singleIntent
,它可以工作,但在消息输入时不会更新活动,就像预览活动处于暂停状态一样。我怎样才能修好它

如果在恢复活动或销毁暂停的活动或可能重新启动活动时没有解决方案,有没有办法做到这一点

public void CustomNotification(String strtext) {
        // Using RemoteViews to bind custom layouts into Notification
        RemoteViews remoteViews = new RemoteViews(getPackageName(),
                R.layout.customnotification);

        // Set Notification Title
        String strtitle = getString(R.string.customnotificationtitle);
        // Open NotificationView Class on Notification Click
        Intent intent = new Intent(this, NotificationView.class);
        // Send data to NotificationView Class
        intent.putExtra("title", strtitle);
        intent.putExtra("text", strtext);
        intent.putExtra("String T", "");
        //intent.putExtra("Integer C", 0);

        // Open NotificationView.java Activity
        PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                // Set Icon
                .setSmallIcon(R.drawable.ic_launcher)
                        // Set Ticker Message
                .setTicker(getString(R.string.customnotificationticker))
                        // Dismiss Notification
                .setAutoCancel(true)
                        // Set PendingIntent into Notification
                .setContentIntent(pIntent)
                        // Set RemoteViews into Notification
                .setContent(remoteViews);

        // Locate and set the Image into customnotificationtext.xml ImageViews
        remoteViews.setImageViewResource(R.id.imagenotileft,R.drawable.ic_launcher);
        remoteViews.setImageViewResource(R.id.imagenotiright,R.mipmap.ic_action_chat);

        // Locate and set the Text into customnotificationtext.xml TextViews
        remoteViews.setTextViewText(R.id.title,getString(R.string.customnotificationtitle));
        remoteViews.setTextViewText(R.id.text, strtext);

        // Create Notification Manager
        NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        // Build Notification with Notification Manager
        notificationmanager.notify(0, builder.build());

    }
日志:

当我按下通知时:

04-29 01:33:09.654  33449-33530/com.example.example E/STATE﹕NEW ACTIVITY
谢谢你的回答

编辑

答复:

答案是加上:

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                        | Intent.FLAG_ACTIVITY_NEW_TASK);

因为当我在同一个窗口中并按下home(主页)按钮时,活动处于暂停状态,然后如果有通知到达,它会将我带到该窗口,但没有更新消息,因此我需要创建一个新的意图,并删除先前处于暂停状态的意图。这样代码就完成了任务。

在您的服务代码中尝试以下内容以使用通知:

 private static final int NOTIFY_ME_ID = 1222;
   NotificationManager mNotificationManager = (NotificationManager) this
                .getSystemService(Context.NOTIFICATION_SERVICE);

     private void Notifi(String message){
        int requestID = (int) System.currentTimeMillis();
                Intent intent = new Intent(Intent.ACTION_MAIN);
                //CLEAR PREVIOUS ACTIVITIES AND CREATE A NEW ONE
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                        | Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setComponent(new ComponentName("packagenameofyourapplication",
                        "packagenameofyourapplication.NotificationView"));
                PendingIntent pintent = PendingIntent.getActivity(this, 0, intent,
                        PendingIntent.FLAG_UPDATE_CURRENT);

                Notification notif = new Notification.Builder(this)
                        .setContentTitle(getString(R.string.notification_titel)).setContentText(message)
                        .setSmallIcon(R.drawable.ic_launcher).setAutoCancel(true)
                        .setContentIntent(pintent).build();
                notif.flags = Notification.FLAG_NO_CLEAR;
                mNotificationManager.notify(NOTIFY_ME_ID, notif);

}

要更新活动,您应该覆盖
onNewIntent()

说:

void onNewIntent(意图)

对于在中将
launchMode
设置为“singleTop”的活动,会调用此命令 他们的包,或者如果客户使用
标志\u活动\u单个\u顶部
标志 调用
开始触觉(意图)
时。无论哪种情况,当活动 在活动堆栈顶部重新启动,而不是新的 将调用正在启动的活动的实例,
onNewIntent()
在现有实例上,具有用于重新启动的意图 它

在收到新的意图之前,活动将始终暂停,因此 您可以指望在此方法之后调用
onResume()

请注意,
getIntent()
仍然返回原始意图。你可以用
setIntent(Intent)
将其更新为新的Intent


注意:正如文档所说,您需要在活动标签(在AndroidManifest中)中设置
android:launchMode=“singleTop”

您在活动中定义了onResume()方法吗?@Keshav1234我定义了,为什么?它会引起问题吗?或者我必须重写它吗?如果你想在活动恢复时执行某些操作,你肯定需要重写onResume()方法,并在该方法中添加所有恢复操作。@Keshav1234但当我按下通知时,它没有恢复,它正在创建一个新的活动,这就是为什么。。。。previews活动仍在Resume上如果它正在创建新活动,那么您的活动似乎没有暂停,在onCreate和onResume中添加日志消息,然后查看执行的是哪种方法。为什么您认为这是一个答案,根据您的说法,这将如何解决问题?我认为@Anshuman没有阅读,我已经试过了,单任务实际上在试过所有事情后,这都起作用了,因为我说我的通知正在打开一个新活动,所以Intent.FLAG\u activity\u CLEAR\u TOP会删除暂停的预览意图,这对我的应用程序很好,我不想继续以前的活动,但创建了一个新的活动,所以我的意图是。FLAG_ACTIVITY_new_TASK)完成了这项工作。thak you我一开始不明白,这是4天的研究和数小时的调试。我是一个初学者,还有很多其他人,所以下次发布答案时请解释一下。谢谢,它帮了我不少忙lot@kobbycoder这是解决这个问题的一种方法。但是,如果您(或其他人)希望在仅更新少数内容的同时重用上一个实例,我的答案将派上用场。如果我设置了single Top,它将使我的程序工作方式不同,加载它是否需要时间?我是说至少还有几毫秒?我有一个函数,可以在意图被销毁(加密数据)后创建备份,所以我让它在线程上运行。这就是为什么我说我的应用程序会有不同的工作方式。@kobbycoder对延迟响应表示抱歉。不同——是的。你可能喜欢阅读,以便更好地理解。
 private static final int NOTIFY_ME_ID = 1222;
   NotificationManager mNotificationManager = (NotificationManager) this
                .getSystemService(Context.NOTIFICATION_SERVICE);

     private void Notifi(String message){
        int requestID = (int) System.currentTimeMillis();
                Intent intent = new Intent(Intent.ACTION_MAIN);
                //CLEAR PREVIOUS ACTIVITIES AND CREATE A NEW ONE
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                        | Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setComponent(new ComponentName("packagenameofyourapplication",
                        "packagenameofyourapplication.NotificationView"));
                PendingIntent pintent = PendingIntent.getActivity(this, 0, intent,
                        PendingIntent.FLAG_UPDATE_CURRENT);

                Notification notif = new Notification.Builder(this)
                        .setContentTitle(getString(R.string.notification_titel)).setContentText(message)
                        .setSmallIcon(R.drawable.ic_launcher).setAutoCancel(true)
                        .setContentIntent(pintent).build();
                notif.flags = Notification.FLAG_NO_CLEAR;
                mNotificationManager.notify(NOTIFY_ME_ID, notif);

}
protected void onNewIntent (Intent intent) {
    super.onNewIntent(intent);
    //reload your data here
}