2014-10-28 19 views
1

我創建了一個Android應用程序。而我想要的是當我安裝我的應用程序時,它應該自動添加快捷鍵/啓動器圖標到主屏幕。如何在安裝應用程序時自動添加應用程序啓動器圖標到主屏幕(安卓)

當我安裝我的應用程序時,一個啓動器圖標應自動創建。

我試過這個:但它不工作。

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

的可能重複[我怎樣才能把發射器的主屏幕上的應用程序圖標?(http://stackoverflow.com/questions/4854197/how -can-i-place-app-icon-on-launcher-home-screen) – 2014-11-03 17:33:15

+0

要在啓動器屏幕上顯示一個典型的圖標,除了聲明通常的意圖過濾器外,通常不需要執行任何操作**清單中的啓動器活動。 ' 2014-11-03 17:43:43

回答

1

你需要發送一個廣播:

//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); 

點擊此處瞭解詳情:Add Shortcut for android application To home screen On button click

3

藤做到這一點,你需要做反向的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); 
} 

最後在調用之前創建布爾變量將其存儲在共享首選項中Above方法確保布爾變量爲false這只是爲了避免多個快捷方式。 不要忘記添加許可,您的清單<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> 希望它可以幫助你

相關問題