[英]Shortcut Add to Android Home Screen
我该如何做与电报和许多其他应用类似的操作,在这种情况下,您可以通过电报添加一个元素,如果单击该元素可以打开联系人聊天窗口。
我想做这样的事情,如果您在首页上添加一个元素,则可以执行某些操作。
但是我必须打开一个我的外部应用程序。
编辑:
单击主屏幕上str名称连接元素上的链接时必须调用的Intent。
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);
}
如果我理解正确,则希望从应用程序创建的快捷方式启动应用程序。 然后,您可以执行以下操作:
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可以从主屏幕启动您的活动。 因此,只需将该过滤器添加到要放在主屏幕上的活动中即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.