android - 在主屏幕上为特定 Activity 创建快捷方式

标签 android android-intent shortcut intentfilter

我想为我的用户提供一个选项,让他们可以创建指向应用内特定页面的快捷方式。 我在 Whatsapp 上看到过类似的用法,当你长按聊天时,你就可以为这个特定的聊天创建桌面快捷方式。

我已经尝试查找有关此 functionality 的一些文档但无法让它工作。 这是我拥有的:

不是启动器 Activity 的 Activity (包括 intent-filter)

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

list

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

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

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

*我正在我的一个 viewpager fragment 中运行代码。

最佳答案

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

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

现在在 list 中添加权限

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

现在定义

android:exported="true"

属性在

<activity> tag 

喜欢

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

这就像 whatsapp 应用聊天快捷方式。

关于android - 在主屏幕上为特定 Activity 创建快捷方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30829896/

相关文章:

Android AudioRecord 支持的采样率

Android Intent Share 使用外部图片 url

android - 对 Android 联系人应用程序中的新联系人使用react

android - 当设备不支持时,NFC权限是否会导致错误?

Ubuntu 17.10 上的安卓工作室 : The emulator process for AVD Nexus_5_API_22 was killed

shortcut - 如何在 Windows 命令行中通过 WSL 调用 Linux 命令?

bash - 查找调用 bash 脚本的桌面快捷方式的位置

java - android studio 无法在 ImageView 中显示图像

android - ActivityManager : java. lang.SecurityException : Permission Denial: starting Intent { act=android. intent.act

android - 应用程序图标存储在 Android 设备中的什么位置?