猿问

如何在Android中自定义共享意图?

现在我可以使用共享意图打开共享对话框


    Intent intent = new Intent(Intent.ACTION_SEND);

    intent.setType("text/plain");

    intent.putExtra(Intent.EXTRA_TEXT, link);  

    startActivity(Intent.createChooser(intent, "Share with"));

但我需要对话框不出现并直接共享给一个社交网络(例如FB或Twitter)


有什么建议怎么做?


慕莱坞森
浏览 390回答 3
3回答

小唯快跑啊

不,你不能。意向应该以这种方式工作。如果必须强制打开某个特定应用程序,请在目标应用程序支持的情况下使用明确的意图。在不知道目标应用程序的程序包名称或组件名称,数据类型或MIME类型的情况下,您不能强制特定的应用程序在通用意图上运行。

当年话下

有一种方法可以直接打开您想要的意图。您可以获得意向列表,仅打开一个。参见以下代码:private void initShareIntent(String type) {&nbsp; &nbsp; boolean found = false;&nbsp; &nbsp; Intent share = new Intent(android.content.Intent.ACTION_SEND);&nbsp; &nbsp; share.setType("image/jpeg");&nbsp; &nbsp; // gets the list of intents that can be loaded.&nbsp; &nbsp; List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(share, 0);&nbsp; &nbsp; if (!resInfo.isEmpty()){&nbsp; &nbsp; &nbsp; &nbsp; for (ResolveInfo info : resInfo) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (info.activityInfo.packageName.toLowerCase().contains(type) ||&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; info.activityInfo.name.toLowerCase().contains(type) ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; share.putExtra(Intent.EXTRA_SUBJECT,&nbsp; "subject");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; share.putExtra(Intent.EXTRA_TEXT,&nbsp; &nbsp; &nbsp;"your text");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(myPath)) ); // Optional, just if you wanna share an image.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; share.setPackage(info.activityInfo.packageName);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; found = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (!found)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; startActivity(Intent.createChooser(share, "Select"));&nbsp; &nbsp; }}如果您想打开Twitter,请执行以下操作:initShareIntent("twi");如果是facebook:initShareIntent("face");如果邮件:initShareIntent("mail"); // or "gmail"如果要显示与类型匹配的意图列表,请使用第一个机器,请参阅此文章:Android Intent for Twitter application
随时随地看视频慕课网APP

相关分类

Android
我要回答