【发布时间】: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