2011.07.08(5)——— android shortcut

2011.07.08(5)——— android shortcut

参考: http://www.bangchui.org/read.php?tid=13625
http://www.imobilebbs.com/wordpress/?p=1156
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。

这个才是强大之处 呵呵

实现这个快捷方式,可以分下面几步来完成:

1.为需要创建快捷方式的Activity的<intent-filter>添加<action android:name=”android.intent.action.CREATE_SHORTCUT” /> ,标识这个Activity可以支持在Home Screen上添加快捷方式。Launcher Shortcuts 是采用的是activity-alias,activity-alias为Target的别名,类似于Activity.

    
<activity android:name=".app.LauncherShortcuts"
                  android:label="@string/shortcuts">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.SAMPLE_CODE" />
            </intent-filter>

        </activity>
        <activity-alias android:name=".app.CreateShortcuts"
            android:targetActivity=".app.LauncherShortcuts"
            android:label="@string/sample_shortcuts">
            <intent-filter>
                <action android:name="android.intent.action.CREATE_SHORTCUT" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity-alias>


这个是android推荐的做法 当然 你也可以不用 也可以直接在LauncherShortcuts上面写 只需把"android.intent.action.CREATE_SHORTCUT"添加上去

2.添加相应用户添加快捷方式的代码,一般在Activity的onCreate方法中为Activity安装快捷方式:
if (Intent.ACTION_CREATE_SHORTCUT.equals(action))
{
 setupShortcut();
 finish();
 return;
}
 
...
private void setupShortcut() {
 // First, set up the shortcut intent.
 //For this example, we simply create an intent that
 // will bring us directly back to this activity.
 //A more typical implementation would use a
 // data Uri in order to display a more specific result,
 //or a custom action in order to
 // launch a specific operation.
 
 Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
 shortcutIntent.setClassName(this, this.getClass().getName());
 shortcutIntent.putExtra(EXTRA_KEY, "ApiDemos Provided This Shortcut");
 
 // Then, set up the container intent (the response to the caller)
 
 Intent intent = new Intent();
 intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
 intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.shortcut_name));
 Parcelable iconResource = Intent.ShortcutIconResource.fromContext(
 this,  R.drawable.app_sample_code);
 intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
 
 // Now, return the result to the launcher
 
 setResult(RESULT_OK, intent);
}



你可能感兴趣的:(android)