【问题标题】:Shortcut Add to Android Home Screen快捷方式添加到 Android 主屏幕
【发布时间】:2018-10-22 19:47:36
【问题描述】:

我怎样才能做类似于 Telegram 和许多其他应用程序的操作,在这种情况下,您可以通过 Telegram 添加一个元素,如果点击它会打开联系人聊天窗口的联系人。

我想做这样的事情,给home添加一个元素,如果你点击它,可以让你做某种操作。

但我必须打开我的外部应用程序。

编辑:

点击主屏幕上的链接时必须调用的意图, str 名称连接元素。

Intent appIntent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://instagram.com/_u/"+str));
appIntent.setPackage("com.instagram.android");
Intent webIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://instagram.com/"+str));
 try {
     startActivity(appIntent);
} catch (ActivityNotFoundException ex) {
    startActivity(webIntent);
}

编辑2:

添加:

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

代码:

if (ShortcutManagerCompat.isRequestPinShortcutSupported(getBaseContext())) {
Intent instagramIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://instagram.com/_u/" + str));
instagramIntent.setPackage("com.instagram.android");
Bitmap bmp = getCroppedBitmap(bitmap);
final IconCompat icon = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) ? IconCompat.createWithAdaptiveBitmap(bmp) : IconCompat.createWithBitmap(bmp);
final ShortcutInfoCompat shortcut = new ShortcutInfoCompat.Builder(getBaseContext(), UUID.randomUUID().toString())
                        .setShortLabel(str)
                        .setLongLabel(str)
                        .setIcon(icon)
                        .setIntent(instagramIntent)
                        .build();
ShortcutManagerCompat.requestPinShortcut(getBaseContext(), shortcut, null);
            }

【问题讨论】:

标签: java android homescreen


【解决方案1】:

如果我理解正确,您想从您的应用创建的快捷方式启动应用。然后你可以做这样的事情:

public void createShortCut{
    Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
    shortcutintent.putExtra("duplicate", false);
    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.shortcutname));
    Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext, R.drawable.icon);
    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent("com.whatsapp"));
    sendBroadcast(shortcutintent);
}

查看this

编辑:

这是使用 ShortcutManagerCompat 的最佳方法:

fun createShortCut(){
    val str= ""
    val uri = Uri.parse("http://instagram.com/_u/"+str)
    val instagramIntent = Intent(Intent.ACTION_VIEW,uri)
    instagramIntent.setPackage("com.instagram.android")
    val icon = IconCompat.createWithResource(this,R.drawable.ic_launcher_background)
    val pinShortcutInfo = ShortcutInfoCompat.Builder(this, "shortcutID")
                .setIcon(icon)
                .setShortLabel("MyShortcut")
                .setIntent(instagramIntent)
                .build()
    ShortcutManagerCompat.requestPinShortcut(this,pinShortcutInfo,null)
}

【讨论】:

  • 我已经阅读了您指出的代码和帖子,但我不确定如何使用它,createShortCut。我通过设置单击主屏幕中创建的链接时必须调用的意图类型的示例来编辑问题。我试着把你发布的代码放上去看看有没有创建链接,但是什么都没有。
  • 感谢您的帮助,但它似乎不起作用,我将您的代码从 hotlin 兑换为 java,但它不起作用,没有创建任何内容。此代码是版本 8 用于在同一应用程序上创建快捷方式的代码,这不是偶然的吗?我使用的设备是android 7.0版。
  • 根据this,方法 requestPinShortcut 应该在设备
  • 谢谢,我解决了。解决办法把它放在原点的帖子里。
【解决方案2】:

不确定我是否理解您想要完成的任务,但可以。 在您的清单中,您可以添加这样的代码

<activity
    android:name=".activityName"
    android:screenOrientation="sensorLandscape">
        <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

intent-filter action.Main 使您可以从主屏幕启动您的活动。因此,只需将该过滤器添加到您要放在主屏幕上的活动中即可。

【讨论】:

    猜你喜欢
    • 2013-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-10
    • 2011-09-14
    相关资源
    最近更新 更多