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

Android快捷方式(ShortCut)创建以及删除

2015-11-18 11:22 453 查看
之前公司用到为app应用创建快捷方式的方法,其实很简单,google提供了一个ShortcutIconResource的类,调用此类用广播发送出去就行了。这里提供一个基本的工具类大家可以直接调用就好。细心的同学会发现最后一个方法的注释,其实系统的app都是放在lunch的数据库里,有一个叫做faverite的表,里面存放的就是我们需要的东西啦。

import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.Intent.ShortcutIconResource;
import android.content.pm.PackageInfo;
import
4000
android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ProviderInfo;
import android.database.Cursor;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.text.TextUtils;

/**
* 桌面快捷方式有关的工具类
* @author xiaanming
*
*/
public class ShortCutUtils {
/**
* 快捷方式添加的action
*/
private final static String SHORTCUT_ADD_ACTION = "com.android.launcher.action.INSTALL_SHORTCUT";
/**
* 快捷方式删除的action
*/
private final static String SHORTCUT_DEL_ACTION = "com.android.launcher.action.UNINSTALL_SHORTCUT";
/**
* 读取数据库需要的权限
*/
private final static String READ_SETTINGS_PERMISSION = "com.android.launcher.permission.READ_SETTINGS";

/**
* 添加快捷方式到桌面,添加快捷方式需要添加用户权限
* <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
* @param context      当前的context对象
* @param resourceId    快捷方式的图标资源id
*/
public static void addShortCut(Context context, String name,int resourceId,String activity){
Intent shortCutIntent = new Intent(SHORTCUT_ADD_ACTION);
//添加快捷方式的名字
// 获取当前应用名称
String appName = null;
try {
appName = obtatinAppName(context);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
shortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
//不允许重复添加
shortCutIntent.putExtra("duplicate", false);
shortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent().setClassName(context,activity).setAction(Intent.ACTION_MAIN)
.addCategory(Intent.CATEGORY_LAUNCHER) .setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP) );
//添加快捷方式的图标
ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(context, resourceId);
shortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);

context.sendBroadcast(shortCutIntent);
}

public static void addShortCut(Context context, BitmapDrawable mBitmapDrawable,String name,String activity){
Intent shortCutIntent = new Intent(SHORTCUT_ADD_ACTION);
//添加快捷方式的名字
// 获取当前应用名称
String appName = null;
try {
appName = obtatinAppName(context);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
shortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
//不允许重复添加
shortCutIntent.putExtra("duplicate", false);
shortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent().setClassName(context,activity).setAction(Intent.ACTION_MAIN)
.addCategory(Intent.CATEGORY_LAUNCHER) .setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP) );
//      //添加快捷方式的图标
shortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, mBitmapDrawable.getBitmap());
context.sendBroadcast(shortCutIntent);
}

/**
* 删除桌面上的快捷方式,需要添加权限
* <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
* @param context
*/
public static void delShortcut(Context context, Activity activity) {
Intent shortcut = new Intent(SHORTCUT_DEL_ACTION);
// 获取当前应用名称
String appName = null;
try {
appName = obtatinAppName(context);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
// 快捷方式名称
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, appName);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN)
.addCategory(Intent.CATEGORY_LAUNCHER).setClassName(context.getPackageName(), context.getClass().getName()));
context.sendBroadcast(shortcut);
}

/**
* 判断桌面上是否有快捷方式,调用此方法需要添加权限
* <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
* @param context
* @return
* @throws NameNotFoundException
*/
public static boolean hasShortcut(Context context) {
String AUTHORITY = getAuthorityFromPermission(context, READ_SETTINGS_PERMISSION);

if (AUTHORITY == null) {
return false;
}
Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/favorites?notify=true");
String appName = null;
try {
appName = obtatinAppName(context);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
Cursor c = context.getContentResolver().query(CONTENT_URI, new String[] { "title" }, "title=?", new String[] { appName },null);
if (c != null && c.getCount() > 0) {
return true;
}
return false;
}

/**
* android系统桌面的基本信息由一个launcher.db的Sqlite数据库管理,里面有三张表
* 其中一张表就是favorites。这个db文件一般放在data/data/com.android.launcher(launcher2)文件的databases下
* 但是对于不同的rom会放在不同的地方
* 例如MIUI放在data/data/com.miui.home/databases下面
* htc放在data/data/com.htc.launcher/databases下面
* @param context
* @param permission  读取设置的权限  READ_SETTINGS_PERMISSION
* @return
*/
private static String getAuthorityFromPermission(Context context, String permission) {
if (TextUtils.isEmpty(permission)) {
return null;
}
List<PackageInfo> packs = context.getPackageManager().getInstalledPackages(PackageManager.GET_PROVIDERS);
if (packs == null) {
return null;
}
for (PackageInfo pack : packs) {
ProviderInfo[] providers = pack.providers;
if (providers != null) {
for (ProviderInfo provider : providers) {
if (permission.equals(provider.readPermission)|| permission.equals(provider.writePermission)) {
return provider.authority;
}
}
}
}
return null;
}

/**
* 获取应用的名称
* @param context
* @return
* @throws NameNotFoundException
*/
private static String obtatinAppName(Context context) throws NameNotFoundException{
PackageManager packageManager = context.getPackageManager();
return packageManager.getApplicationLabel(packageManager.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA)).toString();
}

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