从Android的API 25开始,通过长按,应用可以在启动器中提供额外的快捷方式:
事实是,我发现你的应用程序可以为启动器提供这些快捷方式,但我无法知道启动器如何获取它们的列表.
由于它是一个相当新的API,并且大多数用户和开发人员甚至不使用它,我找不到有关它的更多信息,特别是因为我想搜索API使用的"另一面".
我试过阅读文档(例如,这里).我没有看到它被提及.仅提及其他应用程序的一部分,但未提及接收器应用程序(启动器).
给定应用程序的包名称,如何使用新API获取所有"应用程序快捷方式"的列表?
是否可以使用它来请求从其中一个创建固定快捷方式?
I was trying to create app shortcut at home. I did that quite well for most devices.
with this
private void setShortcutIcon() {
if (ShortcutManagerCompat.isRequestPinShortcutSupported(this)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
final ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
final ShortcutInfo pinShortcutInfo = new ShortcutInfo.Builder(this, "334")
.setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher))
.setShortLabel(getString(R.string.app_name))
.setIntent(new Intent(this, SplashActivity.class).setAction(Intent.ACTION_VIEW).putExtra("duplicate", false))
.build();
shortcutManager.requestPinShortcut(pinShortcutInfo, null);
} else {
final Intent shortcutIntent = new Intent(getApplicationContext(), SplashActivity.class);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
final Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.mipmap.ic_launcher));
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
addIntent.putExtra("duplicate", …Run Code Online (Sandbox Code Playgroud) android app-launcher android-appshortcut android-8.1-oreo android-9.0-pie
在Android 7.1中,开发人员可以创建AppShortCut.
我们可以用两种方式创建快捷方式:
ShortcutManagerAPI的动态快捷方式.那么如何ShortcutManager动态创建快捷方式呢?
在向现有应用添加静态应用快捷方式时,我遇到了一些问题.我按照https://developer.android.com/guide/topics/ui/shortcuts.html中的步骤显示了快捷方式,但是当我点击它时它不会启动活动,而是显示一个toast消息说:" 应用程序未安装 ".
以下是清单的相关部分:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mypackage">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".activities.SplashActivity"
android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.shortcuts"
android:resource="@xml/shortcuts" />
</activity>
<activity
android:name=".activities.MainActivity"
android:label="@string/title_activity_main"
android:theme="@style/AppTheme.NoActionBar" />
<activity
android:name=".activities.ListActivity"
android:label="@string/title_activity_list"
android:parentActivityName=".activities.MainActivity"
android:theme="@style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.mypackage.activities.MainActivity" />
</activity>
<activity
android:name=".activities.NewActivity"
android:label="@string/title_activity_new"
android:parentActivityName=".activities.ListActivity"
android:theme="@style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.mypackage.activities.ListActivity" />
</activity>
<application/>
</manifest>
Run Code Online (Sandbox Code Playgroud)
这是shortcuts.xml文件:
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:shortcutId="shortcut_new_alarm"
android:enabled="true"
android:icon="@mipmap/ic_launcher"
android:shortcutShortLabel="short label"
android:shortcutLongLabel="long label"
android:shortcutDisabledMessage="message">
<intent
android:action="android.intent.action.VIEW"
android:targetPackage="com.mypackage"
android:targetClass="com.mypackage.activities.NewActivity" />
<!-- If your shortcut is …Run Code Online (Sandbox Code Playgroud) 我的应用程序中有一个Activity(MainActivity),并且有一个静态快捷方式(指向TempActivity)。
由于静态快捷方式将始终具有FLAG_ACTIVITY_NEW_TASK和FLAG_ACTIVITY_CLEAR_TASK设置,因此我创建了TempActivity,这是一个不可见的Activity,即它将启动MainActivity,然后调用finish()。而且,如开发人员文档中所建议,SecondActivity 在应用程序的AndroidManifest.xml文件中具有android:taskAffinity =“”。
MainActivity具有android:launchMode =“ singleTop”
即使执行了此操作,MainActivity仍会在新任务中启动,而不是在从主屏幕启动时创建的现有任务中启动。
AndroidManifest.xml
<activity
android:name=".MainActivity"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.shortcuts"
android:resource="@xml/shortcuts" />
</activity>
<activity android:name=".TempActivity" android:taskAffinity=""></activity>
Run Code Online (Sandbox Code Playgroud)
捷径
<shortcut
android:enabled="true"
android:icon="@drawable/icon"
android:shortcutDisabledMessage="@string/app_name"
android:shortcutId="static"
android:shortcutLongLabel="@string/app_name"
android:shortcutShortLabel="@string/app_name">
<intent
android:action="custom"
android:targetClass="com.example.mobile.appshortcut.TempActivity"
android:targetPackage="com.example.mobile.appshortcut" />
</shortcut>
Run Code Online (Sandbox Code Playgroud)
TempActivity.java
public class TempActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main2);
//Intent intent = getIntent(); // From Shortcut
Intent intent = new Intent(); // For Testing
intent.setClass(this,MainActivity.class);
startActivity(intent);
finish();
}
@Override
protected void …Run Code Online (Sandbox Code Playgroud) 这是一个简短的问题:
Android Nougat 7.1为启动器提供了一项新功能,通过显示可供选择的菜单来创建应用程序快捷方式: https://developer.android.com/about/versions/nougat/android-7.1.html https:// developer .android.com /引导/主题/ UI/shortcuts.html
从我看到的,如果你使用动态的,你可以把任何你想要的东西放进去,但是静态快捷方式(那些通过XML预先确定的)有额外的东西吗?含义:例如,我可以将一个字符串放在快捷方式的意图包中吗?或者我可以只选择他们每个人会有哪些行动?
我问这个是因为我没有在那里看到它.
android android-intent android-7.1-nougat android-appshortcut
我在主屏幕中按住图标时尝试实现Android快捷方式.但是当我尝试启动它们时,我得到了一个
"App未安装"Toast
这是我的shortcuts.xml:
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:icon="@drawable/plus_black"
android:shortcutId="add_sub"
android:shortcutLongLabel="@string/shortcut_add_sub_long"
android:shortcutShortLabel="@string/shortcut_add_sub">
<intent
android:action="android.intent.action.VIEW"
android:targetClass="com.dancam.subscriptions.AddSubscription.AddSubscription"
android:targetPackage="com.dancam.subscriptions.AddSubscription" />
</shortcut>
<shortcut
android:icon="@drawable/pen"
android:shortcutId="create_sub"
android:shortcutLongLabel="@string/shortcut_create_sub_long"
android:shortcutShortLabel="@string/shortcut_create_sub">
<intent
android:action="android.intent.action.VIEW"
android:targetClass="com.dancam.subscriptions.CreateSubscription.CreateSubscription"
android:targetPackage="com.dancam.subscriptions.CreateSubscription" />
</shortcut>
</shortcuts>
Run Code Online (Sandbox Code Playgroud)
我已经看过这个问题,但我找不到合适的解决方案.
这是我的包/类树的样子:
关于如何解决这个问题的任何线索?
我的应用程序有一个静态快捷方式,我正在使用导航体系结构组件。
我正在使用以下实现,但在Android 9上不起作用。
在我的MainActivity中:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setupToolbar()
setupNavigationDrawer()
checkPermissions()
if (intent.action == MoneyBook.SHORTCUT_ACTION_ADD_BOOKENTRY) {
findNavController(R.id.nav_host_fragment).popBackStack(R.id.action_entriesFragment_to_newBookEntryFragment, false)
}
}
Run Code Online (Sandbox Code Playgroud)
shortcuts.xml:
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:enabled="true"
android:icon="@drawable/ic_shortcut_addbookentry"
android:shortcutId="shortcut_addbookentry"
android:shortcutLongLabel="@string/shortcut_addbookentry_label_short"
android:shortcutShortLabel="@string/shortcut_addbookentry_label_short">
<intent
android:action="at.guger.moneybook.action.ADD_BOOKENTRY"
android:targetClass="at.guger.moneybook.activity.MainActivity"
android:targetPackage="at.guger.moneybook.dev" />
</shortcut>
</shortcuts>
Run Code Online (Sandbox Code Playgroud)
在调试应用程序时,我注意到该findNavController(...)方法已调用,但屏幕上没有任何反应。
此外,我不会覆盖onStart(...)或onResume(...)。
有没有给定的方法来通过导航组件实现应用程序快捷方式,或者我在这里做错了什么?
android kotlin android-appshortcut android-architecture-components android-jetpack
我正在尝试对我的应用程序实施“应用程序快捷方式”,但无法使它们正常工作。我的shortcut.xml:
<Shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:shortcutId="shortcut"
android:enabled="true"
android:icon="@drawable/ic_shortcut"
android:shortcutShortLabel="@string/shortcut_label">
<intent
android:action="android.intent.action.VIEW"
android:targetPackage="com.example"
android:targetClass="com.example.package.Activity"/>
</shortcut>
Run Code Online (Sandbox Code Playgroud)
manifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example">
<application
android:name=".Application"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:resizeableActivity="false"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".package.Activity"
android:label="@string/app_name"
android:launchMode="singleTask"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/voice_search_params" />
<meta-data android:name="android.app.shortcuts"
android:resource="@xml/shortcuts" />
</activity>
Run Code Online (Sandbox Code Playgroud)
所以,我做的快捷方式与Google示例相同。我可以看到并单击快捷方式,但没有任何反应。我尝试了不同的活动,更改了活动位置,活动动作,参数,例如“ exported = true”,但没有任何变化-在日志中,我只能看到
ActivityNotFoundException:快捷方式无法启动
我的快捷方式日志和Google相机快捷方式日志之间的唯一区别是活动路径:
I / ActivityManager:开始u0 {act = android.intent.action.VIEW flg = 0x1000c000 cmp = com.example / .package.Activity}
与
I / ActivityManager:START u0 {act = android.media.action.STILL_IMAGE_CAMERA flg …