send Sina Weibo - Android App

Android examples for App:Popular App

Description

send Sina Weibo

Demo Code


//package com.java2s;
import java.util.ArrayList;
import java.util.List;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ResolveInfo;
import android.net.Uri;

public class Main {

    public static String SINA_WEIBO_PACKAGE_NAME = "com.sina.weibo";

    private static final String SINA_URL = "http://service.weibo.com/share/share.php?url=";
    private static final String SINA_TITLE = "&pic=&title=";
    private static final String SINA_END = "&appkey=&ralateUid=";
    private static ResolveInfo resolveInfo = null;

    public static void sendSinaWeibo(Context context, String contentStr,
            String shareUrl) {//from w w  w  .j av  a2 s .c om
        boolean flag = check(context, SINA_WEIBO_PACKAGE_NAME);
        if (!flag) {
            Uri sinaWeiboToUri = Uri.parse(SINA_URL + shareUrl + SINA_TITLE
                    + contentStr + SINA_END);
            Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
                    sinaWeiboToUri);
            context.startActivity(intent);
        } else {
            shareThroughClient(context, resolveInfo, contentStr, shareUrl);
        }
    }

    private static boolean check(final Context context, String packageName) {
        boolean flag = isAppInstalled(context, packageName);
        if (flag) {
            resolveInfo = getShareTarget(context, packageName);
        }
        return flag;
    }

    private static void shareThroughClient(Context context,
            ResolveInfo _mResolveInfo, String contentStr, String shareUrl) {
        ResolveInfo launchable = _mResolveInfo;
        ActivityInfo activity = launchable.activityInfo;
        ComponentName name = new ComponentName(
                activity.applicationInfo.packageName, activity.name);
        Intent i = new Intent(Intent.ACTION_SEND);
        i.setComponent(name);
        i.setType("*/*");
        if (activity.applicationInfo.packageName.contains("tencent.WBlog")) {
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        } else {
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                    | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
            i.addCategory(Intent.CATEGORY_LAUNCHER);
        }
        i.putExtra(Intent.EXTRA_TEXT, contentStr + shareUrl);
        context.startActivity(i);
    }

    private static boolean isAppInstalled(Context context,
            String packageName) {
        try {
            context.getPackageManager().getPackageInfo(packageName, 0);
            return true;
        } catch (NameNotFoundException e) {
            return false;
        }
    }

    private static ResolveInfo getShareTarget(Context context,
            String packageName) {
        List<ResolveInfo> mApps = new ArrayList<ResolveInfo>();
        Intent intent = new Intent(Intent.ACTION_SEND, null);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.setType("text/plain");
        PackageManager pm = context.getPackageManager();
        mApps = pm.queryIntentActivities(intent,
                PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);

        for (int i = 0; i < mApps.size(); i++) {
            if (packageName.equals(mApps.get(i).activityInfo.packageName)) {
                return mApps.get(i);
            }
        }
        return null;
    }
}

Related Tutorials