APP跳转微信小程序,跳转微信公众号

  • Post author:
  • Post category:小程序


一、判断手机是否安装了微信的工具类:

//判断手机是否安装了某些程序的工具类
public class IsInstallUtils {

    //判断是否安装了微信
    public static boolean isWeixinAvilible(Context context) {
        final PackageManager packageManager = context.getPackageManager();// 获取packagemanager
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equals("com.tencent.mm")) {
                    return true;
                }
            }
        }

        return false;
    }


    //判断是否安装了QQ
    public static boolean isQQClientAvailable(Context context) {
        final PackageManager packageManager = context.getPackageManager();
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equals("com.tencent.mobileqq")) {
                    return true;
                }
            }
        }
        return false;
    }

    //检测是否安装支付宝
    public static boolean isAliPayInstalled(Context context) {
        Uri uri = Uri.parse("alipays://platformapi/startApp");
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        ComponentName componentName = intent.resolveActivity(context.getPackageManager());
        return componentName != null;
    }
}

二、Android App和微信的一些交互

1.APP跳转微信公众号:

if (IsInstallUtils.isWeixinAvilible(getActivity()) == true) {
    IWXAPI api = WXAPIFactory.createWXAPI(getActivity(), "微信开放平台AppId");
    JumpToBizProfile.Req req = new JumpToBizProfile.Req();
    req.toUserName = "小程序原始id";
    req.extMsg = "";
    req.profileType = JumpToBizProfile.JUMP_TO_NORMAL_BIZ_PROFILE;
    api.sendReq(req);

} else {
    showToast("检测到手机未安装微信,请先下载微信");
}

2.APP跳转微信小程序

if (IsInstallUtils.isWeixinAvilible(getActivity())== true) {
    IWXAPI api = WXAPIFactory.createWXAPI(OrderXYDetailsActivity.this, "微信开放平台AppId");
    WXLaunchMiniProgram.Req req = new WXLaunchMiniProgram.Req();
    req.userName = "小程序原始id"; 
    req.path = "pages/index/index?tradeno=" + orderInfo.get(0).getTradeno() + "&yybh=" + orderInfo.get(0).getYybh();  //拉起小程序页面的可带参路径,不填默认拉起小程序首页
    req.miniprogramType = WXLaunchMiniProgram.Req.MINIPTOGRAM_TYPE_RELEASE;// 可选打开 开发版,体验版和正式版
    api.sendReq(req);

} else {
    showToast("检测到手机未安装微信,请先下载微信");
}

3.APP进行微信分享

public class ShareUtils {

