集成微信登录和分享真的很简单

文末有Demo.
准备工作

这里 注册微信开放平台的账号,
下载 签名生成工具 用来 获取 应用签名。
并不要下载sdk,

再去友盟
注册一个账号,
创建一个应用,获取友盟的Appkey.
下载分享的sdk. 地址
再去根据下面链接添加好下载的jar包和资源文件,
友盟文档
以及在manifest文件中添加以下内容

   <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />  
   <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />  
   <uses-permission android:name="android.permission.READ_PHONE_STATE" />  
   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  
   <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />  
  <uses-permission android:name="android.permission.INTERNET" />  
   <uses-permission android:name="android.permission.READ_LOGS" />  
   <uses-permission android:name="android.permission.CALL_PHONE" />  
   <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />  
   <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />  
   <uses-permission android:name="android.permission.GET_TASKS" />  
   <uses-permission android:name="android.permission.SET_DEBUG_APP" />  
   <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />  
   <uses-permission android:name="android.permission.GET_ACCOUNTS" />  
   <uses-permission android:name="android.permission.USE_CREDENTIALS" />  
   <uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />  

    <activity  
         android:name=".wxapi.WXEntryActivity"  
         android:configChanges="keyboardHidden|orientation|screenSize"  
         android:exported="true"  
         android:screenOrientation="portrait"  
         android:theme="@android:style/Theme.Translucent.NoTitleBar" />  

     <meta-data  
         android:name="UMENG_APPKEY"  
         android:value="561cae6ae0f55abd990035bf" />//修改为自己应用的umengappkey  

     <activity  
         android:name="com.umeng.socialize.editorpage.ShareActivity"  
         android:excludeFromRecents="true"  
         android:theme="@style/Theme.UMDefault" />  

以及在主module包名下添加 package : wxapi,
在wxapi里面添加WXEntryActivity.Java,
如下:

package com.wtime_developer.wecaht.wxapi;  
import com.umeng.weixin.callback.WXCallbackActivity;  
public class WXEntryActivity extends WXCallbackActivity {  
}  

微信分享代码:

share(SHARE_MEDIA.WEIXIN);//微信  
share(SHARE_MEDIA.WEIXIN_CIRCLE);//朋友圈  

private void share(SHARE_MEDIA media) {  
       new ShareAction(acticity).setPlatform(media)  
               .withTitle("Title")  
               .withText("content")  
               .withMedia(new UMImage(context, R.mipmap.ic_launcher))//第二个参数也可以是网络图片地址  
               .withTargetUrl("url")//修改为你要分享的链接  
               .setCallback(umShareListener)  
               .share();  
   }  

private UMShareListener umShareListener = new UMShareListener() {  
       @Override  
       public void onResult(SHARE_MEDIA platform) {  
           Toast.makeText(context, " 分享成功啦", Toast.LENGTH_SHORT).show();  
           finish();  
       }  

       @Override  
       public void onError(SHARE_MEDIA platform, Throwable t) {  
           Toast.makeText(context, " 分享失败啦", Toast.LENGTH_SHORT).show();  
           finish();  
       }  
  
       @Override  
       public void onCancel(SHARE_MEDIA platform) {  
           Toast.makeText(context, " 分享取消了", Toast.LENGTH_SHORT).show();  
           finish();  
       }  
   };  

登录代码:

private void login() {  
    UMShareAPI mShareAPI = UMShareAPI.get(context);  
    if (mShareAPI.isInstall(acticity, SHARE_MEDIA.WEIXIN)) {//判断是否安装微信  
        mShareAPI.doOauthVerify(acticity, SHARE_MEDIA.WEIXIN, umAuthListener);//授权  
    }  
}  

private UMAuthListener umAuthListener = new UMAuthListener() {  
    @Override  
    public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {  
        //授权成功  
        UMShareAPI mShareAPI = UMShareAPI.get(context);  
        mShareAPI.getPlatformInfo(acticity, SHARE_MEDIA.WEIXIN, umAuthListener1);//获取用户信息  
    }  

    @Override  
    public void onError(SHARE_MEDIA platform, int action, Throwable t) {  
        Toast.makeText(context, "授权失败", Toast.LENGTH_SHORT).show();  
    }  

    @Override  
    public void onCancel(SHARE_MEDIA platform, int action) {  
        Toast.makeText(context, "授权取消", Toast.LENGTH_SHORT).show();  
    }  
};  

private UMAuthListener umAuthListener1 = new UMAuthListener() {  
    @Override  
    public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {  
        //把友盟返回的信息 转化为微信的实体类  
        String json = "{\"openid\":\"" + map.get("openid") + "\","  
                + "\"nickname\":\"" + map.get("screen_name") + "\","  
                + "\"sex\":\"" + map.get("gender") + "\","  
                + "\"province\":\"" + map.get("province") + "\","  
                + "\"city\":\"" + map.get("city") + "\","  
                + "\"country\":\"" + map.get("country") + "\","  
                + "\"headimgur\":\"" + map.get("profile_image_url") + "\","  
                + "\"privilege\":[],"  
                + "\"unionid\":\"" + map.get("unionid") + "\"}";  
        //再调用你们后台写的微信登录相关的接口,  
    }  

    @Override  
    public void onError(SHARE_MEDIA platform, int action, Throwable t) {  
        Toast.makeText(context, "获取用户信息失败", Toast.LENGTH_SHORT).show();  
    }  

    @Override  
    public void onCancel(SHARE_MEDIA platform, int action) {  
        Toast.makeText(context, "取消获取用户信息", Toast.LENGTH_SHORT).show();  
    }  
};  

注意事项:
1.确保微信开放平台上的 包名和应用签名准确(配置好签名再用这工具获取签名)。 应用签名生成工具


2.修改了 app/MyApplication里的appid 和 appsecret
3.修改umeng module里manifest里面的umengappkey
4.在app.build 弄好相应的签名信息
5.确保混淆文件添加

有问题 欢迎留言.

项目地址:https://github.com/103style/Wechat
CSDN下载地址:http://download.csdn.net/download/lxk_1993/9675348

推荐阅读更多精彩内容