Android应用程序之桌面快捷方式

相信大家在使用很多的应用的时候都碰到过这样的一个情景,那就是当我们第一次点击使用这个应用的时候程序会主动的为我们创建一个桌面上的快捷方式,这样的好处是可以让用户更加方便的进入这个应用,省去找的麻烦,如:腾讯QQ,360安全卫士等都是。功能实现:你只需要在你的应用程序启动的第一个Activity里添加这样的一个方法:

1.简单使用方式

/** 
        * 创建快捷方式 
        */  
       public void createDeskShortCut() {  
   
              Log.i("coder","------createShortCut--------");  
              //创建快捷方式的Intent  
              Intent shortcutIntent = new Intent(  
                            "com.android.launcher.action.INSTALL_SHORTCUT");  
              //不允许重复创建  
              shortcutIntent.putExtra("duplicate",false);  
              //需要现实的名称  
              shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,  
                            getString(R.string.app_name));  
   
              //桌面快捷方式的图片  
              Parcelable icon = Intent.ShortcutIconResource.fromContext(  
                            getApplicationContext(),R.drawable.ic_launcher);  
   
              shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,icon);  
   		
		
	      //定义当点击桌面快捷方式时实现Activity跳转的intent对象
 	      Intent intent = new Intent(getApplicationContext(), MainActivity.class);  
              //下面两个属性是为了当应用程序卸载时桌面 上的快捷方式会删除  
              intent.setAction("android.intent.action.MAIN");  
              intent.addCategory("android.intent.category.LAUNCHER");  
              //点击快捷图片,运行的程序主入口  
              shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,intent);  
              //发送广播 
              sendBroadcast(shortcutIntent);  
       }  



记得在AndroidManifest.xml添加权限:
<uses-permissionandroid:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>

2.创建、删除、判断是否存在快捷方式的方法封装

<1>Android添加桌面快捷方式

/**
 * 为当前应用添加桌面快捷方式
 * 
 * @param cx
 * @param appName
 *            快捷方式名称
 */
public static void addShortcut(Context cx) {
    Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");

    Intent shortcutIntent = cx.getPackageManager()
            .getLaunchIntentForPackage(cx.getPackageName());
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    // 获取当前应用名称
    String title = null;
    try {
        final PackageManager pm = cx.getPackageManager();
        title = pm.getApplicationLabel(
                pm.getApplicationInfo(cx.getPackageName(),
                        PackageManager.GET_META_DATA)).toString();
    } catch (Exception e) {
    }
    // 快捷方式名称
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
    // 不允许重复创建(不一定有效)
    shortcut.putExtra("duplicate", false);
    // 快捷方式的图标
    Parcelable iconResource = Intent.ShortcutIconResource.fromContext(cx,
            R.drawable.ic_launcher);
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);

    cx.sendBroadcast(shortcut);
}

<2>Android删除桌面快捷方式

/**
 * 删除当前应用的桌面快捷方式
 * 
 * @param cx
 */
public static void delShortcut(Context cx) {
    Intent shortcut = new Intent(
            "com.android.launcher.action.UNINSTALL_SHORTCUT");

    // 获取当前应用名称
    String title = null;
    try {
        final PackageManager pm = cx.getPackageManager();
        title = pm.getApplicationLabel(
                pm.getApplicationInfo(cx.getPackageName(),
                        PackageManager.GET_META_DATA)).toString();
    } catch (Exception e) {
    }
    // 快捷方式名称
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
    Intent shortcutIntent = cx.getPackageManager()
            .getLaunchIntentForPackage(cx.getPackageName());
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    cx.sendBroadcast(shortcut);
}

<3>Android判断应用是否已存在桌面快捷方式

/**
 * 判断桌面是否已添加快捷方式
 * 
 * @param cx
 * @param titleName
 *            快捷方式名称
 * @return
 */
public static boolean hasShortcut(Context cx) {
    boolean result = false;
    // 获取当前应用名称
    String title = null;
    try {
        final PackageManager pm = cx.getPackageManager();
        title = pm.getApplicationLabel(
                pm.getApplicationInfo(cx.getPackageName(),
                        PackageManager.GET_META_DATA)).toString();
    } catch (Exception e) {
    }

    final String uriStr;
    if (android.os.Build.VERSION.SDK_INT < 8) {
        uriStr = "content://com.android.launcher.settings/favorites?notify=true";
    } else {
        uriStr = "content://com.android.launcher2.settings/favorites?notify=true";
    }
    final Uri CONTENT_URI = Uri.parse(uriStr);
    final Cursor c = cx.getContentResolver().query(CONTENT_URI, null,
            "title=?", new String[] { title }, null);
    if (c != null && c.getCount() > 0) {
        result = true;
    }
    return result;
}

<4>相关权限配置

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







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值