    //分享到微信好友列表
    public static void ShareWXFriendList(final Context context, String url, String title, String description, final String HeaderImage) {
        WXWebpageObject webpage2 = new WXWebpageObject();
        webpage2.webpageUrl = url;
        final WXMediaMessage msg2 = new WXMediaMessage(webpage2);
        msg2.title = title;
        msg2.description = description;
        if (HeaderImage.equals("")) {
            Bitmap thumb2 = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher);
            Bitmap thumbBmp = Bitmap.createScaledBitmap(thumb2, 200, 200, true);
            msg2.thumbData = ImageUtils.bitmap2Bytes(thumbBmp, Bitmap.CompressFormat.PNG);
        } else {
            new AsyncTask<String, Void, Bitmap>() {
                @Override
                protected Bitmap doInBackground(String... params) {
                    return getBitMap(context,params[0]);
                }
                @Override
                protected void onPostExecute(Bitmap bitmap) {
//                    int byteCount = bitmap.getByteCount();
                    Bitmap thumbBmp = Bitmap.createScaledBitmap(bitmap, 200, 200, true);
                    msg2.thumbData = ImageUtils.bitmap2Bytes(thumbBmp, Bitmap.CompressFormat.JPEG);
                    new AsyncTask<String, Void, Bitmap>() {
                        @Override
                        protected Bitmap doInBackground(String... params) {
                            return getBitMap(context,params[0]);
                        }
                        @Override
                        protected void onPostExecute(Bitmap bitmap) {
//                    int byteCount = bitmap.getByteCount();
                            Bitmap thumbBmp = Bitmap.createScaledBitmap(bitmap, 200, 200, true);
                            msg2.thumbData = ImageUtils.bitmap2Bytes(thumbBmp, Bitmap.CompressFormat.JPEG);
                            SendMessageToWX.Req req2 = new SendMessageToWX.Req();
                            req2.transaction = "webpage" + System.currentTimeMillis();
                            req2.message = msg2;
                            req2.scene = SendMessageToWX.Req.WXSceneSession;//发送到好友界面req.scene = isTimelineCb.isChecked() ? SendMessageToWX.Req.WXSceneTimeline : SendMessageToWX.Req.WXSceneSession;
                            MyApplication.getWXapi().sendReq(req2);
                        }
                    }.execute(HeaderImage);
                }
            }.execute(HeaderImage);

        }
        SendMessageToWX.Req req2 = new SendMessageToWX.Req();
        req2.transaction = "webpage" + System.currentTimeMillis();
        req2.message = msg2;
        req2.scene = SendMessageToWX.Req.WXSceneSession;//发送到好友界面req.scene = isTimelineCb.isChecked() ? SendMessageToWX.Req.WXSceneTimeline : SendMessageToWX.Req.WXSceneSession;
        MyApplication.getWXapi().sendReq(req2);
    }

    //分享到微信朋友圈
    public static void ShareWXFriendCircle(final Context context, String url, String title, String description, final String HeaderImage) {
        WXWebpageObject webpage2 = new WXWebpageObject();
        webpage2.webpageUrl = url;
        final WXMediaMessage msg2 = new WXMediaMessage(webpage2);
        msg2.title = title;
        msg2.description = description;

        if (HeaderImage.equals("")) {
            Bitmap thumb2 = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher);
            msg2.thumbData = ImageUtils.bitmap2Bytes(thumb2, Bitmap.CompressFormat.PNG);
        } else {
            new AsyncTask<String, Void, Bitmap>() {
                @Override
                protected Bitmap doInBackground(String... params) {
                    return getBitMap(context,params[0]);
                }

                @Override
                protected void onPostExecute(Bitmap bitmap) {
//                    int byteCount = bitmap.getByteCount();
                    Bitmap thumbBmp = Bitmap.createScaledBitmap(bitmap, 200, 200, true);
                    msg2.thumbData = ImageUtils.bitmap2Bytes(thumbBmp, Bitmap.CompressFormat.JPEG);
                    new AsyncTask<String, Void, Bitmap>() {
                        @Override
                        protected Bitmap doInBackground(String... params) {
                            return getBitMap(context,params[0]);
                        }
                        @Override
                        protected void onPostExecute(Bitmap bitmap) {
//                    int byteCount = bitmap.getByteCount();
                            Bitmap thumbBmp = Bitmap.createScaledBitmap(bitmap, 200, 200, true);
                            msg2.thumbData = ImageUtils.bitmap2Bytes(thumbBmp, Bitmap.CompressFormat.JPEG);
                            SendMessageToWX.Req req2 = new SendMessageToWX.Req();
                            req2.transaction = "webpage" + System.currentTimeMillis();
                            req2.message = msg2;
                            req2.scene = SendMessageToWX.Req.WXSceneTimeline;//发送到好友界面req.scene = isTimelineCb.isChecked() ? SendMessageToWX.Req.WXSceneTimeline : SendMessageToWX.Req.WXSceneSession;
                            MyApplication.getWXapi().sendReq(req2);
                        }
                    }.execute(HeaderImage);
                }
            }.execute(HeaderImage);
        }

        SendMessageToWX.Req req2 = new SendMessageToWX.Req();
        req2.transaction = "webpage" + System.currentTimeMillis();
        req2.message = msg2;
        req2.scene = SendMessageToWX.Req.WXSceneTimeline;//发送到好友界面req.scene = isTimelineCb.isChecked() ? SendMessageToWX.Req.WXSceneTimeline : SendMessageToWX.Req.WXSceneSession;
        MyApplication.getWXapi().sendReq(req2);
    }

    public static Bitmap getBitMap(Context context,String ImageUrl) {

        Bitmap bitmap = null;
        try {
//            bitmap = Glide.with(context).load(ImageUrl).asBitmap().centerCrop().into(128,128).get();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bitmap;
    }
}



版权声明:本文为github_39118247原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。