`

2011.09.15 ——— android 桌面添加快捷方式之判断是否存在无效

阅读更多
2011.09.15 ——— android 桌面添加快捷方式之判断是否存在无效

参考:http://maxuefeng.blog.51cto.com/1876326/528645
http://blog.163.com/ethan_518/blog/static/177959139201161934629363/

前面写了如何创建 删除和判断快捷方式的方法 但是后来发现判断快捷方式的方法不是总是很有效 上网查了下 发现大家的为问题都是2.2版本之后的就判断不正确了

在此 将原来的判断方法贴一下

private boolean hasShortCut() {
        ContentResolver resolver = getContentResolver();
        Cursor cursor = resolver.query(Uri.parse("content://com.android.launcher.settings/favorites?notify=true"), null, "title=?",
                        new String[] {getString(R.string.app_name)}, null);

        if (cursor != null && cursor.moveToFirst()) {
                cursor.close();
                return true;
        }

        return false;
	}



后来 我看了一下2.2版本和1.5版本的launcher中的清单文件

1.5的如下:

<!-- Intent received used to install shortcuts from other applications -->
        <receiver
            android:name=".InstallShortcutReceiver"
            android:permission="com.lp.launcher.permission.INSTALL_SHORTCUT">
            <intent-filter>
                <action android:name="com.lp.launcher.action.INSTALL_SHORTCUT" />
            </intent-filter>
        </receiver>

        <!-- Intent received used to uninstall shortcuts from other applications -->
        <receiver
            android:name=".UninstallShortcutReceiver"
            android:permission="com.lp.launcher.permission.UNINSTALL_SHORTCUT">
            <intent-filter>
                <action android:name="com.lp.launcher.action.UNINSTALL_SHORTCUT" />
            </intent-filter>
        </receiver>

        <!-- The settings provider contains Home's data, like the workspace favorites -->
        <provider
            android:name="LauncherProvider"
            android:authorities="com.lp.launcher.settings"
            android:writePermission="com.lp.launcher.permission.WRITE_SETTINGS"
            android:readPermission="com.lp.launcher.permission.READ_SETTINGS" />


2.2的如下:
<!-- Intent received used to install shortcuts from other applications -->
        <receiver
            android:name="com.android.launcher2.InstallShortcutReceiver"
            android:permission="com.android.launcher.permission.INSTALL_SHORTCUT">
            <intent-filter>
                <action android:name="com.android.launcher.action.INSTALL_SHORTCUT" />
            </intent-filter>
        </receiver>

        <!-- Intent received used to uninstall shortcuts from other applications -->
        <receiver
            android:name="com.android.launcher2.UninstallShortcutReceiver"
            android:permission="com.android.launcher.permission.UNINSTALL_SHORTCUT">
            <intent-filter>
                <action android:name="com.android.launcher.action.UNINSTALL_SHORTCUT" />
            </intent-filter>
        </receiver>

        <!-- The settings provider contains Home's data, like the workspace favorites -->
        <provider
            android:name="com.android.launcher2.LauncherProvider"
            android:authorities="com.android.launcher2.settings"
            android:writePermission="com.android.launcher.permission.WRITE_SETTINGS"
            android:readPermission="com.android.launcher.permission.READ_SETTINGS" />


可以看出来 创建和删除快捷方式的receiver没什么变化
但是查询的provider有了变化

android:authorities="com.android.launcher.settings"

android:authorities="com.android.launcher2.settings"


所以说我们的方法用在2.2之后的版本是无效的

现修改代码如下:

public static boolean hasShortCut(Context context) {
		String url = "";
		System.out.println(getSystemVersion());
		if(getSystemVersion() < 8){
			url = "content://com.android.launcher.settings/favorites?notify=true";
		}else{
			url = "content://com.android.launcher2.settings/favorites?notify=true";
		}
        ContentResolver resolver = context.getContentResolver();
        Cursor cursor = resolver.query(Uri.parse(url), null, "title=?",
                        new String[] {context.getString(R.string.app_name)}, null);

        if (cursor != null && cursor.moveToFirst()) {
                cursor.close();
                return true;
        }

        return false;
	}
private static int getSystemVersion(){
		return android.os.Build.VERSION.SDK_INT;
	}



分享到:
评论
6 楼 luitao 2013-01-25  
这个不行啊,,,,2.3,4.0 都不行啊,,,,我这测试的,,,真的
5 楼 1846396994 2012-04-21  
总结的关于添加和删除及判断是否存在快捷方式,Android应用添加(创建)和删除及判断是否存在桌面快捷方式
4 楼 lipeng88213 2011-11-17  
nos19880918 写道
多谢分享经验。

呵呵 彼此帮助
3 楼 nos19880918 2011-11-15  
多谢分享经验。
2 楼 lipeng88213 2011-09-21  
莫言_MoYan 写道
非常的感谢,救me于水火之中啊!顶!

呵呵 有用就好
1 楼 莫言_MoYan 2011-09-21  
非常的感谢,救me于水火之中啊!顶!

相关推荐

Global site tag (gtag.js) - Google Analytics