首页 技术 正文
技术 2022年11月13日
0 收藏 507 点赞 3,536 浏览 3717 个字

android 微信朋友分享,朋友圈分享

包名必须写成  com.weixin

WXEntryActivity

package com.weixin.wxapi;

import android.app.Activity;

import android.os.Bundle;

import com.tencent.mm.sdk.openapi.BaseReq;

import com.tencent.mm.sdk.openapi.BaseResp;

import com.tencent.mm.sdk.openapi.IWXAPI;

import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;

import com.tencent.mm.sdk.openapi.WXAPIFactory;

public class WXEntryActivity extends Activity implements IWXAPIEventHandler {  

    // IWXAPI 是第三方app和微信通信的openapi接口  

    private IWXAPI api;  

    @Override  

    protected void onCreate(Bundle savedInstanceState) {  

        api = WXAPIFactory.createWXAPI(this, "wxf6a32cee6aa658e2", false);  

        api.handleIntent(getIntent(), this);  

        super.onCreate(savedInstanceState);  

    }  

    

    @Override  

    public void onResp(BaseResp resp) {  

        switch (resp.errCode) {  

        case BaseResp.ErrCode.ERR_OK:  

            //分享成功  

            break;  

        case BaseResp.ErrCode.ERR_USER_CANCEL:  

            //分享取消  

            break;  

        case BaseResp.ErrCode.ERR_AUTH_DENIED:  

            //分享拒绝  

            break;  

        }  

    }

    @Override

    public void onReq(BaseReq arg0) {

        // TODO Auto-generated method stub

    }  

}

MainActivity

package com.weixin;

import android.app.Activity;

import android.graphics.Bitmap;

import android.graphics.drawable.BitmapDrawable;

import android.os.Bundle;

import com.tencent.mm.sdk.openapi.IWXAPI;

import com.tencent.mm.sdk.openapi.SendMessageToWX;

import com.tencent.mm.sdk.openapi.WXAPIFactory;

import com.tencent.mm.sdk.openapi.WXMediaMessage;

import com.tencent.mm.sdk.openapi.WXWebpageObject;

public class MainActivity extends Activity {

    private IWXAPI wxApi;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        // 实例化

        wxApi = WXAPIFactory.createWXAPI(this, "wxf6a32cee6aa658e2");

        wxApi.registerApp("wxf6a32cee6aa658e2");

        // 在须要分享的地方加入代码:

        wechatShare(0);// 分享到微信好友

        // wechatShare(1);//分享到微信朋友圈

    }

    /**

     * 微信分享 (这里仅提供一个分享网页的演示样例,其他请參看官网演示样例代码)

     *

     * @param flag

     *            (0:分享到微信好友。1:分享到微信朋友圈)

     */

    private void wechatShare(int flag) {

        WXWebpageObject webpage = new WXWebpageObject();

        webpage.webpageUrl = "这里填写链接url";

        WXMediaMessage msg = new WXMediaMessage(webpage);

        msg.title = "这里填写标题";

        msg.description = "这里填写内容";

        // 这里替换一张自己project里的图片资源

        BitmapDrawable bmpDraw = (BitmapDrawable) getResources().getDrawable(

                R.drawable.share_logo);

        Bitmap thumb = bmpDraw.getBitmap();

        msg.setThumbImage(thumb);

        SendMessageToWX.Req req = new SendMessageToWX.Req();

        req.transaction = String.valueOf(System.currentTimeMillis());

        req.message = msg;

        req.scene = flag == 0 ?

SendMessageToWX.Req.WXSceneSession

                : SendMessageToWX.Req.WXSceneTimeline;

        wxApi.sendReq(req);

    }

}

配置文件须要写

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.weixin"

    android:versionCode="1"

    android:versionName="1.0" >

    <uses-sdk

        android:minSdkVersion="8"

        android:targetSdkVersion="19" />

    <application

        android:allowBackup="true"

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

        <activity

            android:name="com.weixin.MainActivity"

            android:label="@string/app_name" >

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    <activity    

        android:name=".wxapi.WXEntryActivity"  

        android:exported="true"  

        android:screenOrientation="portrait"   

        android:theme="@android:style/Theme.Translucent.NoTitleBar" />  

    </application>

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,110
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,584
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,431
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,202
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,837
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,920