快捷方式添加到Android主屏幕

时间:2018-10-22 19:47:37

标签: java android homescreen

我该如何做与电报和其他许多应用类似的操作,在这种情况下,您可以通过电报添加一个元素,即单击该联系人可以打开联系人聊天窗口。

我想做这样的事情,如果您单击房屋,则可以在房屋中添加一个元素,以便您进行某些操作。

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

编辑:

在主屏幕上单击链接时必须调用的意图, 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);
}

Edit2:

添加:

<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);
            }

2 个答案:

答案 0 :(得分: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)
}

答案 1 :(得分:0)

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

<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使从主屏幕启动活动成为可能。因此,只需将该过滤器添加到要放在主屏幕上的活动中即可。

相关问题