Android添加(创建)、删除及判断是否存在桌面快捷方式的方法
本文实例讲述了Android添加(创建)、删除及判断是否存在桌面快捷方式的方法。分享给大家供大家参考。具体实现方法如下:
/**
*判断桌面是否已添加快捷方式
*
*@paramcx
*@paramtitleName
*快捷方式名称
*@return
*/
publicstaticbooleanhasShortcut(Contextcx){
booleanresult=false;
//获取当前应用名称
Stringtitle=null;
try{
finalPackageManagerpm=cx.getPackageManager();
title=pm.getApplicationLabel(
pm.getApplicationInfo(cx.getPackageName(),
PackageManager.GET_META_DATA)).toString();
}catch(Exceptione){
}
finalStringuriStr;
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";
}
finalUriCONTENT_URI=Uri.parse(uriStr);
finalCursorc=cx.getContentResolver().query(CONTENT_URI,null,
"title=?",newString[]{title},null);
if(c!=null&&c.getCount()>0){
result=true;
}
returnresult;
}
/**
*删除当前应用的桌面快捷方式
*
*@paramcx
*/
publicstaticvoiddelShortcut(Contextcx){
Intentshortcut=newIntent(
"com.android.launcher.action.UNINSTALL_SHORTCUT");
//获取当前应用名称
Stringtitle=null;
try{
finalPackageManagerpm=cx.getPackageManager();
title=pm.getApplicationLabel(
pm.getApplicationInfo(cx.getPackageName(),
PackageManager.GET_META_DATA)).toString();
Log.v("test","title:"+title);
}catch(Exceptione){
}
//快捷方式名称
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,title);
IntentshortcutIntent=cx.getPackageManager()
.getLaunchIntentForPackage(cx.getPackageName());
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,shortcutIntent);
cx.sendBroadcast(shortcut);
}
/**
*为当前应用添加桌面快捷方式
*
*@paramcx
*@paramappName
*快捷方式名称
*/
publicstaticvoidaddShortcut(Contextcx){
Intentshortcut=newIntent(
"com.android.launcher.action.INSTALL_SHORTCUT");
IntentshortcutIntent=cx.getPackageManager()
.getLaunchIntentForPackage(cx.getPackageName());
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,shortcutIntent);
//获取当前应用名称
Stringtitle=null;
try{
finalPackageManagerpm=cx.getPackageManager();
title=pm.getApplicationLabel(
pm.getApplicationInfo(cx.getPackageName(),
PackageManager.GET_META_DATA)).toString();
Log.v("test","title:"+title);
}catch(Exceptione){
}
//快捷方式名称
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,title);
//不允许重复创建(不一定有效)
shortcut.putExtra("duplicate",false);
//快捷方式的图标
ParcelableiconResource=Intent.ShortcutIconResource.fromContext(cx,R.drawable.icon);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,iconResource);
cx.sendBroadcast(shortcut);
}
希望本文所述对大家的Android程序设计有所帮助。