简体   繁体   English

如何将应用程序的快捷方式添加到主屏幕

[英]How to add app's shortcut to the home screen

I know it's not documented and won't work on every device, but I see more and more apps placing their shortcuts on the home screen after they got installed. 我知道它没有记录,也不适用于所有设备,但我看到越来越多的应用程序在安装后将其快捷方式放在主屏幕上。 Found bunch of code chunks how to do it but for me they don't fit together. 发现一堆代码块如何做到但对我来说它们并不适合。 This is what I got for now. 这就是我现在所得到的。

  1. Need a permission in the manifest. 需要清单中的权限。

     <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> 
  2. Create an Intent of activity that should be called. 创建应该调用的活动的Intent。 Ex (from cgeek): Ex(来自cgeek):

     Intent shortcutIntent = new Intent(); shortcutIntent.setClassName("com.example.androidapp", "SampleIntent"); shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
  3. Create shortcut itself 创建快捷方式

     Intent addIntent = new Intent(); addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Shortcut Name"); addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(context, R.drawable.icon)); addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); context.sendBroadcast(addIntent); 

My questions is: Where this code should go to make shortcut added after .apk installed? 我的问题是:在安装.apk后,这段代码应该去哪里添加快捷方式? I tried this code in the launcher activity, it creates broken(another story) shortcut every time app starts. 我在启动器活动中尝试了此代码,每次应用启动时都会创建损坏的(另一个故事)快捷方式。

As far as I know, that's an optional feature of the Market app, not of the apps themselves. 据我所知,这是Market应用程序的可选功能,而不是应用程序本身。 By design an application does not receive a broadcast about itself being installed. 根据设计,应用程序不会收到有关自身安装的广播。 If that codes works, the soonest you can execute it is the first time the user launches the app. 如果该代码有效,则最快可以执行它,这是用户第一次启动应用程序。 That said: 那说:

Do. 做。 Not. 不。 Automatically. 自动。 Create. 创建。 App. 应用。 Shortcuts. 快捷键。

Ever. 永远。

Don't usurp the user's UI design. 不要篡夺用户的UI设计。

I agree with the currently accepted answer, that you should not do this by receiving a broadcast or at-install time. 我同意目前接受的答案,即您不应通过接收广播或安装时间来做到这一点。 Don't do anything without user interaction or permission. 没有用户交互或许可,不要做任何事情。

However, if you provide a button in your application, you would put this in the buttons OnClick handler. 但是,如果在应用程序中提供了一个按钮,则可以将其放在按钮OnClick处理程序中。 It would then add a shortcut when the user selects the "add shortcut" option. 然后,当用户选择“添加快捷方式”选项时,它将添加快捷方式。

This can be possible just add the below code to your main activity in oncreate method 这可以在oncreate方法中将以下代码添加到主活动中

        Intent HomeScreenShortCut= new Intent(getApplicationContext(),
                MainActivity.class);

        HomeScreenShortCut.setAction(Intent.ACTION_MAIN);
        HomeScreenShortCut.putExtra("duplicate", false);
        //shortcutIntent is added with addIntent
        Intent addIntent = new Intent();
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, HomeScreenShortCut);
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "AppName");
        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);

Add add this permission to your manifest 添加将此权限添加到清单

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

I used this code to create 3 shortcuts on homepage: 我使用此代码在主页上创建3个快捷方式:

Intent HomeScreenShortCut= new Intent(getApplicationContext(),
            MainActivity.class);

HomeScreenShortCut.setAction(Intent.ACTION_MAIN);
HomeScreenShortCut.putExtra("duplicate", false);
//shortcutIntent is added with addIntent
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, HomeScreenShortCut);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "AppName");
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);

There are shortcomings in the same method above. 上述相同方法存在缺点。 Take the shortcut to the Home screen. 将快捷方式带到主屏幕。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM