【问题标题】:Create shortcut for specific activity on home screen在主屏幕上为特定活动创建快捷方式
【发布时间】:2015-06-14 13:23:58
【问题描述】:

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

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

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

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

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

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

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

【问题讨论】:

  • 感谢回答的人,Whatsapp 应用程序做得非常好和顺利。作为一个用户,我觉得这个选项非常有用,甚至是必要的
  • 这似乎可行,“主题”必须是启动器活动
  • 您是否尝试了链接问题中显示的确切步骤?
  • @NanaGhartey 为什么它必须是启动器活动?您可以看到 Whatsapp 聊天的页面不是启动器活动,并且快捷方式直接指向聊天

标签: android android-intent shortcut intentfilter


【解决方案1】:

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

    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应用聊天快捷方式。

【讨论】:

  • @juvi 请查看以上答案
【解决方案2】:

参考link(pinned shortcuts)后,下面的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
        )
    }
}

【讨论】:

    猜你喜欢
    • 2011-09-14
    • 2012-03-22
    • 2014-08-12
    • 2018-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多