如何定义android 启动的activity,从通知启动 Activity

当您从通知启动 Activity 时,必须保留用户的预期导航体验。点按“返回”应该让用户通过应用的正常工作流程返回到主屏幕,而打开“最近”屏幕应该将 Activity 显示为一项单独的任务。为了保留该导航体验,您应该在新的任务中启动 Activity。

创建通知一文中介绍了为通知设置点按行为的基本方法,而此页面介绍了如何为通知的操作设置 任务和返回堆栈。但是,具体操作取决于您需要启动的 Activity 类型:

常规 Activity

这类 Activity 是应用的正常用户体验流程的一部分。因此,当用户从通知转到这类 Activity 时,新任务应包括完整的返回堆栈,以便用户可以按“返回”按钮并沿应用层次结构向上导航。

特殊 Activity

只有当 Activity 从通知启动时,用户才可以看到此类 Activity。从某种意义上来说,这类 Activity 通过提供通知本身难以显示的信息来扩展通知界面。因此,这类 Activity 不需要返回堆栈。

设置常规 Activity PendingIntent

如需从通知启动“常规 Activity”,请使用

定义应用的 Activity 层次结构

通过向应用清单文件中的每个

android:name=".MainActivity"

android:label="@string/app_name" >

android:name=".ResultActivity"

android:parentActivityName=".MainActivity" />

...

构建包含返回堆栈的 PendingIntent

如需启动包含 Activity 的返回堆栈的 Activity,您需要创建

只要您为每个 Activity 定义了父 Activity(如上文所述),就可以调用

Kotlin

// Create an Intent for the activity you want to start

val resultIntent = Intent(this, ResultActivity::class.java)

// Create the TaskStackBuilder

val resultPendingIntent: PendingIntent? = TaskStackBuilder.create(this).run {

// Add the intent, which inflates the back stack

addNextIntentWithParentStack(resultIntent)

// Get the PendingIntent containing the entire back stack

getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT)

}Java

// Create an Intent for the activity you want to start

Intent resultIntent = new Intent(this, ResultActivity.class);

// Create the TaskStackBuilder and add the intent, which inflates the back stack

TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

stackBuilder.addNextIntentWithParentStack(resultIntent);

// Get the PendingIntent containing the entire back stack

PendingIntent resultPendingIntent =

stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

如有必要,您可以通过调用

然后,您可以像往常一样将

Kotlin

val builder = NotificationCompat.Builder(this, CHANNEL_ID).apply {

setContentIntent(resultPendingIntent)

...

}

with(NotificationManagerCompat.from(this)) {

notify(NOTIFICATION_ID, builder.build())

}Java

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);

builder.setContentIntent(resultPendingIntent);

...

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);

notificationManager.notify(NOTIFICATION_ID, builder.build());

设置特殊 Activity PendingIntent

由于从通知启动的“特殊 Activity”不需要返回堆栈,因此您可以通过调用

在您的清单中,将以下属性添加到 与您将在代码中使用的

用于从“最近”中排除新任务,以免用户意外返回它。

例如:

android:name=".ResultActivity"

android:launchMode="singleTask"

android:taskAffinity=""

android:excludeFromRecents="true">

构建并发出通知:

例如:

Kotlin

val notifyIntent = Intent(this, ResultActivity::class.java).apply {

flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK

}

val notifyPendingIntent = PendingIntent.getActivity(

this, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT

)Java

Intent notifyIntent = new Intent(this, ResultActivity.class);

// Set the Activity to start in a new, empty task

notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK

| Intent.FLAG_ACTIVITY_CLEAR_TASK);

// Create the PendingIntent

PendingIntent notifyPendingIntent = PendingIntent.getActivity(

this, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT

);

然后,您可以像往常一样将

Kotlin

val builder = NotificationCompat.Builder(this, CHANNEL_ID).apply {

setContentIntent(notifyPendingIntent)

...

}

with(NotificationManagerCompat.from(this)) {

notify(NOTIFICATION_ID, builder.build())

}Java

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);

builder.setContentIntent(notifyPendingIntent);

...

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);

notificationManager.notify(NOTIFICATION_ID, builder.build());

如需详细了解各个任务选项以及返回堆栈的运作方式,请参阅任务和返回堆栈。如需查看使用通知的示例代码,请参阅 Android 通知示例。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值