如何使用通过通知传递的意图更新 Activity

How do I update Activity with intents passed via Notification

我已经创建了一个自定义通知,但是当我使用主页按钮暂停 activity 并且通知到达时,我按下通知并创建了一个新的 activity 并且没有恢复预览activity,当我按下后退按钮时,它会转到相同的预览 window。我试过 singleTop, singleTask, singleIntent,它可以工作,但是当消息进入时它不会更新 activity,就像预览 activity 处于暂停状态一样。我该如何解决?

如果在恢复 activity 或销毁暂停的 activity 或者重新启动 activity 方面没有解决方案,有没有办法做到这一点?

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:29:10.453  29001-29001/com.example.example E/STATE﹕NEW ACTIVITY
04-29 01:29:15.376  29501-29501/com.example.example D/Activity﹕ Activity.onPause(), editTextTapSensorList size: 0
04-29 01:30:06.981  29501-29501/com.example.example E/STATE﹕ RESUMING

当我按下通知时:

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);

因为当我在同一个 window 并按下主页按钮时,activity 处于暂停状态,然后如果有通知到达,它会把我带到那个 window 但是没有更新消息,所以我需要创建一个新的意图并删除之前暂停的意图。这样代码就完成了工作。

在您的服务代码中试试这个以使用通知:

 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);

}

要更新您的 activity,您应该覆盖 onNewIntent():

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

Android Docs 说:

void onNewIntent (Intent intent)

This is called for activities that set launchMode to "singleTop" in their package, or if a client used the FLAG_ACTIVITY_SINGLE_TOP flag when calling startActivity(Intent). In either case, when the activity is re-launched while at the top of the activity stack instead of a new instance of the activity being started, onNewIntent() will be called on the existing instance with the Intent that was used to re-launch it.

An activity will always be paused before receiving a new intent, so you can count on onResume() being called after this method.

Note that getIntent() still returns the original Intent. You can use setIntent(Intent) to update it to this new Intent.

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