Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 添加到Android主屏幕的快捷方式_Java_Android_Homescreen - Fatal编程技术网

Java 添加到Android主屏幕的快捷方式

Java 添加到Android主屏幕的快捷方式,java,android,homescreen,Java,Android,Homescreen,我该如何做一些类似于Telegram和许多其他应用程序的事情,这些应用程序允许您通过Telegram添加一个元素,在本例中,Telegram是一个联系人,单击它会打开联系人聊天窗口 我想做一些类似的事情,在家里添加一个元素,如果你点击它,允许你做一个特定的操作 但我必须打开一个我的外部应用程序 编辑: 单击主屏幕上的链接时必须调用的意图, str name连接元素 Intent appIntent = new Intent(Intent.ACTION_VIEW,Uri.parse("http:

我该如何做一些类似于Telegram和许多其他应用程序的事情,这些应用程序允许您通过Telegram添加一个元素,在本例中,Telegram是一个联系人,单击它会打开联系人聊天窗口

我想做一些类似的事情,在家里添加一个元素,如果你点击它,允许你做一个特定的操作

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

编辑:

单击主屏幕上的链接时必须调用的意图, str name连接元素

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:

加:


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

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

编辑:

下面是使用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)
}

我不确定我是否理解你想要实现的目标,但现在开始。 在清单中,您可以添加如下代码

<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使您可以从主屏幕启动活动。因此,只需将该过滤器添加到您希望在主屏幕上显示的活动中。

我已经阅读了您指示的代码和帖子,但我不确定如何使用它,createShortCut。我通过设置一个示例来编辑这个问题,说明当单击在主屏幕中创建的链接时必须调用的意图。我试着把你发布的代码放进去看看是否创建了链接,但什么都没有。我感谢你的帮助,但它似乎不起作用,我把你的代码从hotlin换成了java,但它不起作用,什么都没有创建。这难道不是巧合吗,这段代码是版本8用来在同一个应用程序上创建快捷方式的代码吗?我使用的设备是安卓7.0版。根据我解决的方法,快捷键应该在Thax设备上工作。解决办法是把它放在原点的柱子上。
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)
}
<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>