桌面快捷方式

本文详细介绍了在Android系统中创建和删除桌面快捷方式的方法,包括所需权限、工具类ShortcutUtils的实现,以及如何通过发送广播来实现快捷方式的创建与删除。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原生的Android系统装的Android应用默认不会显示在桌面快捷方式上,好多程序为了增强粘性而让程序自动生成快捷方式。而生成的方式就是给桌面发送广播。
(1)创建删除快捷方式需要权限:

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

(2)工具类ShortcutUtils.java

public class ShortcutUtils {

/**
 * 创建快捷方式
 * @param context
 * @param shortcutName   快捷方式名称
 * @param iconRes    快捷方式图标 R.mipmap/R.drawable
 * @param actionIntent   快捷方式动作
 * @param allowRepeat    是否允许重复创建
 */
public static void addShortcut(Context context, String shortcutName, int iconRes,
                               Intent actionIntent,boolean allowRepeat){
    Intent shortcutintent = new Intent("com.android.launch.action.INSTALL_SHORTCUT");
    shortcutintent.putExtra("duplicate",allowRepeat);
    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME,shortcutName);
    Parcelable icon = Intent.ShortcutIconResource.fromContext(context.getApplicationContext(),iconRes);
    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,icon);
    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,actionIntent);
    context.sendBroadcast(shortcutintent);

}

/**
 * 创建快捷方式
 * @param context
 * @param shortcutName   快捷方式名称
 * @param bitmap    快捷方式图标 ,设置图片方式和上面的方法不同
 * @param actionIntent   快捷方式动作
 * @param allowRepeat   是否允许重复创建
 */
public static void addShortcut(Context context, String shortcutName, Bitmap bitmap,
                               Intent actionIntent,boolean allowRepeat){
    Intent shortcutintent = new Intent("com.android.launch.action.INSTALL_SHORTCUT");
    shortcutintent.putExtra("duplicate",allowRepeat);
    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME,shortcutName);
    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,bitmap);
    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,actionIntent);
    context.sendBroadcast(shortcutintent);
}

/**
 * 删除快捷方式
 * @param context
 * @param name
 * @param actionIntent
 * @param allowRepeat
 */
public  void deleteShortcut(Context context,String name,Intent actionIntent,
                            boolean allowRepeat){
    Intent shortcutintent = new Intent("com.android.launch.action.INSTALL_SHORTCUT");
    shortcutintent.putExtra("duplicate",allowRepeat);
    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME,name);
    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,actionIntent);
    context.sendBroadcast(shortcutintent);
 }
}

(3) 调用代码

public void addShortCut(){
   Log.i("addShortCut","addShortCut is running ");
   Intent intent = new Intent(Intent.ACTION_VIEW);
   intent.setData(Uri.parse("http://www.baidu.com"));
   intent.addCategory(Intent.CATEGORY_LAUNCHER);   //防止重复启动App
   ShortcutUtils.addShortcut(this,"打开百度",R.
           drawable.baidu,intent,false);
}

以上是《爱上Android》这本书中的示例,大家有补充的希望提醒,谢谢!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值