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

发布于 2024-11-15 07:00:39 字数 676 浏览 2 评论 0原文

我想做的是:

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

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

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

它们似乎是我问题的答案,但有人告诉我这段代码并不适用于所有设备,那就是已弃用,这就是我想做的是不可能的......

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

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

What I want to do is:

1) I'm inside an activity, there are 2 buttons. If I click the first one a shortcut is created in my home screen. The shortcut open an html page that has been previously downloaded, so I want it to use the default browser but I don't want to use internet cause I already have the page.

2)The second button create another shortcut that starts an activity. And i want to pass to the activity some extra arguments (As strings for example)...........

Are those things possible? I found some link and some similar questions like Android: Is there a programming way to create a web shortcut on home screen

They seem to be the answer to my question but someone told me that this code is not gonna work on all devices and that is deprecated and that what i want to do is not possible.......

This technique is not recommended. This is an internal implementation, not part of the Android SDK. It will not work on all home screen implementations. It may not work on all past versions of Android. It may not work in future versions of Android, as Google is not obligated to maintain internal undocumented interfaces. Please do not use this

What means internal implementation? Is that code trustable or not.....help me pls.....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

污味仙女 2024-11-22 07:00:39

示例代码使用未记录的接口(权限和意图)来安装快捷方式。正如“某人”告诉您的那样,这可能不适用于所有手机,并且可能会在未来的 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>

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

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

The example code uses undocumented interfaces (permission and intent) to install a shortcut. As "someone" told you, this may not work on all phones, and may break in future Android releases. Don't do it.

The correct way is to listen for a shortcut request from the home screen-- with an intent filter like so in your manifest:

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

Then in the activity that receives the intent, you create an intent for your shortcut and return it as the activity result.

// 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);
短叹 2024-11-22 07:00:39

我开发了以下一种方法,用于在 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);
}

不要忘记更改您的活动名称、图标资源和活动名称。允许。

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

快乐编码!

编辑:

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

addIntent.putExtra("duplicate", false);

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

I have developed one method below for creating the shortcut icon on android Homescreen [Tested on my own app]. Just call it.

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);
}

Don't forget to change your activity name, icon resource & permission.

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

Happy coding !!!

Edit:

For Duplicate Issue, First Option is to add below line in the code otherwise it creates new one every time.

addIntent.putExtra("duplicate", false);

Second Option is to First uninstall The App Shortcut Icon and then Install It Again If the First Option Didn't Work.

看透却不说透 2024-11-22 07:00:39

自 android oreo 以来,com.android.launcher.action.INSTALL_SHORTCUT 广播不再有任何效果。 LINK

如果您想支持所有 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
    }
}

在清单文件中添加权限:

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

The com.android.launcher.action.INSTALL_SHORTCUT broadcast no longer has any effect since android oreo. LINK

If you want to support all android versions, especially android 8.0 or oreo and newer, use the code below to create shortcut:

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

Add permission in manifest file:

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
ˇ宁静的妩媚 2024-11-22 07:00:39

从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);
}

它有很多限制,遗憾的是:

  1. 需要用户接受添加它。
  2. 无法在后台添加/删除。
  3. 如果目标应用程序被删除,则不会被删除。只有创造它的人。
  4. 无法根据目标应用程序的资源创建图标,除非该图标属于当前应用程序。不过,您可以通过位图来完成。

对于 Android O 之前的版本,您也可以使用 ShortcutManagerCompat 创建新的快捷方式,而没有任何这些限制。

Starting from Android O, this is the way to create a shortcut:

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);
}

It has a lot of limitations, sadly:

  1. Requires the user to accept adding it.
  2. Can't be added/removed in the background.
  3. Won't be removed if the targeted app is removed. Only the one which created it.
  4. Can't create the icon based on a resource of the targeted app, except if it's of the current app. You can do it from a bitmap though.

