AndroidManifest.xml 應用主入口配置:html
<activity android:name="com.*.cust.contacts.MainActivity" android:exported="true" android:label="@string/app_name" android:screenOrientation="portrait" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <!-- 本身定義的action,經過本身定義的action可以應用內調用 --> <action android:name="*.intent.action.SHORTCUT" /> <category android:name="android.intent.category.LAUNCHER" /> <!-- 必須加上這個。不然沒法直接使用自定的action --> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
// 本身定義action Intent intent = new Intent("*.intent.action.SHORTCUT"); Bundle bundle = new Bundle(); // 可以依據傳遞參數運行對應操做 bundle.putString("needgoto", "*activity"); intent.putExtras(bundle); // 建立桌面快捷方式 Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); // 是否贊成反覆建立 shortcutintent.putExtra("duplicate", true); // 需要顯示的名稱 shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name_1)); // 快捷圖片 Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher); shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon); // 點擊快捷圖片,運行的程序主入口 shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent); // 發送廣播運行操做 sendBroadcast(shortcutintent);