嗨,我正在开发应用程序,我想使用共享意图发布带有照片的推文。首先我搜索并找到了一个适用于官方客户端的意图,我想要的是我的应用程序适用于所有客户端。
这是我的代码:
String message = "Here goes my message";
Uri screenshotUri = Uri.parse("file:///sdcard/screenshot.jpeg");
try {
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setClassName(getApplicationContext(),findTwitterClient().getPackage());
sharingIntent.putExtra(Intent.EXTRA_TEXT, message);
sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
startActivity(sharingIntent);
} catch (Exception e) {
Log.e("In Exception", "Comes here");
//This also opens a dialog that
}
我还具有以下功能来了解设备上安装了哪些客户端。
public Intent findTwitterClient() {
final String[] twitterApps = {
// package // name - nb installs (thousands)
"com.twitter.android", // official - 10 000
"com.twidroid", // twidroyd - 5 000
"com.handmark.tweetcaster", // Tweecaster - 5 000
"com.thedeck.android" /* TweetDeck - 5 000*/ };
Intent tweetIntent = new Intent();
tweetIntent.setType("text/plain");
final PackageManager packageManager = getPackageManager();
List<ResolveInfo> list = packageManager.queryIntentActivities(
tweetIntent, PackageManager.MATCH_DEFAULT_ONLY);
for (int i = 0; i < twitterApps.length; i++) {
for (ResolveInfo resolveInfo : list) {
String p = resolveInfo.activityInfo.packageName;
if (p != null && p.startsWith(twitterApps[i])) {
tweetIntent.setPackage(p);
return tweetIntent;
}
}
}
return null;
}