测试环境为Adnroid 2.1以上。
第一步:
AndroidManifest.xml 权限配置:
添加快捷方式权限:
代码如下:
<uses-permiss
ion android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
验证快捷方式是否存在权限:
代码如下:
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
删除快捷方式权限:
代码如下:
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
代码:
代码如下:
public class ShortCutSample {
public void creatShortCut(Activity activity,String shortcutName,int resourceId)
{
Intent intent = new Intent();
intent.setClass(activity, activity.getClass());
intent.setAction("android.intent.action.M
aiN");
intent.addCate
Gory("android.intent.category.LAUNCHER");
Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
//不允许重复创建
shortcutintent.putExtra("duplicate", false);
//需要现实的名称
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortcutName);
//快捷图片
Parcelable icon = Intent.ShortcutIconResource.fromContext(activity.getApplicationContext(), resourceId);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
//点击快捷图片,运行的程序主入口
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
//发送广播。OK
activity.sendBroadcast(shortcutintent);
}
public void deleteShortCut(Activity activity,String shortcutName)
{
Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
//快捷方式的名称
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,shortcutName);
//在网上看到到的基本都是一下几句,测试的时候发现并不能删除快捷方式。
//String appClass = activity.getPackageName()+"."+ activity.getLocalClassName();
//ComponentName comp = new ComponentName( activity.getPackageName(), appClass);
//shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));
Intent intent = new Intent();
intent.setClass(activity, activity.getClass());
intent.setAction("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,intent);
activity.sendBroadcast(shortcut);
}
public boolean ha
sshortcut(Activity activity,String shortcutName)
{
String url = "";
int systemversion = Integer.parseInt(android.os.Build.VERSION.SDK);
if(systemversion < 8){
url = "content://com.android.launcher.settings/favorites?notify=true";
}else{
url = "content://com.android.launcher2.settings/favorites?notify=true";
}
ContentResolver resolver = activity.getContentResolver();
Cursor cursor = resolver.query(Uri.parse(url), null, "title=?",new String[] {shortcutName}, null);
if (cursor != null && cursor.moveToFirst()) {
cursor.close();
return true;
}
return false;
}
}
调用测试代码:
代码如下:
public class mainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ShortCutSample sample =new ShortCutSample();
String shortcutName=getString(R.string.app_name);
if(sample.hasShortcut(this, shortcutName))
sample.deleteShortCut(this,sh
ortcutName);
else
sample.creatShortCut(this,shortcutName,R.drawable.icon);
}
}
在网上找了很久都是一样的代码,删除那块搞了一个下午才弄好,其实很简单的东东。
第一次发文章,Adnroid新人。多多交流和指导呀。呵呵。
您可能感兴趣的文章:android ListView内数据的动态添加与删除实例代码Android实现
WEBView删除
缓存的方法Android Studio彻底删除
项目 Android Studio彻底删除ModuleAndroid Recyclerview实现多选,单选,全选,反选,批量删除的功能Android高仿QQ6.0侧滑删除实例代码Android通讯录
开发之删除功能的实现方法Android中删除Preference详解Android中删除文件以及文件夹的命令记录Android
递归方式删除某文件夹下的所有文件(.mp3文件等等)android实现简单左滑删除控件
0