NOTE:这只是简单的实现,没有使用各自的SDK。基本思路是通过包名和类名来直接操作的。如果app修改了名字,可能就需要对应的修改报名和类名。
之前有分享到微信,QQ的代码,自行查找,就在上一篇博客。接着写:
public class ShareToXinlangWeibo { private static final String PackageName = "com.sina.weibo"; private static final String ActivityName = "com.sina.weibo.EditActivity"; public static void share(Activity activity,String msg, ArrayList<Uri> images) { if (AppInstallHelper.isInstalled(activity, PackageName, ActivityName)) { Intent intent = new Intent(); intent.setAction(Intent.ACTION_SEND_MULTIPLE); intent.putExtra(Intent.EXTRA_TEXT, msg); intent.setType("image/jpeg"); intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, images); intent.setClassName(PackageName, ActivityName); activity.startActivity(intent); } else { Toast.makeText(activity, "您还没有安装新浪微博!", Toast.LENGTH_SHORT).show(); } } }
public class ShareToYxFriend { //易信好友 private static final String PackageName = "im.yixin"; private static final String ActivityName = "im.yixin.activity.share.ShareToSessionActivity"; //pics are OK。 text is not.... public static void share(Activity activity,String msg, ArrayList<Uri> images) { if (AppInstallHelper.isInstalled(activity, PackageName, ActivityName)) { ShareUtil shareUtil = new ShareUtil(activity, images); Intent baseIntent = shareUtil.getBaseIntent(ActivityName); // baseIntent.putExtra("summary", msg); // baseIntent.putExtra(Intent.EXTRA_TITLE, msg); // baseIntent.putExtra(Intent.EXTRA_TEXT, msg); baseIntent.putExtra(Intent.EXTRA_SUBJECT, msg); activity.startActivity(baseIntent); } else { Toast.makeText(activity, "您还没有安装易信!", Toast.LENGTH_SHORT).show(); } } }
public class ShareToYxZone { private static final String PackageName = "im.yixin"; private static final String ActivityName = "im.yixin.activity.share.ShareToSnsActivity"; //pics are OK。 text is not.... public static void share(Activity activity,String msg, ArrayList<Uri> images) { if (AppInstallHelper.isInstalled(activity, PackageName, ActivityName)) { ShareUtil shareUtil = new ShareUtil(activity, images); Intent baseIntent = shareUtil.getBaseIntent(ActivityName); // baseIntent.putExtra("summary", msg); // baseIntent.putExtra(Intent.EXTRA_TITLE, msg); baseIntent.putExtra(Intent.EXTRA_TEXT, msg); // baseIntent.putExtra(Intent.EXTRA_SUBJECT, msg); activity.startActivity(baseIntent); } else { Toast.makeText(activity, "您还没有安装易信!", Toast.LENGTH_SHORT).show(); } } }
import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.pm.ResolveInfo; import android.net.Uri; public class ShareUtil { private Context mContext; private Intent mIntent; private ArrayList<ActivityResolveInfo> mActivities = new ArrayList<ActivityResolveInfo>(); public ShareUtil(Context context, ArrayList<Uri> uris) { mContext = context; mIntent = computeSharingIntent(uris); loadActivities(); } private Intent computeSharingIntent(ArrayList<Uri> uris) { final Intent intent = new Intent(); int size = uris.size(); final String mimeType = "image/*"; if (size > 1) { intent.setAction(Intent.ACTION_SEND_MULTIPLE).setType(mimeType); intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris); } else { intent.setAction(Intent.ACTION_SEND).setType(mimeType); intent.putExtra(Intent.EXTRA_STREAM, uris.get(0)); } intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); return intent; } private void loadActivities() { if (mIntent != null) { mActivities.clear(); List<ResolveInfo> resolveInfos = mContext.getPackageManager() .queryIntentActivities(mIntent, 0); final int resolveInfoCount = resolveInfos.size(); for (int i = 0; i < resolveInfoCount; i++) { ResolveInfo resolveInfo = resolveInfos.get(i); mActivities.add(new ActivityResolveInfo(resolveInfo)); } } } public Intent getBaseIntent(String activityName) { if (mIntent == null) { return null; } for(int index = 0; index < mActivities.size(); index ++) { ActivityResolveInfo chosenActivity = mActivities.get(index); if(activityName.equals(chosenActivity.resolveInfo.activityInfo.name)) { ComponentName chosenName = new ComponentName( chosenActivity.resolveInfo.activityInfo.packageName, chosenActivity.resolveInfo.activityInfo.name); Intent choiceIntent = new Intent(mIntent); choiceIntent.setComponent(chosenName); choiceIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); return choiceIntent; } } return null; } // public void startAct(int index) { // Intent launchIntent = getBaseIntent(index); // if (launchIntent != null) { // launchIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); // mContext.startActivity(launchIntent); // } // } /** * Represents an activity. */ public final class ActivityResolveInfo implements Comparable<ActivityResolveInfo> { /** * The {@link ResolveInfo} of the activity. */ public final ResolveInfo resolveInfo; /** * Weight of the activity. Useful for sorting. */ public float weight; /** * Creates a new instance. * * @param resolveInfo * activity {@link ResolveInfo}. */ public ActivityResolveInfo(ResolveInfo resolveInfo) { this.resolveInfo = resolveInfo; } @Override public int hashCode() { return 31 + Float.floatToIntBits(weight); } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } ActivityResolveInfo other = (ActivityResolveInfo) obj; if (Float.floatToIntBits(weight) != Float .floatToIntBits(other.weight)) { return false; } return true; } public int compareTo(ActivityResolveInfo another) { return Float.floatToIntBits(another.weight) - Float.floatToIntBits(weight); } @Override public String toString() { StringBuilder builder = new StringBuilder(); builder.append("["); builder.append("resolveInfo:").append(resolveInfo.toString()); builder.append("; weight:").append(new BigDecimal(weight)); builder.append("]"); return builder.toString(); } } }