测试环境为Adnroid 2.1以上。第一步:AndroidManifest.xml 权限配置: 添加快捷方式权限: 验证快捷方式是否存在权限: 删除快捷方式权限: 代码如下:创建ShortCutSample类
package cn.haokuai.maya.suppliers.app;import android.app.Activity;import android.content.ContentResolver;import android.content.Intent;import android.database.Cursor;import android.net.Uri;import android.os.Parcelable;public class ShortCutSample {/** * 添加快捷方式 * */public void creatShortCut(Activity activity, String shortcutName,int resourceId) {Intent intent = new Intent();intent.setClass(activity, AppStart.class);/* 以下两句是为了在卸载应用的时候同时删除桌面快捷方式 */intent.setAction("android.intent.action.MAIN");intent.addCategory("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);// 发送广播。OKactivity.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 hasShortcut(Activity activity, String shortcutName) {String url = "";int systemversion = Integer.parseInt(android.os.Build.VERSION.SDK);/* 大于8的时候在com.android.launcher2.settings 里查询(未测试) */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 Welcome extends Activity {    private static final String TAG  = "Welcome"; final AppContext ac = (AppContext) getApplication(); private Button btn; private ViewFlow viewFlow;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        final View view = View.inflate(this, R.layout.welcome, null);        setContentView(view);        ShortCutSample sample =new ShortCutSample(); String shortcutName=getString(R.string.app_name); if(sample.hasShortcut(this, shortcutName)) sample.deleteShortCut(this,shortcutName); else sample.creatShortCut(this,shortcutName,R.drawable.ic_launcher); 
}}

   


更多相关文章

  1. Android(安卓)文件及文件夹操作
  2. android kitkat(4.4以上)各个版本的特性解析
  3. Android通过tcpdump抓包
  4. Android(安卓)实现截屏
  5. 如何使Android应用程序获取系统权限
  6. Android中动态添加╱删除的Spinner菜单
  7. Android下如何获取Mac地址
  8. 取WiFi MAC地址
  9. Android发送短信

随机推荐

  1. Hybrid App for Android 资源整理(初步整
  2. Android禁用键盘的所有按键
  3. android 设置状态栏颜色和字体颜色
  4. TextView垂直滚动
  5. android遍历map的六种方式
  6. Android开源项目分享
  7. [Android(安卓)Pro] ListView,GridView之
  8. 升级到3.6.3 之后,原项目出现错误 Found t
  9. TableLayout设置分隔线
  10. 【Android】实现打开关闭效果