您的位置:首页 > 移动开发 > Android开发

Android创建桌面快捷方式

2014-03-27 22:05 323 查看
首先创建、删除快捷方式需要权限:

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

推荐方案:

这个是我自己使用的方案,下面其他的是备选方案,万一这个有问题的,可以结合下面的方案做一下调整。

删除快捷方式可以根据需要取舍,Android的快捷方式一旦创建,如果新版本更新App图标,在覆盖安装的情况下是不会将之前创建的快捷方式图标更换成新图标的,除非手动删除再创建新的快捷方式,或者卸载App,否则这个快捷方式的图标是不会改变的,当需要保证图标是最新的又不需要用户手动操作,删除快捷方式的方法就派上用场了。

public class Utils {

/**
* 为程序创建桌面快捷方式
* 与delShortcut是一套
* @param context
* @param clazz  启动Activity
*/
public static void addShortcut(Context context,Class<? extends Activity> clazz) {
//此处是为了判断是否已经有了快捷方式
if(Utils.hasShortCut(context)){
return;
}

Intent shortcut = new Intent(
"com.android.launcher.action.INSTALL_SHORTCUT");

// 快捷方式的名称
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,
context.getString(R.string.app_name));
shortcut.putExtra("duplicate", false); // 不允许重复创建

Intent intent = new Intent(context, clazz);
intent.setAction("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);

// 快捷方式的图标
ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(
context, R.drawable.ic_launcher);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);

context.sendBroadcast(shortcut);
}

/**
* 删除程序的快捷方式
* @param context
* @param clazz   启动Activity
*/
public static void delShortcut(Context context,Class<? extends Activity> clazz) {
Intent shortcut = new Intent(
"com.android.launcher.action.UNINSTALL_SHORTCUT");

// 快捷方式的名称
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,
context.getString(R.string.app_name));

Intent intent = new Intent(context, clazz);
intent.setAction("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");

shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,intent);

context.sendBroadcast(shortcut);

}

/**
* 判断当前应用在桌面是否有桌面快捷方式
*
* @param context
*/
public static boolean hasShortCut(Context context) {
try {
Uri uri = null;
String spermi = getAuthorityFromPermission(context,
"com.android.launcher.permission.READ_SETTINGS");
if (spermi == null) {
return false;
}
if (getSystemVersion() < 8) {
uri = Uri.parse("content://" + spermi
+ "/favorites?notify=true");
} else {
uri = Uri.parse("content://" + spermi
+ "/favorites?notify=true");
}
final ContentResolver cr = context.getContentResolver();
Cursor cursor = cr.query(uri, null, "title=?", new String[] { context
.getResources().getString(R.string.app_name) }, null);
if (cursor != null && cursor.getCount() > 0) {
cursor.close();
return true;
} else {
return false;
}
} catch (Exception e) {
return hasShortcut(context);
}

}

public static String getAuthorityFromPermission(Context context,
String permission) {

if (permission == null)
return null;
List<PackageInfo> packs = context.getPackageManager()
.getInstalledPackages(PackageManager.GET_PROVIDERS);
if (packs != null) {
for (PackageInfo pack : packs) {
ProviderInfo[] providers = pack.providers;
if (providers != null) {
for (ProviderInfo provider : providers) {
if (provider.readPermission != null) {
if ((provider.readPermission).equals(permission)) {
return provider.authority;
}
}
}
}
}
}
return null;

}

public static int getSystemVersion() {
return android.os.Build.VERSION.SDK_INT;
}

/**
* 当读取桌面快捷方式权限有问题时,执行该方法判断
* @param context
* @return
*/
private static boolean hasShortcut(Context context) {
try {
boolean isInstallShortcut = false;
Uri uri = null;
if (getSystemVersion() < 8) {
uri = Uri
.parse("content://com.android.launcher.settings/favorites?notify=true");
} else {
uri = Uri
.parse("content://com.android.launcher2.settings/favorites?notify=true");
}
Cursor c = context.getContentResolver().query(uri,
new String[] { "title", "iconResource" }, "title=?",
new String[] { context.getString(R.string.app_name).trim() },
null);
if (null != c && c.getCount() > 0) {
isInstallShortcut = true;
}

return isInstallShortcut;
} catch (Exception e) {
return true;
}

}

}

备选方案:

/**
* 为程序创建桌面快捷方式
*/
private void addShortcut(){

Intent intent = new Intent();
intent.setAction("android.intent.action.MAIN");//使快捷方式和应用程序关联,卸载时可以一起卸载掉
intent.setClass(this.getApplicationContext(), StartActivity.class);//表明程序启动的activity

shortcut= new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
//快捷方式的名称
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
shortcut.putExtra("duplicate", false); //不允许重复创建
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
//快捷方式的图标
ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
sendBroadcast(shortcut);
}
/**
* 删除程序的快捷方式
*/
private void delShortcut(){
Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");

//快捷方式的名称
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));

//指定当前的Activity为快捷方式启动的对象: 如 com.everest.video.VideoPlayer
//注意: ComponentName的第二个参数必须是完整的类名(包名+类名),否则无法删除快捷方式
String appClass = this.getPackageName() + "." +this.getLocalClassName();
ComponentName comp = new ComponentName(this.getPackageName(), appClass);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));

sendBroadcast(shortcut);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ANDROID 快捷方式 ICON