总结:android 创建快捷方式的两种方式+判断是否已经创建+删除快捷方式
发布日期:2021-07-01 05:57:05 浏览次数:3 分类:技术文章

本文共 4148 字,大约阅读时间需要 13 分钟。

1.   在清单文件里面进行注册:例如:

这样,就会自动加入到 系统launcher的快捷方式里面

2.  手动创建快捷方式

public static void createSystemSwitcherShortCut(Context context) {		final Intent addIntent = new Intent(				"com.android.launcher.action.INSTALL_SHORTCUT");		final Parcelable icon = Intent.ShortcutIconResource.fromContext(				context, R.drawable.ic_switcher_shortcut); // 获取快捷键的图标		addIntent.putExtra("duplicate", false);		final Intent myIntent = new Intent(context,				SystemSwitcherActivity.class);		myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);		addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,				context.getString(R.string.switch_widget));// 快捷方式的标题		addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);// 快捷方式的图标		addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, myIntent);// 快捷方式的动作		context.sendBroadcast(addIntent);	}
更具有通用性的写法如下:
/**      * 为程序创建桌面快捷方式      */     private void addShortcut(){          Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");                         //快捷方式的名称          shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));          shortcut.putExtra("duplicate", false); //不允许重复创建           /****************************此方法已失效*************************/        //ComponentName comp = new ComponentName(this.getPackageName(), "."+this.getLocalClassName());          //shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));         /******************************end*******************************/     Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);     shortcutIntent.setClassName(this, this.getClass().getName());     shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);         //快捷方式的图标          ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);          shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);                         sendBroadcast(shortcut);      }

小结:一般做法是在设置里面加上手动创建快捷方式的设置。

            在程序第一次启动的时候,手动创建一次快捷方式。

3.判断是否已经创建了快捷方式(在某些机型中需要判断)

private boolean hasShortcut(){        boolean isInstallShortcut = false;        final ContentResolver cr = activity.getContentResolver();        final String AUTHORITY ="com.android.launcher.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[] {mapViewActivity.getString(R.string.app_name).trim()}, null);        if(c!=null && c.getCount()>0){            isInstallShortcut = true ;        }        return isInstallShortcut ;

4.删除

/**      * 删除程序的快捷方式      */     private void delShortcut(){          Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");                         //快捷方式的名称          shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));          String appClass = this.getPackageName() + "." +this.getLocalClassName();          ComponentName comp = new ComponentName(this.getPackageName(), appClass);          shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));                         sendBroadcast(shortcut);                     }

5.声明权限

在AndroidManifest.xml 文件中声明 创建和删除快捷方式时声明权限

6.一个应用开启多个的问题

现在都很流行在桌面上为自己的应用创建快捷方式,网上也很多例子,但是基本都是同一个,创建快捷方式的手法是对的,但是通过快捷方式开启自己的应用的时候会发现程序菜单里头打开的应用和桌面快捷方式打开的应用竟然不是同一个,这样会导致一个应用开启了多个。
 
一开始从activity的加载模式入手,把默认的activity的加载模式设置成了android:launchMode="singleInstance"   虽然每次都只开启了同一个页面,但是每次都是从新加载的,而不是直接调到已经开启的activity,这个交互很不理想;
 
经过仔细研究log发现,通过桌面快捷方式开启应用和通过程序菜单开启应用的日志信息差了一个东西cat=[android.intent.category.LAUNCHER],通过快捷方式开启应用并没有打印出这个属性,所以就尝试在快捷方式创建的时候给他的intent添加一个属性,
 
ComponentName comp = new ComponentName(this.getPackageName(), this.getPackageName() + "." +this.getLocalClassName());    Intent intent = new Intent(Intent.ACTION_MAIN).setComponent(comp);  intent.addCategory(Intent.CATEGORY_LAUNCHER);  shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
 
其中,关键的就是intent.addCategory(Intent.CATEGORY_LAUNCHER);  完美解决了标题提到的问题!

转载地址:https://panda1234lee.blog.csdn.net/article/details/9042873 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:工厂模式(简单工厂,工厂方法,抽象工厂)
下一篇:android开发之——混淆编译

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月19日 23时30分30秒