gpt4 book ai didi

android - 如何使用通过通知传递的 Intent 更新 Activity

转载 作者:行者123 更新时间:2023-12-05 00:14:23 25 4
gpt4 key购买 nike

我已经创建了一个自定义通知,但是当我使用主页按钮暂停 Activity 并且通知到达时,我按下通知并且它创建了一个新 Activity 并且不会恢复预览 Activity 并且当我按下后退按钮时它会消失到同一窗口的预览。我已经尝试过 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);

因为当我在同一个窗口中并按下主页按钮时, Activity 暂停,然后如果有通知到达它会将我带到那个窗口但没有更新消息所以我需要创建一个新的 Intent 并删除前一个暂停了。这样代码就完成了工作。

最佳答案

要更新您的 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"

关于android - 如何使用通过通知传递的 Intent 更新 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29934940/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com