首页 技术 正文
技术 2022年11月14日
0 收藏 860 点赞 4,053 浏览 3139 个字

一、判断是否已有快捷方式

    private String getAuthorityFromPermission(Context context, String permission){
if (permission == null) return null;
List<PackageInfo> packs = context.getPackageManager().getInstalledPackages(PackageManager.GET_PROVIDERS);
if (packs != null) {
for (PackageInfo pack : packs) {
ProviderInfo[] providers = pack.providers;
if (providers != null) {
for (ProviderInfo provider : providers) {
if (permission.equals(provider.readPermission)) return provider.authority;
if (permission.equals(provider.writePermission)) return provider.authority;
}
}
}
} return null;
}
private boolean hasShortcut(Context context,String shortCutName)
{
boolean has = false;
final ContentResolver cr = context.getContentResolver();
final String AUTHORITY = getAuthorityFromPermission(context, "com.android.launcher.permission.READ_SETTINGS"); final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/favorites?notify=true"); //确认content Provider中是否有快捷键信息
Cursor c = cr.query(CONTENT_URI,
new String[] {"title","iconResource" },
"title=?",
new String[] {shortCutString.trim()},
null);
if(c != null && c.getCount() > 0){
has= true ;
}
return has;
}

二、添加快捷方式

private void addShortCut(Context mContext)
{ boolean has = hasShortcut(mContext, mContext.getString(R.string.str_app_name)); if(has)
{
return;
} Intent shortCutIntent = null;
int shortCutNameId = R.string.app_name;
int shortCutIconId = R.drawable.app_icon;
String pkg = PACKAGE_NAME; boolean installed = isInstalledApp(mContext); if(installed)
{ Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
resolveIntent.setPackage(pkg);
List<ResolveInfo> apps = mContext.getPackageManager().queryIntentActivities(resolveIntent, PackageManager.GET_ACTIVITIES); shortCutIntent = new Intent();
if((apps != null) && (apps.size() != 0))
{
shortCutIntent.setComponent(new ComponentName(pkg, apps.get(0).activityInfo.name));
}
}
else
{
shortCutIntent = new Intent(mContext.getApplicationContext(), AppActivity.class);
} Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); //快捷方式的名称
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, mContext.getString(shortCutNameId));
shortcut.putExtra("duplicate", false); //不允许重复创建 //快捷方式的图标
ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(mContext.getApplicationContext(), shortCutIconId);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes); //程序入口
shortCutIntent.setAction(Intent.ACTION_MAIN);
shortCutIntent.addCategory(Intent.CATEGORY_LAUNCHER);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortCutIntent); mContext.sendBroadcast(shortcut);
}

三、删除快捷方式

   private void deleteShortCut(Context mContext)
{
Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
Intent shortCutIntent = new Intent(mContext.getApplicationContext(), AppActivity.class);
shortCutIntent.setAction(Intent.ACTION_MAIN);
shortCutIntent.addCategory(Intent.CATEGORY_LAUNCHER); // 快捷方式的名称
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, mContext.getString(R.string.app_name)); // 注意: ComponentName的第二个参数必须是完整的类名(包名+类名),否则无法删除快捷方式
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortCutIntent); mContext.sendBroadcast(shortcut);
}

四、被快捷方式启动的Activity的Intent-filter

            <intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,490
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,291