Android 創建桌面快捷方式

jopen 10年前發布 | 35K 次閱讀 Android Android開發 移動開發

Android手機中生成快捷方式有兩種情況,一種是由應用直接在桌面生成;一種是長按桌面,在彈出的快捷菜單中生成。

第一種方式是通過廣播(Broadcast)的形式向Luncher發送請求生成快捷方式的。

以下截取該廣播的注冊信息

    <!-- 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>  

由此可以看出,要在桌面上創建快捷方式就需要權限了:
android:permission="com.android.launcher.permission.INSTALL_SHORTCUT。
所以在我們的manifest.xml文件中,我們需要加入下面這段話:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>


    private void createShortcut() {  

            //創建快捷方式的Intent  
            Intent shortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");  
            //不允許重復創建  
            shortcutIntent.putExtra("duplicate", false);  
            //快捷方式名稱  
            shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.shortcut_name));  
            //快捷圖片  
            Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher_shortcut);  
            shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);  
            //點擊快捷圖片,運行的程序主入口  
            shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(getApplicationContext() , TargetActivity.class));  
            //發送廣播。OK  
            sendBroadcast(shortcutIntent);  
        }  


AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>  
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
        package="com.example.shortcuttest"  
        android:versionCode="1"  
        android:versionName="1.0" >  

        <uses-sdk  
            android:minSdkVersion="8"  
            android:targetSdkVersion="15" />  

        <!-- 聲明 創建和刪除快捷方式時聲明權限 -->  
        <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.shortcuttest.MainActivity"  
                android:label="@string/app_name" >  
                <intent-filter>  
                    <action android:name="android.intent.action.MAIN" />  

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

            <activity android:name="com.example.shortcuttest.TargetActivity">  
                <!-- 目標Activity 需要具備 LAUNCHER功能-->  
                <intent-filter>  
                    <action android:name="android.intent.action.MAIN" />  
                    <category android:name="android.intent.category.LAUNCHER" />  
                </intent-filter>  
            </activity>  

        </application>  

    </manifest>  



注意:需要成為快捷方式入口的Activity必須增加以下配置,否則點擊快捷方式的時候會報錯

<intent-filter>  
                <action android:name="android.intent.action.MAIN" />  
                <category android:name="android.intent.category.LAUNCHER" />  
            </intent-filter>  
 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!