【问题标题】:how to automatically add application launcher icon to home screen when app is installed (android)安装应用程序时如何将应用程序启动器图标自动添加到主屏幕(android)
【发布时间】:2014-10-28 15:32:40
【问题描述】:

我创建了一个安卓应用。我想要的是当我安装我的应用程序时,它应该自动将快捷方式/启动器图标添加到主屏幕。

当我安装我的应用程序时,应该会自动创建一个启动器图标。

我试过这个:但它不起作用。

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

【问题讨论】:

  • 要在启动器屏幕上获得一个典型的图标,除了在清单中为启动器 Activity 声明通常的意图过滤器之外,您通常不需要执行任何操作&lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN"/&gt; &lt;category android:name="android.intent.category.LAUNCHER"/&gt; &lt;/intent-filter&gt;

标签: android android-launcher


【解决方案1】:

Todo 这样做你需要做反向 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);
}

在调用上述方法之前,最后创建布尔变量将其存储在共享首选项中,确保布尔变量为假,这只是为了避免多个快捷方式。 不要忘记在您的清单中添加权限&lt;uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /&gt; 希望对你有帮助

【讨论】:

    【解决方案2】:

    你需要发送一个广播:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-15
      • 1970-01-01
      • 2014-10-11
      • 1970-01-01
      • 2011-06-18
      • 2017-08-20
      相关资源
      最近更新 更多