Android如何创建桌面快捷方式

Android创建桌面的快捷方式 概述 :创建桌面快捷方式相当与创建一个程序的入口,就像我们程序在安装完毕后会自动创建一个图标到桌面。其实创建桌面快捷方式跟创建一个程序入口差不多,但是像QQ会话一样创建一个QQ好友的会话快捷方式,就得动态的创建图标,名字了。

1.首先权限是必不可少的

<uses-permission android:name=”com.android.launcher.permission.INSTALL_SHORTCUT” />

2.然后就是在你项目配置文件里面配置

 <activity      android:name="com.easemob.chatuidemo.activity.ChatActivity" >      <intent-filter>        <category android:name="android.intent.category.LAUNCHER" />        <action android:name="android.intent.action.CREATE_SHORTCUT" />      </intent-filter>  </activity>

这个actvity即为你要快捷方式点击后跳转的那一个activity

3.然后就是你要创建快捷方式的方法。

代码如下:

 public void CreateShotCut(final Context context, final Class<?> clazz,      final String name, final String image) {    Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);    // 加入action,和category之后,程序卸载的时候才会主动将该快捷方式也卸载    shortcutIntent.addCategory(Intent.CATEGORY_LAUNCHER);    shortcutIntent.setClass(context, clazz);    /**     * 创建一个Bundle对象让其保存将要传递的值     */    Bundle bundle = new Bundle();    bundle.putString("userId", userId);    shortcutIntent.putExtras(bundle);    /**     * 设置这条属性,可以使点击快捷方式后关闭其他的任务栈的其他activity,然后创建指定的acticity     */    shortcutIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);    // 创建快捷方式的Intent    Intent shortcut = new Intent(Intent.ACTION_CREATE_SHORTCUT);    // 不允许重复创建    shortcut.putExtra("duplicate", false);    // 点击快捷图片,运行的程序主入口    shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);    // 需要现实的名称    shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);    // 快捷图片    Parcelable icon = Intent.ShortcutIconResource.fromContext(        getApplicationContext(), R.drawable.ic_launcher);    shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);    shortcut.setAction("com.android.launcher.action.INSTALL_SHORTCUT");    context.sendBroadcast(shortcut);  }

这行代码的重要性就在如果没有这一行,那么在你点击这个快捷方式,跳转的时候就会直接跳到这个应用的栈顶(如果指定的activity在栈顶,也不会跳转其上而是销毁)而不是指定的那一个Activity(刚开始没加这条属性的时候,一直跳转不到指定的activity上)。

shortcutIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

如果想要动态的添加图片即创建快捷方式的时候获取网路上的图片来进行设置其快捷图片则使用

// Intent.EXTRA_SHORTCUT_ICON 是bitmap对象 shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON,bitmap);

这行代码,你可以请求网路图片后转换为BitMap后设置进去。

ok动态的创建快捷方式就这样完成了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

可偏偏。多么温柔,一出口便是相互指责和嘲讽。

Android如何创建桌面快捷方式

相关文章:

你感兴趣的文章:

标签云: