微信微博QQの簡単な実現を分かち合う(二)
NOTE:これは単なる実装であり,それぞれのSDKは使用されていない.基本的な考え方は,パッケージ名とクラス名によって直接操作される.appが名前を変更した場合、対応する変更申し込みとクラス名が必要になる可能性があります.
前に微信、QQのコードを共有して、自分で探して、前のブログにあります.次のように書きます.
前に微信、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();
}
}
}
ShareUtil:
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();
}
}
}