Android创建快捷方式
来源:互联网 发布:nga宁芙淘宝店名 编辑:程序博客网 时间:2024/05/21 05:44
为应用创建快捷方式目前有两种方法:
1. 程序启动时主动添加快捷方式到桌面------------>主动添加
2.长按桌面,弹出应用选择窗,拖动应用到桌面---------->被动添加
公用方法:
- /**
- * 返回添加到桌面快捷方式的Intent:
- * 1.给Intent指定action="com.android.launcher.INSTALL_SHORTCUT"
- * 2.给定义为Intent.EXTRA_SHORTCUT_INENT的Intent设置与安装时一致的action(必须要有)
- * 3.添加权限:com.android.launcher.permission.INSTALL_SHORTCUT
- */
- public static Intent getShortcutToDesktopIntent(Context context) {
- Intent intent = new Intent();
- intent.setClass(context, context.getClass());
- /*以下两句是为了在卸载应用的时候同时删除桌面快捷方式*/
- intent.setAction("android.intent.action.MAIN");
- intent.addCategory("android.intent.category.LAUNCHER");
- Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
- // 不允许重建
- shortcut.putExtra("duplicate", false);
- // 设置名字
- shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,context.getString(R.string.app_name));
- // 设置图标
- shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(context, R.drawable.ic_launcher));
- // 设置意图和快捷方式关联程序
- shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,intent);
- return shortcut;
- }
一、主动添加方式:
1. 在AndroidManifest.xml中添加权限:
- <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
2. 在启动Activity中发送广播:
- sendBroadcast(getShortcutToDesktopIntent(MainActivity.this));
二、被动添加方式:
1.在AndroidManifest.xml中添加权限:
- <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
2.在AndroidManifest.xml中为主Activity添加action监听:
- <!-- 如果是通过桌面长按添加快捷方式,才需要添加此配置 -->
- <intent-filter>
- <action android:name="android.intent.action.CREATE_SHORTCUT" />
- <category android:name="android.intent.category.DEFAULT" />
- </intent-filter>
3.在启动Activity中添加广播监听:
- final Intent launchIntent = getIntent();
- final String action = launchIntent.getAction();
- if (Intent.ACTION_CREATE_SHORTCUT.equals(action)) {
- Log.i(TAG, "create shortcut method one---------------- ");
- setResult(RESULT_OK, ShortcutUtils.getShortcutToDesktopIntent(MainActivity.this));
- finish();
- }
三、删除快捷方式:
1.在AndroidManifest.xml中添加权限:
- <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"/>
2.代码:
- /**
- * 删除快捷方式
- * */
- public static void deleteShortCut(Context context)
- {
- Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
- //快捷方式的名称
- shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,context.getString(R.string.app_name));
- /**删除和创建需要对应才能找到快捷方式并成功删除**/
- Intent intent = new Intent();
- intent.setClass(context, context.getClass());
- intent.setAction("android.intent.action.MAIN");
- intent.addCategory("android.intent.category.LAUNCHER");
- shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,intent);
- context.sendBroadcast(shortcut);
- }
1.添加权限:
- <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/>
2.代码:
- /**
- * 判断是否已添加快捷方式:
- * 暂时没有方法能够准确的判断到快捷方式,原因是,
- 1、不同厂商的机型他的快捷方式uri不同,我遇到过HTC的他的URI是content://com.htc.launcher.settings/favorites?notify=true
- 2、桌面不只是android自带的,可能是第三方的桌面,他们的快捷方式uri都不同
- 提供一个解决办法,创建快捷方式的时候保存到preference,或者建个文件在SD卡上,下次加载的时候判断不存在就先发删除广播,再重新创建
- * 添加权限:<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" ></uses-permission>
- */
- public static boolean hasInstallShortcut(Context context) {
- boolean hasInstall = false;
- String AUTHORITY = "com.android.launcher.settings";
- int systemversion = Build.VERSION.SDK_INT;
- Log.i("Build.VERSION.SDK==========>", systemversion + "");
- /*大于8的时候在com.android.launcher2.settings 里查询(未测试)*/
- if(systemversion >= 8){
- AUTHORITY = "com.android.launcher2.settings";
- }
- Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/favorites?notify=true");
- Cursor cursor = context.getContentResolver().query(CONTENT_URI,
- new String[] { "title" }, "title=?",
- new String[] { context.getString(R.string.app_name) }, null);
- if (cursor != null && cursor.getCount() > 0) {
- hasInstall = true;
- }
- return hasInstall;
- }
原文链接
http://blog.csdn.net/jjmm2009/article/details/37902949
0 0
- Android 创建快捷方式 删除快捷方式
- Android创建快捷方式,Phonegap创建快捷方式
- Android创建快捷方式(shortcut)
- android 代码创建快捷方式
- android创建快捷方式
- android 创建快捷方式
- Android:创建快捷方式
- android 创建桌面快捷方式
- android 创建快捷方式
- android创建任意快捷方式
- Android创建快捷方式
- android 创建快捷方式
- Android--创建快捷方式
- Android 创建桌面快捷方式
- android 创建快捷方式
- Android之创建快捷方式
- android创建桌面快捷方式
- android软件创建快捷方式
- SylixOS ARM BSP 文件结构
- 【Unity】Shader编程 基础总结
- ORACLE定期清理INACTIVE会话
- 共享微信JSSDK的access_token.json
- Windows 7 默认情况下没有 tftp 和 telnet 命令.
- Android创建快捷方式
- usr/bin/ld: cannot find 错误解决方法
- 密码找回漏洞总结
- 修改tomcat项目的图标
- NSPredicate --(谓词查询) Foundation提供的类,它指定数据被获取或者过滤的方式
- Postgresql插入数据返回自增id
- Stay Hungry, Stay Foolish 的由来 --2005斯坦福大学05年毕业演讲
- Android5.0 ToolBar
- 【我们都爱Paul Hegarty】斯坦福IOS8公开课个人笔记34 Info.plist、Capabilities