Android Shortcut操作(快捷方式)

来源:互联网 发布:怎么做淘宝客推广 编辑:程序博客网 时间:2024/05/17 09:13

1添加到Shortcut选项中

[java] view plaincopy
  1. /**  
  2. * 添加到Shortcut选项中(默认桌面上长按调出)  
  3.  
  4. * 同时需要在manifest中为activity提供一个包含  
  5. * action="android.intent.action.CREATE_SHORTCUT"的intent-filter  
  6. */   
  7. private void addShortcutToOptions(){   
  8. // 创建一个默认的Intent   
  9. Intent shortcut = new Intent();   
  10.   
  11. //快捷方式的名称   
  12. shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));   
  13. //不允许重复创建   
  14. shortcut.putExtra("duplicate"false);   
  15.   
  16. //指定当前的Activity为快捷方式启动的对象: 如 com.everest.video.VideoPlayer   
  17. //注意: ComponentName的第二个参数必须加上点号(.),否则快捷方式无法启动相应程序   
  18. String appClass = this.getPackageName() + "." +this.getLocalClassName();   
  19. ComponentName comp = new ComponentName(this.getPackageName(), appClass);   
  20. shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, newIntent(Intent.ACTION_MAIN).setComponent(comp));   
  21. // 下面的方法与上面的效果是一样的,另一种构建形式而已   
  22. // Intent respondIntent = new Intent(this, this.getClass());   
  23. // shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, respondIntent);   
  24.   
  25. //快捷方式的图标   
  26. ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);   
  27. shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);   
  28.   
  29. // 发送到消息队列   
  30. setResult(RESULT_OK, shortcut);   
  31. }  

2.为程序创建桌面快捷方式

[java] view plaincopy
  1. /**  
  2. * 为程序创建桌面快捷方式  
  3.  
  4. * 同时需要在manifest中设置以下权限:  
  5. * <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />  
  6. */   
  7. private void addShortcut(){   
  8. Intent shortcut = newIntent("com.android.launcher.action.INSTALL_SHORTCUT");   
  9.   
  10. // 快捷方式的名称   
  11. shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));   
  12. // 不允许重复创建   
  13. shortcut.putExtra("duplicate"false);   
  14.   
  15. // 指定当前的Activity为快捷方式启动的对象: 如 com.everest.video.VideoPlayer   
  16. // 这里必须为Intent设置一个action,可以任意(但安装和卸载时该参数必须一致)   
  17. String action = "com.android.action.test";   
  18. Intent respondIntent = new Intent(thisthis.getClass());   
  19. respondIntent.setAction(action);   
  20. shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, respondIntent);   
  21. // 下面的方法与上面的效果是一样的,另一种构建形式而已   
  22. // 注意: ComponentName的第二个参数必须加上点号(.),否则快捷方式无法启动相应程序   
  23. // String appClass = this.getPackageName() + "." + this.getLocalClassName();   
  24. // ComponentName comp = new ComponentName(this.getPackageName(), appClass);   
  25. // shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(action).setComponent(comp));   
  26.   
  27. // 快捷方式的图标   
  28. ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);   
  29. shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);   
  30.   
  31. sendBroadcast(shortcut);   
  32. }   

3.删除程序的快捷方式

[java] view plaincopy
  1. /**  
  2. * 删除程序的快捷方式  
  3.  
  4. * 同时需要在manifest中设置以下权限:  
  5. * <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />  
  6. */   
  7. private void delShortcut() {   
  8. Intent shortcut = newIntent("com.android.launcher.action.UNINSTALL_SHORTCUT");   
  9.   
  10. // 快捷方式的名称   
  11. shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));   
  12.   
  13. // 指定当前的Activity为快捷方式启动的对象: 如 com.everest.video.VideoPlayer   
  14. // 这里必须为Intent设置一个action,可以任意(但安装和卸载时该参数必须一致)   
  15. String action = "com.android.action.test";   
  16. Intent respondIntent = new Intent(thisthis.getClass());   
  17. respondIntent.setAction(action);   
  18. shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, respondIntent);   
  19. // 下面的方法与上面的效果是一样的,另一种构建形式而已   
  20. // 注意: ComponentName的第二个参数必须加上点号(.),否则快捷方式无法启动相应程序   
  21. // String appClass = this.getPackageName() + "." + this.getLocalClassName();   
  22. // ComponentName comp = new ComponentName(this.getPackageName(), appClass);   
  23. // shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(action).setComponent(comp));   
  24.   
  25. sendBroadcast(shortcut);   
  26. }   

来自:http://www.eoeandroid.com/thread-92125-1-1.html

4.判断是否存在快捷方式

[java] view plaincopy
  1. /** 
  2.  * 判断桌面是否已添加快捷方式 
  3.  *  
  4.  * @param cx 
  5.  * @param titleName 
  6.  *            快捷方式名称 
  7.  * @return 
  8.  */  
  9. public static boolean hasShortcut(Context cx) {  
  10.     boolean result = false;  
  11.     // 获取当前应用名称  
  12.     String title = null;  
  13.     try {  
  14.         final PackageManager pm = cx.getPackageManager();  
  15.         title = pm.getApplicationLabel(  
  16.                 pm.getApplicationInfo(cx.getPackageName(),  
  17.                         PackageManager.GET_META_DATA)).toString();  
  18.     } catch (Exception e) {  
  19.     }  
  20.   
  21.     final String uriStr;  
  22.     if (android.os.Build.VERSION.SDK_INT < 8) {  
  23.         uriStr = "content://com.android.launcher.settings/favorites?notify=true";  
  24.     } else {  
  25.         uriStr = "content://com.android.launcher2.settings/favorites?notify=true";  
  26.     }  
  27.     final Uri CONTENT_URI = Uri.parse(uriStr);  
  28.     final Cursor c = cx.getContentResolver().query(CONTENT_URI, null,  
  29.             "title=?"new String[] { title }, null);  
  30.     if (c != null && c.getCount() > 0) {  
  31.         result = true;  
  32.     }  
  33.     return result;  
  34. }  

上面内容从网上找来的,不过我发现我在三星4.0的机器上判断是否存在快捷方式时总是返回false。然后看了一下系统源码,找到了一个系统用来判断是否存在快捷方式的方法。

[java] view plaincopy
  1. /** 
  2.  * Returns true if the shortcuts already exists in the database. 
  3.  * we identify a shortcut by its title and intent. 
  4.  */  
  5. static boolean shortcutExists(Context context, String title, Intent intent) {  
  6.     final ContentResolver cr = context.getContentResolver();  
  7.     Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,  
  8.         new String[] { "title""intent" }, "title=? and intent=?",  
  9.         new String[] { title, intent.toUri(0) }, null);  
  10.     boolean result = false;  
  11.     try {  
  12.         result = c.moveToFirst();  
  13.     } finally {  
  14.         c.close();  
  15.     }  
  16.     return result;  
  17. }  

[java] view plaincopy
  1. LauncherSettings.Favorites.CONTENT_URI = Uri.parse("content://com.android.launcher2.settings/favorites?notify=true")  

上方法中的intent指的是用来创建快捷方式的Intent。上面方法的大概意思就是判断数据库里是否有了一条title相同,intent转成的字符串相同的数据,有就是此快捷方式存在。

嗯,还是没有解决问题,查询这个表得到null,三星改了数据库吧!


网上的东西不可尽信,上面创建快捷方式的代码中是存在一个问题的,就是第一次登进程序,按home,再进去又会新建一个Activity而不会恢复原来的,注意:还一种情况能导致这种现象的出现,那就是给第一个Activity设置了SingleTask之类的launchMode。

在另一种形式代码中加上一句
[java] view plaincopy
  1. respondIntent.addCategory("android.intent.category.LAUNCHER");  
修复此问题。
0 0
原创粉丝点击