2010-07-10 140 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

相关问题