Android創建桌面快捷方式

Android創建桌面快捷方式

  • 首先在MainActivity的onCreate調用以下方法:
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (!hasShortCut(this)) {
            addShortCut();
        }
    }
  • hasShortCut(Context context)方法代碼:
//判斷桌面快捷方式是否存在!
    public boolean hasShortCut(Context context) {
        String url = "";
        System.out.println(getSystemVersion());
        //2.2以後的手機讀取的路徑與之前的不一樣
        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 int getSystemVersion() {
        return android.os.Build.VERSION.SDK_INT;
    }
  • *添加桌面快捷方式的代碼:
// 添加快捷方式:
    public void addShortCut() {
        // 創建快捷方式的Intent
        Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
        // 不允許重複創建
        shortcutintent.putExtra("duplicate", false);
        // 需要現實的名稱
        shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME,
                getString(R.string.app_name));
        // 快捷圖片
        Parcelable icon = Intent.ShortcutIconResource.fromContext(
                getApplicationContext(), R.drawable.ic_launcher);
        shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
        // 點擊快捷圖片,運行的程序主入口,這裏爲自己啓動自己
        shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(
                getApplicationContext(), MainActivity.class));
        // 發送廣播,通知桌面創建
        sendBroadcast(shortcutintent);
    }
  • 需要的權限以及對應Activity的配置
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.createshortcut2"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <!-- 需要權限 -->
    <!-- 讀取桌面快捷方式是否存在的權限 -->
    <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
    <uses-permission android:name="com.android.launcher.permission.WRITE_SETTINGS" />
    <!-- 添加桌面快捷方式需要的權限 -->
    <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />



    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.createshortcut2.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.CREATE_SHORTCUT" >
                </action>
            </intent-filter>
        </activity>
    </application>

</manifest>
發佈了29 篇原創文章 · 獲贊 4 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章