gpt4 book ai didi

android - 在与 PendingIntent 相同的 Activity 中选择通知,但 ViewPage 不同

转载 作者:行者123 更新时间:2023-11-30 02:54:48 24 4
gpt4 key购买 nike

场景:我的应用程序有一个包含 ViewPager 的 Activity ,它显示两个 fragment (称为页面 A 和页面 B)。

问题:当后台事件触发页面 A 的通知时,用户在页面 B 上,选择通知什么都不做;即没有对 onCreate()、onResume()、onNewIntent()、onResumeFragments() 或 onStart() 的回调,用户停留在页面 B。

问题:

  • 当用户访问时,如何让 ViewPager 显示页面 A已选择通知并在页面 B 上?

  • 是否有可能在 Activity 中检测用户何时选择指向同一 Activity 的通知?

通知代码:

// Create a notification
final Context ctx = this;
Intent notificationIntent = new Intent(ctx, ActivityMain.class);
notificationIntent.putExtra(INTENT_EXTRA_NOTIFICATION, true);

NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
Resources res = ctx.getResources();

PendingIntent pendingIntent = PendingIntent.getActivity(ctx, REQUESTCODE_ACTIVITYMAIN_LISTFRAGMENT, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);

Notification.Builder builder = new Notification.Builder(ctx);
builder.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_launcher)//required
.setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.ic_launcher))
.setContentTitle(res.getString(R.string.notification_title))//required
.setContentText(data.description)//required
.setTicker(res.getString(R.string.notification_title))
.setWhen(System.currentTimeMillis())
.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS)
.setAutoCancel(true);

Notification notification = builder.build();

notificationManager.notify(mNotificationID, notification);

也许有一个标志/配置可以添加到通知 Intent 中以强制 Activity 识别选定的通知?

最佳答案

这需要一些努力,但这是在为 已经在前台运行的 Activity 选择通知时切换 ViewPager 选项卡的解决方案:

1) 创建带有标志 FLAG_ACTIVITY_NEW_TASK 的通知和一个额外的标志来表明通知启动了这个 Activity

// Create a notification
final Context ctx = this;
Intent notificationIntent = new Intent(ctx, ActivityMain.class);
notificationIntent.putExtra(INTENT_EXTRA_NOTIFICATION, true);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//see rest of code in my question to create the notification

2) 使用 singleTop 为 Activity 更新 list

<activity
android:name="com.cool.app.ActivityMain"
android:label="@string/app_name"
android:launchMode="singleTop" >
</activity>

3) 在 ActivityMain(或任何您的 Activity )中覆盖 onNewIntent()

@Override
protected void onNewIntent(Intent intent)
{
Log.i(TAG,"onNewIntent");
super.onNewIntent(intent);
setIntent(intent);//needed so that getIntent() doesn't return null inside onResume()
}

4) 在 ActivityMain(或任何您的 Activity )中覆盖 onResume() 并包含切换标签的逻辑

@Override
protected void onResume()
{
super.onResume();

// Test if a Notification caused this activity to launch - if so then select the first tab
// NOTE: we get here from a notification by using Intent.FLAG_ACTIVITY_NEW_TASK
Bundle extras = getIntent().getExtras();
if (extras != null)
{
final boolean bIsLaunchedFromNotification = extras.getBoolean(INTENT_EXTRA_NOTIFICATION);
if (bIsLaunchedFromNotification)
{
final ActionBar actionBar = getActionBar();
actionBar.setSelectedNavigationItem(0);
}
}
}

5) 对于良好的形式 - 在 ActivityMain(或应用程序)中,定义常量以传递额外的内容

final public static String INTENT_EXTRA_NOTIFICATION = "fromNotification";

关于android - 在与 PendingIntent 相同的 Activity 中选择通知,但 ViewPage 不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23462258/

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