安卓(Android) 怎么发微信

2019年10月19日12:11:08安卓(Android) 怎么发微信已关闭评论 207 2329字阅读7分45秒

1.简介

安卓(Android) 怎么发微信?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */


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.widget.Toast;

public class Main {
    public static String WEIXIN_PACKAGE_NAME = "com.tencent.mm";
    private static ResolveInfo resolveInfo = null;

    public static void sendWeiXin(Context context, String contentStr,
            String shareUrl) {
        boolean flag = check(context, WEIXIN_PACKAGE_NAME);
        if (!flag) {
            Toast.makeText(context, "???????", Toast.LENGTH_SHORT).show();
        } 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;
    }
}

 
继续阅读
编程十万个怎么办