从 AS 3.5 更新到 Android Studio 4 后,作为 Mac 用户,我发现所有默认快捷方式cmd+shift+F都无法正常工作,即不再打开search everywhere dialog?
android shortcut keymaps android-shortcut android-studio-4.0
是否可以为多种口味定义静态快捷方式而无需复制shortcuts.xml?我有两种口味:
该shortcuts.xml看起来是这样的:
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:enabled="true"
android:icon="@drawable/ic_shortcut_add_photo"
android:shortcutId="new_photo"
android:shortcutLongLabel="@string/new_photo"
android:shortcutShortLabel="@string/new_photo">
<intent
android:action="android.intent.action.VIEW"
android:targetClass="com.test.MainActivity"
android:targetPackage="com.test"/>
</shortcut>
Run Code Online (Sandbox Code Playgroud)
问题是intent中的包名称不能引用字符串资源,必须在xml中进行硬编码.
为了还为自由的味道,我必须提供快捷方式复制的shortcuts.xml并更改targetPackage到com.test.free这是一个坏的解决方案.
我想我已经尝试了我在互联网上找到的所有解决方案,但没有人工作 - 没有力量关闭,但桌面上没有任何内容.
现在,我有这个:
private void createShortcutOnDesktop(Application app) {
Intent shortcutIntent = new Intent();
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, app.getIntentShortcut());
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, app.getName());
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(context, R.drawable.home_button));
shortcutIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
this.sendBroadcast(shortcutIntent);
finish();
}
Run Code Online (Sandbox Code Playgroud)
的app.getIntentShortcut():
public Intent getIntentShortcut() {
Intent i = new Intent();
i.setClassName(packageName, name);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
return i;
}
Run Code Online (Sandbox Code Playgroud)
并在AndroidManifest.xml文件中:
<permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
Run Code Online (Sandbox Code Playgroud)
我错过了什么?谢谢.
编辑Java文件时,如果我在行的末尾并输入Enter键,它会正确地缩短下一行缩进4列,以匹配上面的行.这是确定的,但它与缩进标签,没有空格,即使在File>Settings>Editor>Java>Tabs和缩进的复选框使用制表符被取消选中,一直都是.
如果我只是键入tab键,我也会得到一个标签,而不是空格.
我从不想使用制表符; 我总想用空格.我在Eclipse或Microsoft Visual Studio中没有这个问题,因此它不是我的PC的某些系统设置.Android Studio中是否还有其他设置?
编辑: 我尝试了Omar Al Halabi(下面)制作的建议并且它没有工作但是在测试时我发现了一些有趣的东西:这个选项卡问题似乎主要适用于行的开头,即如果我输入Enter在上一行中,它会立即使用实际制表符而非空格切换到下一行的第一个缩进位置.另一方面,如果我在现有行上键入一些文本,一旦我输入了一个小文本,如果我点击了tab,它就会使用空格进行选项卡.
android code-formatting android-studio android-shortcut android-components
我有一个带有上下文菜单的Non-Launcher活动.该菜单包含一个选项,可将活动添加到android主屏幕作为快捷方式.
我使用以下代码来创建快捷方式.
private void ShortcutIcon(){
Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Test");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher));
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);
}
Run Code Online (Sandbox Code Playgroud)
正确设置必要的权限和意图过滤器.当我运行此代码时,快捷方式已成功创建.在快捷方式单击时,活动将按预期打开.
但是,我的活动显示了一些动态数据.为此,我需要将一个小字符串变量传递给活动.
我之前尝试使用此代码setAction(就像您将额外数据传递给正常意图启动活动一样)
addIntent.putExtra("key_primarykey", value_i_want_to_pass);
Run Code Online (Sandbox Code Playgroud)
但是,当用户点击活动内部的快捷方式时,value_i_want_to_pass会显示Null.
一些应用程序Whatsapp允许完全相同.您可以保存聊天的快捷方式.还有一些dialer apps允许添加联系人作为快捷方式,以便当您点击快捷方式时,会自动启动语音呼叫.
我想知道如何将一些数据从我的快捷方式传递给我的活动.
在使用 Shortcut.xml 实现静态快捷方式时,我想根据我的意图传递一些捆绑包附加内容。
启动应用程序后,需要传递额外的内容来决定目标类中的一些功能。
是否可以访问额外内容?如何以及在哪里访问它?
任何线索将不胜感激
我的应用有minSdk = 21和targetSdk = 26
我想设置应用程序快捷方式
我已将<meta-data>标记添加到应用启动时启动的第一个活动.
<activity android:name=".SignInActivity"
android:theme="@style/SubscriptionsTheme.NoActionBar">
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.MAIN"/>
</intent-filter>
<meta-data android:name="android.app.shortcuts" android:resource="@xml/shortcuts"/>
</activity>
Run Code Online (Sandbox Code Playgroud)
然后我创建了xml-v25目录,并在其中包含此shortcuts.xml文件:
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android" >
<shortcut
android:shortcutId="test"
android:icon="@drawable/plus"
android:shortcutShortLabel="test"
android:shortcutLongLabel="long test" >
<intent
android:action="android.intent.action.VIEW"
android:targetPackage="com.xxxxx.xxxxx.xxxxx"
android:targetClass="com.xxxxx.xxxxx.xxxxxx.main" />
</shortcut>
</shortcuts>
Run Code Online (Sandbox Code Playgroud)
当我尝试构建应用程序时,我收到以下错误:
错误:错误:'long test'与属性android:shortcutLongLabel(attr)引用不兼容.错误:错误:'test'与属性android:shortcutShortLabel(attr)引用不兼容.错误:'long test'与属性android:shortcutLongLabel(attr)引用不兼容.
我正在尝试一次添加两个快捷方式。但是 android 8 显示一个对话框并请求用户允许添加快捷方式,并且其中两个对话框无法同时显示。
因此,我需要一种方法,可以在第一个对话框关闭时获得回调或广播,以便之后添加另一个快捷方式。目前,当用户允许请求时,我能够获得广播。但我想知道用户是否取消了对话框。
我正在使用以下代码:
@RequiresApi(api = Build.VERSION_CODES.O)
private static void addShortcutNew(Context context, Intent shortcutIntent, String label, int iconRes, int color) {
ShortcutManager manager = context.getSystemService(ShortcutManager.class);
if (manager != null && manager.isRequestPinShortcutSupported()) {
ShortcutInfo shortcutInfo = new ShortcutInfo.Builder(context, label)
.setIcon(prepareIcon(context, iconRes, color)) // prepareIcon is my own method which returns an Icon.
.setIntent(shortcutIntent)
.setShortLabel(label)
.setLongLabel(label)
.build();
context.registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Broadcast", Toast.LENGTH_SHORT).show();
context.unregisterReceiver(this);
}
}, new IntentFilter("test_action"));
Intent …Run Code Online (Sandbox Code Playgroud) java android android-broadcast android-shortcut android-shortcutmanager
如何在Android中的应用程序快捷方式图标中删除徽章?当我以编程方式创建应用程序快捷方式时,连同为快捷方式指定的图标一样,应用程序图标位于图标的右下角。我不要那个徽章
这是我使用的代码
public static void addShortcutToHomeScreen(Context context)
{
if (ShortcutManagerCompat.isRequestPinShortcutSupported(context))
{
ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(context, "#1")
.setIntent(new Intent(context, Splash.class).setAction(Intent.ACTION_MAIN)) // !!! intent's action must be set on oreo
.setShortLabel("Test")
.setIcon(IconCompat.createWithResource(context, R.drawable.logo))
.build();
ShortcutManagerCompat.requestPinShortcut(context, shortcutInfo, null);
}
else
{
// Shortcut is not supported by your launcher
}
}
Run Code Online (Sandbox Code Playgroud) 从应用商店下载应用时,默认情况下,iOS 在快捷方式选项中提供“共享应用名称”选项。
请参考下图:
当单击它时,它将在电话菜单屏幕本身中打开共享意图,而无需重定向到应用程序,用户可以在其中共享应用程序。
我想在android中实现它。
到目前为止,这是我尝试过的:
<shortcut
android:shortcutId="share_app"
android:enabled="true"
android:icon="@drawable/ic_cart_active"
android:shortcutShortLabel="@string/shortcut_share"
android:shortcutLongLabel="@string/shortcut_share">
<intent
android:action="android.intent.action.send"
android:targetPackage="com.example.android.internal"
android:targetClass="" />
</shortcut>
Run Code Online (Sandbox Code Playgroud)
但这是行不通的,因为我无法理解,这里的targetClass应该是什么 。
编辑: Nilesh的答案几乎奏效,但是我现在面临的唯一问题是,每当我从快捷方式单击共享按钮时,启动器活动就会显示一秒钟,然后显示共享意图选择器现在,当我按下显示共享选项的主屏幕按钮时,该应用将进入后台。这不应该发生。任何想法如何避免这种情况。