2010.9.15———androidデスクトップにショートカットを追加した場合、無効があるかどうかを判断します。

5435 ワード

2010.9.15———androidデスクトップにショートカットを追加した場合、無効があるかどうかを判断します。
参考:
http://maxuefeng.blog.51cto.com/1876326/528645
http://blog.163.com/ethan_518/blog/static/1779591392011614629363/
前には削除とショートカットの判断方法が書かれていましたが、ショートカットの判断方法はいつも有効ではないことが分かりました。皆さんの問題は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には変化がないことが分かります。
お問い合わせのプロバイダーが変わりました。
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;
	}