如何在不使用 Facebook SDK 的情况下提示用户创建 Facebook post?
How can I prompt user to make a Facebook post without using Facebook SDK?
我想让用户在我的应用程序中按下一个按钮,它会将他们带到 Facebook 并显示已输入的消息。以下是我为 Twitter 所做的工作。有没有我可以在不集成 Facebook SDK 的情况下为 Facebook 做类似的事情?
public void twitterTweetButtonPressed(View view)
{
String tweetUrl = String.format("https://twitter.com/intent/tweet?text=%s&url=%s", urlEncode("My tweet goes here"), urlEncode(""));
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(tweetUrl));
List<ResolveInfo> matches = getPackageManager().queryIntentActivities(intent, 0);
for (ResolveInfo info : matches)
{
if (info.activityInfo.packageName.toLowerCase().startsWith("com.twitter")) {
intent.setPackage(info.activityInfo.packageName);
}
}
startActivity(intent);
}
是的,您基本上可以使用包名 com.facebook.katana 和相同的操作 (ACTION_VIEW
) 并使用额外的 Intent.EXTRA_TEXT 设置文本。
如果安装了官方 Facebook 应用程序,此代码会直接打开它,否则会回退到在浏览器中打开 sharer.php。
String urlToShare = "";
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
// intent.putExtra(Intent.EXTRA_SUBJECT, "Foo bar"); // NB: has no effect!
intent.putExtra(Intent.EXTRA_TEXT, urlToShare);
// See if official Facebook app is found
boolean facebookAppFound = false;
List<ResolveInfo> matches = getPackageManager().queryIntentActivities(intent, 0);
for (ResolveInfo info : matches) {
if (info.activityInfo.packageName.toLowerCase().startsWith("com.facebook.katana")) {
intent.setPackage(info.activityInfo.packageName);
facebookAppFound = true;
break;
}
}
// As fallback, launch sharer.php in a browser
if (!facebookAppFound) {
String sharerUrl = "https://www.facebook.com/sharer/sharer.php?u=" + urlToShare;
intent = new Intent(Intent.ACTION_VIEW, Uri.parse(sharerUrl));
}
startActivity(intent);
希望我上面的代码对你有帮助。
如果您需要我这边的更多帮助,请告诉我。
我想让用户在我的应用程序中按下一个按钮,它会将他们带到 Facebook 并显示已输入的消息。以下是我为 Twitter 所做的工作。有没有我可以在不集成 Facebook SDK 的情况下为 Facebook 做类似的事情?
public void twitterTweetButtonPressed(View view)
{
String tweetUrl = String.format("https://twitter.com/intent/tweet?text=%s&url=%s", urlEncode("My tweet goes here"), urlEncode(""));
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(tweetUrl));
List<ResolveInfo> matches = getPackageManager().queryIntentActivities(intent, 0);
for (ResolveInfo info : matches)
{
if (info.activityInfo.packageName.toLowerCase().startsWith("com.twitter")) {
intent.setPackage(info.activityInfo.packageName);
}
}
startActivity(intent);
}
是的,您基本上可以使用包名 com.facebook.katana 和相同的操作 (ACTION_VIEW
) 并使用额外的 Intent.EXTRA_TEXT 设置文本。
如果安装了官方 Facebook 应用程序,此代码会直接打开它,否则会回退到在浏览器中打开 sharer.php。
String urlToShare = "";
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
// intent.putExtra(Intent.EXTRA_SUBJECT, "Foo bar"); // NB: has no effect!
intent.putExtra(Intent.EXTRA_TEXT, urlToShare);
// See if official Facebook app is found
boolean facebookAppFound = false;
List<ResolveInfo> matches = getPackageManager().queryIntentActivities(intent, 0);
for (ResolveInfo info : matches) {
if (info.activityInfo.packageName.toLowerCase().startsWith("com.facebook.katana")) {
intent.setPackage(info.activityInfo.packageName);
facebookAppFound = true;
break;
}
}
// As fallback, launch sharer.php in a browser
if (!facebookAppFound) {
String sharerUrl = "https://www.facebook.com/sharer/sharer.php?u=" + urlToShare;
intent = new Intent(Intent.ACTION_VIEW, Uri.parse(sharerUrl));
}
startActivity(intent);
希望我上面的代码对你有帮助。
如果您需要我这边的更多帮助,请告诉我。