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

| 我想做的是: 1)我在一个活动中,有2个按钮。如果单击第一个,则会在主屏幕中创建一个快捷方式。快捷方式会打开一个先前已下载的“ 0”页面,因此我希望它使用默认浏览器,但由于我已经拥有该页面,所以我不想使用互联网。 2)第二个按钮创建另一个启动活动的快捷方式。我想向活动传递一些额外的参数(例如字符串)........... 那些事可能吗?我发现了一些链接和一些类似的问题,例如Android:是否有一种编程方法可以在主屏幕上创建网络快捷方式 它们似乎是我问题的答案,但是有人告诉我,该代码将无法在所有设备上使用,并且已弃用,并且我想做的事情是不可能的.......   不建议使用此技术。这是内部实现,不是Android SDK的一部分。它不适用于所有主屏幕实施。它可能不适用于所有以前的Android版本。由于Google没有义务维护内部未记录的界面,因此它可能无法在将来的Android版本中使用。请不要使用这个 什么是内部实施?该代码是否可信任.....请帮助我.....     
已邀请:
        示例代码使用未记录的界面(权限和意图)安装快捷方式。正如“某人”告诉您的那样,这可能不适用于所有手机,并且可能会在将来的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);
    
        我在下面开发了一种在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);
第二种选择是先卸载“应用程序快捷方式”图标,然后在第一种选择不起作用的情况下再次安装。     
        自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
    }
}
在清单文件中添加权限:
<uses-permission android:name=\"com.android.launcher.permission.INSTALL_SHORTCUT\"/>
    
        我在上面改进了一个解决方案。现在,它可以保存首选项中是否已添加快捷方式,如果用户删除了快捷方式,则不会在应用程序的新启动中添加快捷方式。这也节省了一些时间,因为添加现有快捷方式的代码不再运行。
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();
}
    
        从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);
            }
遗憾的是,它有很多限制: 要求用户接受添加。 无法在后台添加/删除。 您应该能够对其进行更新,但是对我而言,它无效。 如果目标应用已删除,则不会被删除。只有创建它的那个。 无法基于目标应用程序的资源创建图标,除非它是当前应用程序的资源。不过,您可以从位图进行操作。 对于Android之前的O,您也可以使用ShortcutManagerCompat创建新的快捷方式,而没有任何这些限制。     
        从
API level 26
开始,不建议使用
com.android.launcher.action.INSTALL_SHORTCUT
。创建快捷方式的新方法是使用
ShortcutManager
。 它提到了3种快捷方式:静态,动态和固定。 根据您的要求,您可以选择创建它们。     
        @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();

    }
    
        
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);
    
        
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个图标。我不明白??     

要回复问题请先登录注册