现在的位置: 首页 > 综合 > 正文

Android Shortcut操作(快捷方式)

2013年08月23日 ⁄ 综合 ⁄ 共 4854字 ⁄ 字号 评论关闭

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");

修复此问题。

抱歉!评论已关闭.