Android在主屏幕上创建快捷方式

Sgo*_*nks 57 android shortcut homescreen

我想做的是:

1)我在一个活动中,有2个按钮.如果我单击第一个,则会在主屏幕中创建一个快捷方式.快捷方式打开html以前下载的页面,所以我希望它使用默认浏览器,但我不想使用因特网,因为我已经有了该页面.

2)第二个按钮创建另一个启动活动的快捷方式.我想传递一些额外的参数(例如字符串)...........

那些事情可能吗?我发现了一些链接和一些类似Android的问题 :是否有一种编程方式可以在主屏幕上创建Web快捷方式

他们似乎是我的问题的答案,但有人告诉我,这个代码不会在所有设备上工作,这是不赞成的,我想做的是不可能.......

不建议使用此技术.这是一个内部实现,不是Android SDK的一部分.它不适用于所有主屏幕实现.它可能不适用于所有以前版本的Android.它可能无法在Android的未来版本中使用,因为Google没有义务维护内部未记录的界面.请不要使用它

内部实施意味着什么?该代码是否可信赖.....请帮助我.....

ant*_*oft 83

示例代码使用未记录的接口(权限和意图)来安装快捷方式.正如"有人"告诉你的那样,这可能无法在所有手机上运行,​​并且可能会在未来的Android版本中出现问题.不要这样做.

正确的方法是从主屏幕监听快捷方式请求 - 在您的清单中使用类似意图的过滤器:

<activity android:name=".ShortCutActivity" android:label="@string/shortcut_label">
  <intent-filter>
    <action android:name="android.intent.action.CREATE_SHORTCUT" />
    <category android:name="android.intent.category.DEFAULT" />
  </intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)

然后在接收意图的活动中,为快捷方式创建意图并将其作为活动结果返回.

// create shortcut if requested
ShortcutIconResource icon =
    Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);

Intent intent = new Intent();

Intent launchIntent = new Intent(this,ActivityToLaunch.class);

intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launchIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, someNickname());
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);

setResult(RESULT_OK, intent);
Run Code Online (Sandbox Code Playgroud)

  • @ user280560 - 所有应用程序都将使用权限和意图; 这只是一些权限和意图没有记录 - Android使用它们进行内部实现(因为源代码是公开的,很容易找到它们),但你不应该在第三方应用程序中 - 因为它们是没有证件,他们可能会在没有通知的情 (5认同)
  • 您可以使用此代码段检查设备是否能够处理快捷方式创建:if(getPackageManager().queryBroadcastReceivers(new Intent(INSTALL_SHORTCUT_ACTION),0).size()> 0) (5认同)
  • 没有什么是错的和邪恶的.他只是说这个界面可能会改变,然后你的应用程序将不再起作用. (2认同)

Sid*_*kar 66

我已经开发了一种方法,用于在Android主屏幕[在我自己的应用程序上测试]创建快捷方式图标.打电话吧.

private void ShortcutIcon(){

    Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class);
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    Intent addIntent = new Intent();
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Test");
    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);
}
Run Code Online (Sandbox Code Playgroud)

不要忘记更改您的活动名称,图标资源和权限.

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
Run Code Online (Sandbox Code Playgroud)

快乐的编码!

编辑:

对于重复问题,第一个选项是在代码中添加以下行,否则每次都会创建新行.

addIntent.putExtra("duplicate", false);
Run Code Online (Sandbox Code Playgroud)

第二个选项是首先卸载应用程序快捷方式图标,然后再次安装它如果第一个选项不起作用.

  • 添加`addIntent.putExtra("duplicate",false);`否则每次都会创建一个新的. (21认同)
  • 该解决方案看起来比接受的解决方案更简单.它适用于我的情况. (2认同)

Q L*_*ker 11

自从android oreo以来,com.android.launcher.action.INSTALL_SHORTCUT广播不再有任何影响.链接

如果你想支持所有Android版本,特别是android 8.0或者oreo和更新版本,请使用下面的代码创建快捷方式:

public static void addShortcutToHomeScreen(Context context)
{
    if (ShortcutManagerCompat.isRequestPinShortcutSupported(context))
    {
        ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(context, "#1")
                .setIntent(new Intent(context, YourActivity.class).setAction(Intent.ACTION_MAIN)) // !!! intent's action must be set on oreo
                .setShortLabel("Test")
                .setIcon(IconCompat.createWithResource(context, R.drawable.ic_launcher))
                .build();
        ShortcutManagerCompat.requestPinShortcut(context, shortcutInfo, null);
    }
    else
    {
        // Shortcut is not supported by your launcher
    }
}
Run Code Online (Sandbox Code Playgroud)

在清单文件中添加权限:

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
Run Code Online (Sandbox Code Playgroud)

  • 唯一的答案适用于Android 8.0+. (5认同)

wzb*_*zon 8

我在上面改进了一点解决方案.现在,它会保存首选项是否已添加快捷方式,如果用户将其删除,则不会在应用程序的新启动中添加快捷方式.这也节省了一点时间,因为添加现有快捷方式的代码不再运行.

final static public String PREFS_NAME = "PREFS_NAME";
final static private String PREF_KEY_SHORTCUT_ADDED = "PREF_KEY_SHORTCUT_ADDED";


// Creates shortcut on Android widget screen
private void createShortcutIcon(){

    // Checking if ShortCut was already added
    SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
    boolean shortCutWasAlreadyAdded = sharedPreferences.getBoolean(PREF_KEY_SHORTCUT_ADDED, false);
    if (shortCutWasAlreadyAdded) return;

    Intent shortcutIntent = new Intent(getApplicationContext(), IntroActivity.class);
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    Intent addIntent = new Intent();
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "YourAppName");
    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);

    // Remembering that ShortCut was already added
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean(PREF_KEY_SHORTCUT_ADDED, true);
    editor.commit();
}
Run Code Online (Sandbox Code Playgroud)


and*_*per 8

从Android O开始,这是创建快捷方式的方法:

            if (ShortcutManagerCompat.isRequestPinShortcutSupported(context)) {
                final ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);

                ShortcutInfo pinShortcutInfo = new ShortcutInfo.Builder(context, shortcutId)
                        .setIcon(Icon.createWithResource(context, R.mipmap.ic_launcher))
                        .setShortLabel(label)
                        .setIntent(new Intent(context, MainActivity.class).setAction(Intent.ACTION_MAIN))
                        .build();
                shortcutManager.requestPinShortcut(pinShortcutInfo, null);
            }
Run Code Online (Sandbox Code Playgroud)

遗憾的是,它有很多局限性:

  1. 要求用户接受添加它.
  2. 无法在后台添加/删除.
  3. 你应该能够更新它,但对我来说它不起作用.
  4. 如果删除目标应用,则不会删除.只有创造它的人.
  5. 无法根据目标应用的资源创建图标,除非它是当前应用的.你可以从位图做到这一点.

对于AndroidO之前的版本,您也可以使用ShortcutManagerCompat创建新的快捷方式,而不受任何限制.