1、指定某些应用来分享
private void initShareIntent() {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
List<ResolveInfo> resInfo =getPackageManager().queryIntentActivities(
intent, 0);
if (!resInfo.isEmpty()) {
List<Intent> targetedShareIntents = newArrayList<Intent>();
for (ResolveInfo info : resInfo) {
Intent targeted = new Intent(Intent.ACTION_SEND);
targeted.setType("text/plain");
ActivityInfo activityInfo = info.activityInfo;
// judgments :activityInfo.packageName, activityInfo.name, etc.
if (activityInfo.packageName.contains("com.sina.weibo")
|| activityInfo.name.contains("tencent")) {
targeted.putExtra(Intent.EXTRA_TEXT, "分享内容");
targeted.setPackage(activityInfo.packageName);
targetedShareIntents.add(targeted);
}
}
Intent chooserIntent = Intent.createChooser(
targetedShareIntents.remove(0), "Select app to share");
if (chooserIntent == null) {
return;
}
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
targetedShareIntents.toArray(new Parcelable[]{}));
try {
startActivity(chooserIntent);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(this, "Can't find sharecomponent to share",
Toast.LENGTH_SHORT).show();
}
}
}
2、指定一个应用来分享
private void initShareIntent(String type) {
boolean found = false;
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("image/*");
// gets the list of intentsthat can be loaded.
List<ResolveInfo> resInfo =getPackageManager().queryIntentActivities(
share, 0);
if (!resInfo.isEmpty()) {
for (ResolveInfo info : resInfo) {
if (info.activityInfo.packageName.toLowerCase().contains(type)
|| info.activityInfo.name.toLowerCase().contains(type)) {
share.putExtra(Intent.EXTRA_SUBJECT, "subject");
share.putExtra(Intent.EXTRA_TEXT, "your text");
//share.putExtra(Intent.EXTRA_STREAM,
// Uri.fromFile(newFile(myPath))); // Optional, just
// // if you wanna
// // share an
// // image.
share.setPackage(info.activityInfo.packageName);
found = true;
break;
}
}
if (!found)
return;
startActivity(Intent.createChooser(share, "Select"));
}
}
调用方法:
private void initShareIntent(String type) {
boolean found = false;
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("image/*");
// gets the list of intentsthat can be loaded.
List<ResolveInfo> resInfo =getPackageManager().queryIntentActivities(
share, 0);
if (!resInfo.isEmpty()) {
for (ResolveInfo info : resInfo) {
if (info.activityInfo.packageName.toLowerCase().contains(type)
|| info.activityInfo.name.toLowerCase().contains(type)) {
share.putExtra(Intent.EXTRA_SUBJECT, "subject");
share.putExtra(Intent.EXTRA_TEXT, "your text");
//share.putExtra(Intent.EXTRA_STREAM,
// Uri.fromFile(newFile(myPath))); // Optional, just
// // if you wanna
// // share an
// // image.
share.setPackage(info.activityInfo.packageName);
found = true;
break;
}
}
if (!found)
return;
startActivity(Intent.createChooser(share, "Select"));
}
}