gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:25:41 26 4
gpt4 key购买 nike

我想为我的用户提供一个选项,让他们可以创建指向应用内特定页面的快捷方式。我在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com