Android Shortcut操作(快捷方式)
来源:互联网 发布:怎么做淘宝客推广 编辑:程序博客网 时间:2024/05/17 09:13
1添加到Shortcut选项中
- /**
- * 添加到Shortcut选项中(默认桌面上长按调出)
- *
- * 同时需要在manifest中为activity提供一个包含
- * action="android.intent.action.CREATE_SHORTCUT"的intent-filter
- */
- private void addShortcutToOptions(){
- // 创建一个默认的Intent
- Intent shortcut = new Intent();
- //快捷方式的名称
- shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
- //不允许重复创建
- shortcut.putExtra("duplicate", false);
- //指定当前的Activity为快捷方式启动的对象: 如 com.everest.video.VideoPlayer
- //注意: ComponentName的第二个参数必须加上点号(.),否则快捷方式无法启动相应程序
- String appClass = this.getPackageName() + "." +this.getLocalClassName();
- ComponentName comp = new ComponentName(this.getPackageName(), appClass);
- shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, newIntent(Intent.ACTION_MAIN).setComponent(comp));
- // 下面的方法与上面的效果是一样的,另一种构建形式而已
- // Intent respondIntent = new Intent(this, this.getClass());
- // shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, respondIntent);
- //快捷方式的图标
- ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);
- shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
- // 发送到消息队列
- setResult(RESULT_OK, shortcut);
- }
2.为程序创建桌面快捷方式
- /**
- * 为程序创建桌面快捷方式
- *
- * 同时需要在manifest中设置以下权限:
- * <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
- */
- private void addShortcut(){
- Intent shortcut = newIntent("com.android.launcher.action.INSTALL_SHORTCUT");
- // 快捷方式的名称
- shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
- // 不允许重复创建
- shortcut.putExtra("duplicate", false);
- // 指定当前的Activity为快捷方式启动的对象: 如 com.everest.video.VideoPlayer
- // 这里必须为Intent设置一个action,可以任意(但安装和卸载时该参数必须一致)
- String action = "com.android.action.test";
- Intent respondIntent = new Intent(this, this.getClass());
- respondIntent.setAction(action);
- shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, respondIntent);
- // 下面的方法与上面的效果是一样的,另一种构建形式而已
- // 注意: ComponentName的第二个参数必须加上点号(.),否则快捷方式无法启动相应程序
- // String appClass = this.getPackageName() + "." + this.getLocalClassName();
- // ComponentName comp = new ComponentName(this.getPackageName(), appClass);
- // shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(action).setComponent(comp));
- // 快捷方式的图标
- ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);
- shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
- sendBroadcast(shortcut);
- }
3.删除程序的快捷方式
- /**
- * 删除程序的快捷方式
- *
- * 同时需要在manifest中设置以下权限:
- * <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
- */
- private void delShortcut() {
- Intent shortcut = newIntent("com.android.launcher.action.UNINSTALL_SHORTCUT");
- // 快捷方式的名称
- shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
- // 指定当前的Activity为快捷方式启动的对象: 如 com.everest.video.VideoPlayer
- // 这里必须为Intent设置一个action,可以任意(但安装和卸载时该参数必须一致)
- String action = "com.android.action.test";
- Intent respondIntent = new Intent(this, this.getClass());
- respondIntent.setAction(action);
- shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, respondIntent);
- // 下面的方法与上面的效果是一样的,另一种构建形式而已
- // 注意: ComponentName的第二个参数必须加上点号(.),否则快捷方式无法启动相应程序
- // String appClass = this.getPackageName() + "." + this.getLocalClassName();
- // ComponentName comp = new ComponentName(this.getPackageName(), appClass);
- // shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(action).setComponent(comp));
- sendBroadcast(shortcut);
- }
来自:http://www.eoeandroid.com/thread-92125-1-1.html
4.判断是否存在快捷方式
- /**
- * 判断桌面是否已添加快捷方式
- *
- * @param cx
- * @param titleName
- * 快捷方式名称
- * @return
- */
- public static boolean hasShortcut(Context cx) {
- boolean result = false;
- // 获取当前应用名称
- String title = null;
- try {
- final PackageManager pm = cx.getPackageManager();
- title = pm.getApplicationLabel(
- pm.getApplicationInfo(cx.getPackageName(),
- PackageManager.GET_META_DATA)).toString();
- } catch (Exception e) {
- }
- final String uriStr;
- if (android.os.Build.VERSION.SDK_INT < 8) {
- uriStr = "content://com.android.launcher.settings/favorites?notify=true";
- } else {
- uriStr = "content://com.android.launcher2.settings/favorites?notify=true";
- }
- final Uri CONTENT_URI = Uri.parse(uriStr);
- final Cursor c = cx.getContentResolver().query(CONTENT_URI, null,
- "title=?", new String[] { title }, null);
- if (c != null && c.getCount() > 0) {
- result = true;
- }
- return result;
- }
上面内容从网上找来的,不过我发现我在三星4.0的机器上判断是否存在快捷方式时总是返回false。然后看了一下系统源码,找到了一个系统用来判断是否存在快捷方式的方法。
- /**
- * Returns true if the shortcuts already exists in the database.
- * we identify a shortcut by its title and intent.
- */
- static boolean shortcutExists(Context context, String title, Intent intent) {
- final ContentResolver cr = context.getContentResolver();
- Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
- new String[] { "title", "intent" }, "title=? and intent=?",
- new String[] { title, intent.toUri(0) }, null);
- boolean result = false;
- try {
- result = c.moveToFirst();
- } finally {
- c.close();
- }
- return result;
- }
- LauncherSettings.Favorites.CONTENT_URI = Uri.parse("content://com.android.launcher2.settings/favorites?notify=true")
上方法中的intent指的是用来创建快捷方式的Intent。上面方法的大概意思就是判断数据库里是否有了一条title相同,intent转成的字符串相同的数据,有就是此快捷方式存在。
嗯,还是没有解决问题,查询这个表得到null,三星改了数据库吧!
网上的东西不可尽信,上面创建快捷方式的代码中是存在一个问题的,就是第一次登进程序,按home,再进去又会新建一个Activity而不会恢复原来的,注意:还一种情况能导致这种现象的出现,那就是给第一个Activity设置了SingleTask之类的launchMode。
在另一种形式代码中加上一句修复此问题。
- respondIntent.addCategory("android.intent.category.LAUNCHER");
0 0
- Android Shortcut操作(快捷方式)
- Android Shortcut操作(快捷方式)
- 常用代码段Android Shortcut操作(快捷方式)
- Android快捷方式(ShortCut)创建以及删除
- Android创建快捷方式(shortcut)
- Android创建快捷方式-SHORTCUT
- android快捷方式shortcut 管理
- Android Shortcut操作(一)
- Android Shortcut操作(二)
- Android中快捷方式的创建和删除(ShortCut)
- 阅读笔记一(Android系统创建桌面快捷方式ShortCut)
- Shortcut 快捷方式
- Android桌面组件widget与快捷方式shortcut
- Android添加快捷方式(ShortCut)到手机桌面
- Android添加快捷方式(Shortcut)到手机桌面
- 用IShellLink解析快捷方式(ShortCut)
- 通过深度剖析Android之Launcher源码设计架构,创建HomeScreen的Shortcut(快捷方式)
- android系统中,创建任意应用的快捷方式(shortcut)
- 英语词语辨析:Instead of and In place of的区别
- Android中设置Ethernet为默认网络类型
- 关于蓝牙设计的问题
- python 加压/解压 zip(zipfile模块的使用)
- oracle官方文档之数据库用户使用oracle组件或第三方应用程序时所需的权限规定
- Android Shortcut操作(快捷方式)
- iOS开发之单例
- Honor3C入手小GET
- Linux网络编程:原始套接字的魔力【下】
- Android 启动模拟器是出现“Failed to allocate memory: 8”错误提示的原因及解决办法
- setDescendantFocusability
- MFC运行机制--笔记
- Oracle Golden Gate 系列二 -- GG 的系统需求
- CAN总线基础知识(三)