在Pin It button developer docs 的帮助下,我找到了一种以简单的 Android 意图(不使用 Pinterest SDK)分享到 Pinterest 的方法。
基本上,您只需使用Intent.ACTION_VIEW 打开这样的 URL;官方 Pinterest 应用程序好心地支持这些 URL。 (我之前对sharing to Twitter 使用了非常相似的方法。)
https://www.pinterest.com/pin/create/button/
?url=http%3A%2F%2Fwww.flickr.com%2Fphotos%2Fkentbrew%2F6851755809%2F
&media=http%3A%2F%2Ffarm8.staticflickr.com%2F7027%2F6851755809_df5b2051c9_z.jpg
&description=Next%20stop%3A%20Pinterest
为了更流畅的用户体验,set 打算直接在 Pinterest 应用程序中打开(如果已安装)。
一个完整的例子:
String shareUrl = "https://stackoverflow.com/questions/27388056/";
String mediaUrl = "http://cdn.sstatic.net/stackexchange/img/logos/so/so-logo.png";
String description = "Pinterest sharing using Android intents"
String url = String.format(
"https://www.pinterest.com/pin/create/button/?url=%s&media=%s&description=%s",
urlEncode(shareUrl), urlEncode(mediaUrl), urlEncode(description));
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
filterByPackageName(context, intent, "com.pinterest");
context.startActivity(intent);
上面用到的Util方法:
public static void filterByPackageName(Context context, Intent intent, String prefix) {
List<ResolveInfo> matches = context.getPackageManager().queryIntentActivities(intent, 0);
for (ResolveInfo info : matches) {
if (info.activityInfo.packageName.toLowerCase().startsWith(prefix)) {
intent.setPackage(info.activityInfo.packageName);
return;
}
}
}
public static String urlEncode(String s) {
try {
return URLEncoder.encode(s, "UTF-8");
}
catch (UnsupportedEncodingException e) {
Log.wtf("", "UTF-8 should always be supported", e);
return "";
}
}
这是安装了 Pinterest 应用程序的 Nexus 5 上的结果:
如果没有安装了 Pinterest 应用,通过浏览器也可以进行分享: