android 创建桌面快捷方式,4.0系统可用
16lz
2021-01-23
1、判断是否存在快捷方式:
title = ? 查询条件指的是创建快捷方式时设置的label字符串(Intent.EXTRA_SHORTCUT_NAME)
private boolean hasAddShortCut() { boolean isInstallShortcut = false; final ContentResolver cr = this.getContentResolver(); String AUTHORITY = "com.android.launcher2.settings"; final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/favorites?notify=true"); Cursor c = cr.query(CONTENT_URI, new String[] { "title", "iconResource" }, "title=?", new String[] { getString(R.string.app_name) }, null); if (c != null && c.getCount() > 0) { isInstallShortcut = true; } return isInstallShortcut; }
2、创建快捷方式
private void addShortCut(){ Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getResources().getString(R.string.app_name)); //设置是否可重复创建 shortcut.putExtra("duplicate", false); //设置显示的图标 Parcelable icon = Intent.ShortcutIconResource.fromContext(this,R.mipmap.ic_launcher_mail); shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,icon); //点击快捷方式发起的intent的定义 Intent intent = new Intent(Intent.ACTION_MAIN); intent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY); intent.addCategory(Intent.CATEGORY_LAUNCHER); //对于使用activity-alias控件的请用setClassName接口来设置启动的activity</span> //intent.setClass(MailActivityEmail.this, MailActivityEmail.class); intent.setClassName("com.kingsoft.email","com.kingsoft.email.activity.Welcome"); shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent); this.sendBroadcast(shortcut); }
更多相关文章
- android获取控件宽度
- Android 自定义控件之ViewPager Indicator实现方式
- Android 创建一个包含星期的格式字符串
- Android源码快速查找文件、搜索字符串和编译
- android仿iPhone滚轮控件实现及源码
- Android中的webView控件实现网页缩放
- android上传图片以及POST字符串
- Android 自定义控件实现点击波浪效果(九)
- 第八篇 TabHost控件