前面寫了如何創(chuàng)建 刪除和判斷快捷方式的方法 但是后來發(fā)現(xiàn)判斷快捷方式的方法不是總是很有效 上網(wǎng)查了下 發(fā)現(xiàn)大家的為問題都是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" />?
可以看出來 創(chuàng)建和刪除快捷方式的receiver沒什么變化
但是查詢的provider有了變化
Java代碼?
android:authorities="com.android.launcher.settings"?
Java代碼?
android:authorities="com.android.launcher2.settings"?
所以說我們的方法用在2.2之后的版本是無效的
現(xiàn)修改代碼如下:
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;?
??? }?