在主屏幕上创建android应用程序快捷方式

时间:2018-09-09 12:58:41

标签: android shortcut programmatically

我需要在主屏幕上(以编程方式)为我的应用添加快捷方式。 我知道应用商店默认情况下会执行此操作,但首先,该应用将不在Google应用商店中。 我进行了很多搜索,一遍又一遍地发现基本上相同的代码行,但它似乎对我不起作用。

我使用的代码: 在清单中:

<activity android:name=".MainScreenActivity" android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

在onCreate方法中,我调用了执行以下操作的函数:

    private boolean createShortcut()
{
    //create shortcut intent
    Intent shortcutIntent = new Intent(getApplicationContext(),MainScreenActivity.class);
    shortcutIntent.setAction(Intent.ACTION_MAIN);

    //create intent to add and define the shortcut
    Intent addingIntent = new Intent();
    addingIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,shortcutIntent);
    addingIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,"SenseGuard");
    addingIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(getApplicationContext(),R.drawable.peak_detection_icon));
    addingIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");

    getApplicationContext().sendBroadcast(addingIntent);
}

我尝试将“ getApplicationContext()”切换为“ this”。 我尝试在实际的平板电脑和模拟器上进行尝试,但无法正常工作。

2 个答案:

答案 0 :(得分:2)

这样做:

第1步:

更新您的manifest.xml:

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

第2步:

在您的MainActivity.java中创建 addShortcut()方法,并在其代码段中放置以下代码:

private void addShourcut(){
  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 , "Convertor");
  addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE ,
    Intent.ShortcutIconResource.fromContext(getApplicationContext() , R.mipmap.ic_launcher));
  addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
  addIntent.putExtra("duplicate" , false);
  getApplicationContext().sendBroadcast(addIntent);

}

第3步:

为您的视图设置onClickListener,以创建快捷方式:

img_pin = (ImageView) findViewById(R.id.img_pin);
img_pin.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {

    addShourcut();
    Toast.makeText(MainActivity.this, "shortcut created !", Toast.LENGTH_SHORT).show();

  }
});

这对我有用... 快乐codinngggg ...:)

答案 1 :(得分:1)

不能保证该代码有效。该广播也由ShortcutManagerCompat发送(您可能应该使用该广播,而不是手动发送广播)。

但是,这有两个问题。

  1. 不能保证您的默认启动器会收听此广播。例如,Nova Launcher默认情况下禁用此行为。其他启动器甚至可能根本没有收听该操作。

  2. 在Android Oreo(26)及更高版本上,这将无法正常运行(有关更多详细信息,请阅读我链接的方法的注释)。

您仍然可以使用此逻辑,并希望它对某些用户有效,但是请记住,许多默认启动器甚至都没有应用程序抽屉,因此添加快捷方式可能会使您的用户出现重复的图标。另外,我知道,至少对于我来说,我的主屏幕是按照我的意愿进行组织的,如果安装了一个应用程序,将其添加到主屏幕中确实会很烦人。

但是,如果您正在使用默认的AOSP启动器(或关闭分支),并且无法正常工作,请确保将其添加到清单中:

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