当前位置:首页 > 芯闻号 > 充电吧
[导读]前面写了如何创建 删除和判断快捷方式的方法 但是后来发现判断快捷方式的方法不是总是很有效 上网查了下 发现大家的为问题都是2.2版本之后的就判断不正确了 在此 将原来的判断方法贴一下 Java代码 

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

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

Java代码 
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的如下:

Java代码 
 
       <receiver 
            android:name=".InstallShortcutReceiver" 
            android:permission="com.lp.launcher.permission.INSTALL_SHORTCUT"> 
            
        
 
         
       <receiver 
            android:name=".UninstallShortcutReceiver" 
            android:permission="com.lp.launcher.permission.UNINSTALL_SHORTCUT"> 
            
        
 
         
       <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的如下:
Java代码 
 
       <receiver 
            android:name="com.android.launcher2.InstallShortcutReceiver" 
            android:permission="com.android.launcher.permission.INSTALL_SHORTCUT"> 
            
        
 
         
       <receiver 
            android:name="com.android.launcher2.UninstallShortcutReceiver" 
            android:permission="com.android.launcher.permission.UNINSTALL_SHORTCUT"> 
            
        
 
         
       <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有了变化

Java代码 
android:authorities="com.android.launcher.settings" 

Java代码 
android:authorities="com.android.launcher2.settings" 


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

现修改代码如下:

Java代码 
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; 
    } 


本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除( 邮箱:macysun@21ic.com )。
换一批
延伸阅读
关闭