2010-07-10 142 views
12

您好我是這個android編程的新手。如何在android中以編程方式添加應用程序快捷方式

我需要將我的應用程序添加到主屏幕作爲快捷編程。

請給出這個想法。如果可能的話,請告訴我如何管理現有的快捷方式(刪除和添加一些更多的快捷鍵)

+0

我不認爲這些API公開該功能。默認情況下,您會在上拉對話框中獲得應用程序圖標。 – 2010-07-10 04:28:01

回答

9

我讀了一篇文章,它可以幫助你在主屏幕上添加程序快捷方式的應用。

您可以參閱 example

您也可以參考快捷here相關計算器問題。

+0

可能有其他鏈接討論它,例如:http://code.google.com/p/apps-for-android/source/browse/#git%2FAnyCut和https:// groups。 google.com/forum/?fromgroups=#!topic/android-developers/B9n6PjtTKic – 2012-09-08 08:36:27

5

調用此方法可在第一屏的onCreate()方法。此外,還要確保 檢查應用程序使用SharedPreferences運行第一次像我 那樣:

private void addShortcut() { 
    //Adding shortcut for MainActivity on Home screen 
    Intent shortcutIntent = new Intent(getApplicationContext(),MainActivity.class); 

    shortcutIntent.setAction(Intent.ACTION_MAIN); 

    Intent addIntent = new Intent(); 
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); 
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, this.getResources().getString(R.string.app_name)); 
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(getApplicationContext(), 
         R.drawable.ic_launcher)); 

    addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); 
    getApplicationContext().sendBroadcast(addIntent); 
} 

    // TO check app is installed first time. 
    SharedPreferences prefs = getSharedPreferences("ShortCutPrefs", MODE_PRIVATE); 
    if(!prefs.getBoolean("isFirstTime", false)){ 
     addShortcut(); 
     SharedPreferences.Editor editor = prefs.edit(); 
     editor.putBoolean("isFirstTime", true); 
     editor.commit(); 
    } 
4

我花了相當多的時間從計算器嘗試不同的解決方案,但其中大部分都是沒用的,因爲他們正在開始活動的新實例。我需要的快捷方式與應用列表中的完全相同,或者Google Play自動安裝的快捷方式(啓動活動或將已啓動的活動放在前面)。

@Override 
    public void onCreate(Bundle savedInstanceState) { 
     //Save the flag to SharedPreferences to prevent duplicated shortcuts 
     if (!settings.isShortcutAdded()) { 
      addShortcut(); 
      settings.setShortcutAdded(true); 
     } 
    } 

    private void addShortcut() { 
     Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class); 
     shortcutIntent.setAction(Intent.ACTION_MAIN); 
     shortcutIntent.addCategory(Intent.CATEGORY_LAUNCHER); 
     int flags = Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT; 
     shortcutIntent.addFlags(flags); 

     Intent addIntent = new Intent(); 
     addIntent.putExtra("duplicate", false); 
     addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); 
     addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getResources().getString(R.string.app_name)); 
     addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource 
       .fromContext(getApplicationContext(), R.drawable.ic_launcher)); 
     addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); 
     getApplicationContext().sendBroadcast(addIntent); 
    } 

而且不要忘記更新您的清單:

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

我們可以通過javascript來做這件事嗎?螞蟻想法? – 2014-10-29 12:36:49

相關問題