在主屏幕上创建特定活动的快捷方式

问题描述 投票:0回答:3

我想为我的用户提供一个选项来创建应用程序中特定页面的快捷方式。 我在 Whatsapp 上看到过类似的用法,当您长按聊天时,您可以创建此特定聊天的桌面快捷方式。

我尝试找到一些有关此功能的文档,但无法使其正常工作。 这是我所拥有的:

不是启动器活动的活动(包括意图过滤器)

 <activity android:name="com.my.example.pages.Topics"
    android:parentActivityName="com.my.example.pages.Apps">
        <intent-filter>
            <action android:name="android.intent.action.CREATE_SHORTCUT"/>
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
    </activity>

创建快捷方式功能

 public void createShortcut(){
        Intent shortcutIntent = new Intent("com.my.example.pages.Topics");
        Intent.ShortcutIconResource iconResource = Intent.ShortcutIconResource.fromContext(getActivity(), R.drawable.app_logo);

        // The result we are passing back from this activity
        Intent intent = new Intent();
        intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Shortcut Test");
        intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
        getActivity().setResult(getActivity().RESULT_OK, intent);
        getActivity().finish();

        Toast.makeText(getActivity(),"Shortcut created",Toast.LENGTH_SHORT).show();
    }

清单

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

我可能错过了一些东西,因为在调用函数后我得到了 Toast,但没有创建快捷方式,并且由于 finish() 方法,应用程序退出了。

更清楚地说 - 如何为非启动器活动创建快捷方式?

*我正在我的 viewpager 片段之一中运行代码。

android android-intent shortcut intentfilter
3个回答
7
投票

使用它为非启动器活动创建快捷方式。

    private void createShortcutOfApp() {

        Intent shortcutIntent = new Intent(getApplicationContext(),
            YourTargetActivity.class);
        shortcutIntent.setAction(Intent.ACTION_MAIN);

        Intent addIntent = new Intent();
        addIntent
            .putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "App shortcut name");
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
        Intent.ShortcutIconResource.fromContext(getApplicationContext(),
        R.mipmap.logo_of_your_app_shortcut));

        addIntent
            .setAction("com.android.launcher.action.INSTALL_SHORTCUT");
        addIntent.putExtra("duplicate", false);  //may it's already there so   don't duplicate
        getApplicationContext().sendBroadcast(addIntent);
    }

现在在清单中添加权限

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

现在定义

android:exported="true"

中的属性
<activity> tag 

喜欢

<activity
  android:name=".YourTargetActivity"
  android:exported="true"></activity>

这就像 Whatsapp 应用程序聊天快捷方式。


0
投票

参考链接(固定快捷方式)后,以下 kotlin 代码对我有用

fun createWebActivityShortcut(context:Context,shortcutname:String,extra1:String) {

    val shortcutManager = ContextCompat.getSystemService<ShortcutManager>(
        context,
        ShortcutManager::class.java
    )

    if (shortcutManager!!.isRequestPinShortcutSupported) {
        val pinShortcutInfoBuilder = ShortcutInfo.Builder(context, "yourapp_"+shortcutname)
        pinShortcutInfoBuilder.setShortLabel(shortcutname)
        val intent = Intent(Intent.ACTION_VIEW, null, context, YourSpecificActivity::class.java)
        intent.putExtra("extra1",extra1) //add as many extras as you like
        pinShortcutInfoBuilder.setIntent(intent)
        pinShortcutInfoBuilder.setIcon()//add your icon here
        val pinShortcutInfo = pinShortcutInfoBuilder.build()

        val pinnedShortcutCallbackIntent = shortcutManager.createShortcutResultIntent(
            pinShortcutInfo
        )

        val successCallback = PendingIntent.getBroadcast(
            context, /* request code */ 0,
            pinnedShortcutCallbackIntent, /* flags */ 0
        )

        shortcutManager.requestPinShortcut(
            pinShortcutInfo,
            successCallback.intentSender
        )
    }
}

0
投票

这也适用于 android 14 (api 34)

public void createLauncherDesktopShortcut(Intent intent, @DrawableRes int iconRes, String title) {
  intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  if (intent.getAction() == null) {
    intent.setAction(Intent.ACTION_VIEW);
  }
  ShortcutInfoCompat shortcut = new ShortcutInfoCompat.Builder(_context, Long.toString(new Random().nextLong()))
      .setIntent(intent)
      .setIcon(IconCompat.createWithResource(_context, iconRes))
      .setShortLabel(title)
      .setLongLabel(title)
      .build();
  ShortcutManagerCompat.requestPinShortcut(_context, shortcut, null);
}

这里复制

© www.soinside.com 2019 - 2024. All rights reserved.