For pre-Android O, you can use ShortcutManagerCompat to create new shortcuts too, without any of those limitations.

稀香 2024-11-22 07:00:39

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

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();
}

I improved a little bit a solution above. Now it saves in preferences whether a shortcut was already added and doesn't add it in new launches of an app if user deleted it. This also saves a little bit time, since the code to add an existing shortcut doesn't run anymore.

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();
}
寒尘 2024-11-22 07:00:39

@Siddiq Abu Bakkar 答案有效。但为了防止每次应用程序启动时创建快捷方式,请使用共享首选项。

final String PREF_FIRST_START = "AppFirstLaunch";
 SharedPreferences settings = getSharedPreferences(PREF_FIRST_START, 0);
    if(settings.getBoolean("AppFirstLaunch", true)){

        // record the fact that the app has been started at least once
        settings.edit().putBoolean("AppFirstLaunch", false).commit();
        ShortcutIcon();

    }

@Siddiq Abu Bakkar Answer works. But in order to prevent creating shortcut every time app launches use shared Preferences.

final String PREF_FIRST_START = "AppFirstLaunch";
 SharedPreferences settings = getSharedPreferences(PREF_FIRST_START, 0);
    if(settings.getBoolean("AppFirstLaunch", true)){

        // record the fact that the app has been started at least once
        settings.edit().putBoolean("AppFirstLaunch", false).commit();
        ShortcutIcon();

    }
临走之时 2024-11-22 07:00:39

API 级别 26 起,不推荐使用 com.android.launcher.action.INSTALL_SHORTCUT。创建快捷方式的新方法是使用 ShortcutManager

它提到有 3 种快捷方式:静态、动态和固定。
根据您的要求,您可以选择创建它们。

Since API level 26, using com.android.launcher.action.INSTALL_SHORTCUT is deprecated. The new way of creating shortcuts are using ShortcutManager.

It mentions that there are 3 kinds of shortcuts, static, dynamic and pinned.
Based on your requirements, you can choose to create them.

吹梦到西洲 2024-11-22 07:00:39

要添加主屏幕快捷方式,请使用此代码。

public void addShortcutToHomeScreen(Context context) {
        if (ShortcutManagerCompat.isRequestPinShortcutSupported(context)) {
            ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(context, "#1")
                    .setIntent(new Intent(context, MainActivity.class).setAction(Intent.ACTION_MAIN)) // !!! intent's action must be set on oreo
                    .setShortLabel("Label Goes Here")
                    .setIcon(IconCompat.createWithResource(context, R.mipmap.ic_launcher_shortcut))
                    .build();
            ShortcutManagerCompat.requestPinShortcut(context, shortcutInfo, null);
        } else {
            Toast.makeText(context, R.string.no_shortcut, Toast.LENGTH_SHORT).show();
        }
    }

并且不需要额外的许可!

To add a shortcut to the home screen use this code.

public void addShortcutToHomeScreen(Context context) {
        if (ShortcutManagerCompat.isRequestPinShortcutSupported(context)) {
            ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(context, "#1")
                    .setIntent(new Intent(context, MainActivity.class).setAction(Intent.ACTION_MAIN)) // !!! intent's action must be set on oreo
                    .setShortLabel("Label Goes Here")
                    .setIcon(IconCompat.createWithResource(context, R.mipmap.ic_launcher_shortcut))
                    .build();
            ShortcutManagerCompat.requestPinShortcut(context, shortcutInfo, null);
        } else {
            Toast.makeText(context, R.string.no_shortcut, Toast.LENGTH_SHORT).show();
        }
    }

and no extra permission need !!!

埖埖迣鎅 2024-11-22 07:00:39
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);
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);
最好是你 2024-11-22 07:00:39
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
    }
}

我用过它,但有些设备的主屏幕上添加了 2 个图标。我没听懂??

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

I've used it, but some devices on the home screen adds 2 icons. I did not understand??

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文