Android ApiDemos示例解析(24):App->Launcher Shortcuts
来源:互联网 发布:nginx搭建web服务器 编辑:程序博客网 时间:2024/05/18 03:28
Android ApiDemos示例解析(24):App->Launcher Shortcuts
Android 操作系统对于<intent-filter>含有下列属性的Activity会在应用程序管理器(Launcher)显示一项,一般这个Activity对应于某个应用的主Activity。
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />
此外,如果用户想在设备的Home Screen上添加应用的快捷方式,可以在Launcher中长按这个应用的图标,Android系统会自动为该应用在Home Screen上添加一个快捷方式,名称和图标和在Launcher中的一样。
除了支持指向应用(主Activity)的快捷方式外,Android可以在Home Screen上定义指向Application中任意Activity的快捷方式。
比如说你的应用中有个功能用户可能会经常使用,比如说地图中查询地址,正常情况下用户需要先启动主Activity,可能需要经过几次菜单选择或是其它方式才能进到这个功能,用户可能感觉到不方便,这是可以为这个功能在Home Screen建立一个快捷方式,用户按这个快捷方式后会直接进入这个功能界面,即使这个Activity不是主Activity。
Launcher Shortcuts就是介绍了如何为某个非主Activity在Home Screen上建立一个快捷方式。
实现这个快捷方式,可以分下面几步来完成:
1.为需要创建快捷方式的Activity的<intent-filter>添加<action android:name=”android.intent.action.CREATE_SHORTCUT” /> ,标识这个Activity可以支持在Home Screen上添加快捷方式。Launcher Shortcuts 是采用的是activity-alias,activity-alias为Target的别名,类似于Activity.
2.添加相应用户添加快捷方式的代码,一般在Activity的onCreate方法中为Activity安装快捷方式:
1
if
(Intent.ACTION_CREATE_SHORTCUT.equals(action))
2
{
3
setupShortcut();
4
finish();
5
return
;
6
}
7
8
...
9
private
void
setupShortcut() {
10
// First, set up the shortcut intent.
11
//For this example, we simply create an intent that
12
// will bring us directly back to this activity.
13
//A more typical implementation would use a
14
// data Uri in order to display a more specific result,
15
//or a custom action in order to
16
// launch a specific operation.
17
18
Intent shortcutIntent =
new
Intent(Intent.ACTION_MAIN);
19
shortcutIntent.setClassName(
this
,
this
.getClass().getName());
20
shortcutIntent.putExtra(EXTRA_KEY,
"ApiDemos Provided This Shortcut"
);
21
22
// Then, set up the container intent (the response to the caller)
23
24
Intent intent =
new
Intent();
25
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
26
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.shortcut_name));
27
Parcelable iconResource = Intent.ShortcutIconResource.fromContext(
28
this
, R.drawable.app_sample_code);
29
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
30
31
// Now, return the result to the launcher
32
33
setResult(RESULT_OK, intent);
34
}
如果用户想为某个Activity创建快捷方式,方法是在Home Screen的空白处长按,这时Android会显示用户可以选择添加的桌面种类列表,选择Shortcut(快捷方式)后,Android会列出所有定义了android.intent.action.CREATE_SHORTCUT的所有应用:
此时如果选择ApiDemos,那么Add to Home Screen会启动LauncherShortcuts Activity,发出请求的Intent的action 会设置为Intent.ACTION_CREATE_SHORTCUT,因此可以在onCreate中使用Intent.ACTION_CREATE_SHORTCUT.equals(action)来判断请求是来自Add to Home Screen还是用户选择App->Launcher Shorts。如果是来自Add to Home Screen,Launcher Shortcuts则为本Activity创建一个快捷方式setupShortcut,然后退出。
Add to Home Screen 发出Intent请其后(运行startActivityForResult),预期从LauncherShortcuts返回一个结果,这也是为什么setupShortcut需要返回一个结果。对应创建的快捷方式的Intent至少需要定义三个参数:SHORTCUT_INTENT (value: Intent), SHORTCUT_NAME (value: String), SHORTCUT_ICON (value: Bitmap)或SHORTCUT_ICON_RESOURCE (value: ShortcutIconResource).
本例是为当前Activity(LauncherShortcuts)创建快捷方式,实际应用中可以根据需要为别的Activity或是提供一个列表供用户来选择需创建的快捷方式。
本例在Home Screen创建一个Sample 快捷方式,选择该快捷方式后,直接进入Launcher Shortcuts,而无需从App再进入Launcher Shortcuts
- Android ApiDemos示例解析(24):App->Launcher Shortcuts
- Android ApiDemos示例解析(24):App->Launcher Shortcuts
- Android ApiDemos示例解析(22):App->Dialog
- Android ApiDemos示例解析(23):App->Intents
- Android ApiDemos示例解析
- Android ApiDemos示例解析(3): App->Activity->Animation
- Android ApiDemos示例解析(4):App->Activity->Custom Dialog
- Android ApiDemos示例解析(5):App->Activity->Custom Title
- Android ApiDemos示例解析(6):App->Activity->Dialog
- Android ApiDemos示例解析(7):App->Activity->Forwarding
- Android ApiDemos示例解析(8):App->Activity->Hello world
- Android ApiDemos示例解析(9):App->Activity->Persistent State
- Android ApiDemos示例解析(10):App->Activity->QuickContactsDemo
- Android ApiDemos示例解析(11):App->Activity->Receive Result
- Android ApiDemos示例解析(12):App->Activity->Redirection
- Android ApiDemos示例解析(13):App->Activity->Reorder Activities
- Android ApiDemos示例解析(14):App->Activity->Save & Restore State
- Android ApiDemos示例解析(15):App->Activity->SetWallpaper
- Linux服务器集群系统(三) LVS集群中的IP负载均衡技术
- ArcEngine10 中引用ESRI.ArcGIS.Framework.dll 出现异常解决
- 各种移动GPU压缩纹理的使用方法
- 使用Android内部的DownloadProvider下载文件,并获取cache权限 .
- R频数分布图及箱线图
- Android ApiDemos示例解析(24):App->Launcher Shortcuts
- Xcode 4.1~4.6 + iOS 5、iOS 6免证书(iDP)开发+真机调试+生成IPA全攻略
- #define 的妙用
- Word,Excel大小写批量转换
- Java Swing编程
- python发送HTTP GET请求
- Web Service学习笔记
- C#调用Exe方法+实例分析
- wordpress杂记