簡體   English   中英

安裝應用程序后如何自動將應用程序啟動器圖標添加到主屏幕(android)

[英]how to automatically add application launcher icon to home screen when app is installed (android)

我創建了一個Android應用。 我想要的是當我安裝我的應用程序時,它應該自動將快捷方式/啟動器圖標添加到主屏幕。

當我安裝我的應用程序時,應該會自動創建啟動器圖標。

我試過了:但是沒有用。

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />

要執行此操作,您需要反向操作Android,因為主屏幕在用戶控制下,但是仍然存在方法只需在oncreate方法外創建一個方法createShortCut(),然后在覆蓋的onCreate(Bundle savedInstanceState)方法內調用它

private void createShortCut() {

     Intent shortcutIntent = new Intent(getApplicationContext(),MainActivity.class);
     shortcutIntent.setAction(Intent.ACTION_MAIN);
     Intent intent = new Intent();
     intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
     intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, R.string.app_name);
     intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher));
     intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
     getApplicationContext().sendBroadcast(intent);
}

最終創建boolean變量並將其存儲在共享首選項中,然后再調用上方方法,確保boolean變量為false,這只是為了避免多個快捷方式。 不要忘記在清單中添加權限<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />希望對您<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />幫助

您需要發送廣播:

//where this is a context (e.g. your current activity)
final Intent shortcutIntent = new Intent(this, SomeActivity.class);

final Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
// Sets the custom shortcut's title
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
// Set the custom shortcut icon
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.icon));
// add the shortcut
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
sendBroadcast(intent);

此處的更多信息: 將android應用程序的快捷方式添加到主屏幕在按鈕上單擊

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM