Android官方开发文档Training系列课程中文版:通知用户之创建不同导航方式的Activity...

原文地址:http://android.xsoftlab.net/training/notify-user/navigation.html

设计通知时要考虑到用户所预想的导航体验。通常有以下两种情况:

常规的Activity(Regular activity)

  • 这里所启动的Activity是作为应用程序的正常流程部分出现的。

指定的Activity(Special activity)

  • 用户只会看到这个Activity,如果这个Activity是从通知启动的话。在直觉上,这个Activity是用来展示通知上的详细信息的。

设置常规的Activity

设置启动常规的Activity需要执行以下步骤:

  • 1.在清单文件中定义Activity的层级,最终的清单文件应该是这样的:
<activity
    android:name=".MainActivity"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity
    android:name=".ResultActivity"
    android:parentActivityName=".MainActivity">
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value=".MainActivity"/>
</activity>
  • 2.创建一个基于回退栈的Intent,它用来启动父Activity(下面的代码可能有误,请自行验证。):
int id = 1;
...
Intent resultIntent = new Intent(this, ResultActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack
stackBuilder.addParentStack(ResultActivity.class);
// Adds the Intent to the top of the stack
stackBuilder.addNextIntent(resultIntent);
// Gets a PendingIntent containing the entire back stack
PendingIntent resultPendingIntent =
        stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
...
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(id, builder.build());

设置启动指定的Activity

启动指定的Activity不需要回退栈,所以不需要在清单文件中定义Activity的层级,也不需要在代码中使用addParentStack()构建回退栈。相反的,使用清单文件来设置Activity的任务模式,并通过getActivity()创建PendingIntent就可以完成创建。

  • 1.在清单文件中,为Activity添加如下属性:

    • android:name=”activityclass” 指定该Activity的全限定名称.
    • android:taskAffinity=”” 与在代码中设置的FLAG_ACTIVITY_NEW_TASK标志一同使用。这可以确保这个Activity不会进入应用程序的默认任务栈中。任何已有的任务栈皆不会受到这个属性的影响。
    • android:excludeFromRecents=”true” 将该Activity从Recents中排除,所以用户不会意外的再通过返回键启动这个Activity.

下面的代码段展示了这个属性的设置:

<activity
    android:name=".ResultActivity"
...
    android:launchMode="singleTask"
    android:taskAffinity=""
    android:excludeFromRecents="true">
</activity>
...

下面的代码段演示了这个实现过程:

// Instantiate a Builder object.
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
// Creates an Intent for the Activity
Intent notifyIntent =
        new Intent(new ComponentName(this, ResultActivity.class));
// Sets the Activity to start in a new, empty task
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | 
        Intent.FLAG_ACTIVITY_CLEAR_TASK);
// Creates the PendingIntent
PendingIntent notifyIntent =
        PendingIntent.getActivity(
        this,
        0,
        notifyIntent,
        PendingIntent.FLAG_UPDATE_CURRENT
);
// Puts the PendingIntent into the notification builder
builder.setContentIntent(notifyIntent);
// Notifications are issued by sending them to the
// NotificationManager system service.
NotificationManager mNotificationManager =
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Builds an anonymous Notification object from the builder, and
// passes it to the NotificationManager
mNotificationManager.notify(id, builder.build());
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值