码迷,mamicode.com
首页 > 微信 > 详细

android接入微信分享(朋友、朋友圈)、QQ分享(好友、空间)

时间:2016-08-27 12:42:09      阅读:464      评论:0      收藏:0      [点我收藏+]

标签:

1、申请注册你的appid 
2、下载sdk 
QQ: http://wiki.open.qq.com/wiki/mobile/SDK%E4%B8%8B%E8%BD%BD 
微信:https://open.weixin.qq.com/cgi-bin/readtemplate?t=resource/app_download_android_tmpl&lang=zh_CN 
3、将下载的文件

 技术分享

导入进工程下的app/libs/下,liba开头的的微信需要的,另外两个是QQ需要的 
阅读官方文档QQ:http://wiki.open.qq.com/wiki/mobile/API%E8%B0%83%E7%94%A8%E8%AF%B4%E6%98%8E#portal-header 
微信:https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419317340&token=&lang=zh_CN 
PS: 微信需要安装签名生成工具输入包名申请。 

4、在 AndroidManifest.xml 中配置 


微信:

<activity
            android:name=".wxapi.WXEntryActivity"
            android:exported="true">
        </activity>

QQ:

<activity
            android:name="com.tencent.tauth.AuthActivity"
            android:noHistory="true"
            android:launchMode="singleTask" >
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="tencent你的appid" />
            </intent-filter>
        </activity>
        <activity android:name="com.tencent.connect.common.AssistActivity"
            android:theme="@android:style/Theme.Translucent.NoTitleBar"
            android:configChanges="orientation|keyboardHidden|screenSize" />
    </application>

 

5、接下来就是代码,微信还需要在报名下指定文件夹中新建指定类名的activity 

技术分享

WXEntryActivity类:

public class WXEntryActivity  extends AppCompatActivity implements IWXAPIEventHandler {
    private final String APP_ID= "你的appid";
    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 通过WXAPIFactory工厂,获取IWXAPI的实例
        api = WXAPIFactory.createWXAPI(this, APP_ID, false);
        api.registerApp(APP_ID);
        api.handleIntent(getIntent(), this);
    }

    //微信发送消息给app,app接受并处理的回调函数
    @Override
    public void onReq(BaseReq req) {

    }

    //app发送消息给微信,微信返回的消息回调函数,根据不同的返回码来判断操作是否成功
    @Override
    public void onResp(BaseResp resp) {
        int result = 0;

        switch (resp.errCode) {
            case BaseResp.ErrCode.ERR_OK:
                result = R.string.errcode_success;
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                result = R.string.errcode_cancel;
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                result = R.string.errcode_deny;
                break;
            default:
                result = R.string.errcode_unknown;
                break;
        }

        Toast.makeText(this, result, Toast.LENGTH_LONG).show();
    }
}

ShareActivity类:

public class ShareActivity extends BaseActivity implements View.OnClickListener {
    private final String W_APPID = "你的appid";
    private IWXAPI api;

    private final String Q_APPID = "你的appid";
    private Tencent mTencent;

    private Button quit;
    private ImageView qZone;
    private ImageView qq;
    private ImageView weichat;
    private ImageView weichatFriend;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.share_popup);
        regToWx();
        regToQQ();
        setBaseActionbar();
        initView();
    }

    private void regToQQ() {
        // Tencent类是SDK的主要实现类,开发者可通过Tencent类访问腾讯开放的OpenAPI。
        // 其中APP_ID是分配给第三方应用的appid,类型为String。
        mTencent = Tencent.createInstance(Q_APPID, mContext);
    }

    private void regToWx() {
        api = WXAPIFactory.createWXAPI(this, W_APPID, true);
        api.registerApp(W_APPID);
    }

    private void initView() {
        quit = (Button) findViewById(R.id.quit);
        qZone = (ImageView) findViewById(R.id.qzone);
        qq = (ImageView) findViewById(R.id.qq);
        weichat = (ImageView) findViewById(R.id.weichat);
        weichatFriend = (ImageView) findViewById(R.id.weichat_friend);
        actionbarLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        quit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        qZone.setOnClickListener(this);
        qq.setOnClickListener(this);
        weichat.setOnClickListener(this);
        weichatFriend.setOnClickListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void setBaseActionbar() {
        setActionbarLeft(R.mipmap.pixel_ic_toolbar_back);
        setActionbarTitle("分享", "SAHRE");
        hideActionbarRight();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.qzone:
//                mLog("点击QQ空间");
                qq(true);
                break;
            case R.id.qq:
//                mLog("点击QQ");
                qq(false);
                break;
            case R.id.weichat:
//                mLog("点击威信");
                weiChat(0);
                break;
            case R.id.weichat_friend:
//                mLog("点击朋友圈");
                weiChat(1);
                break;
            default:
                break;
        }
    }

    private void qq(boolean flag) {
        ShareListener myListener = new ShareListener();

        final Bundle params = new Bundle();
        params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
        params.putString(QQShare.SHARE_TO_QQ_TITLE, "分享");
        params.putString(QQShare.SHARE_TO_QQ_SUMMARY,  "墨迹天气");
        params.putString(QQShare.SHARE_TO_QQ_TARGET_URL,  "http://www.qq.com/news/1.html");
        params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL,"https://www.baidu.com/img/bd_logo1.png");
        if(flag){
            params.putInt(QQShare.SHARE_TO_QQ_EXT_INT,  QQShare.SHARE_TO_QQ_FLAG_QZONE_AUTO_OPEN);
        }
        mTencent.shareToQQ(ShareActivity.this, params, myListener);
    }

    // 0-分享给朋友  1-分享到朋友圈
    private void weiChat(int flag) {
        if (!api.isWXAppInstalled()) {
            mToast("您还未安装微信客户端", 0);
            return;
        }
        String text = "这是我做的一款天气类app,高端大气上档次,快来看看吧!";
        WXTextObject textObject = new WXTextObject();
        textObject.text = text;

        WXMediaMessage msg = new WXMediaMessage(textObject);
        msg.mediaObject = textObject;
        msg.description = text;

        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = String.valueOf(System.currentTimeMillis());//transaction字段用于唯一标识一个请求,这个必须有,否则会出错
        req.message = msg;

        //表示发送给朋友圈  WXSceneTimeline  表示发送给朋友  WXSceneSession
        req.scene = flag == 0 ? SendMessageToWX.Req.WXSceneSession : SendMessageToWX.Req.WXSceneTimeline;

        api.sendReq(req);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        ShareListener myListener = new ShareListener();
        Tencent.onActivityResultData(requestCode,resultCode,data,myListener);
    }

    private class ShareListener implements IUiListener {

        @Override
        public void onCancel() {
            mToast("分享取消",0);
        }

        @Override
        public void onComplete(Object arg0) {
            mToast("分享成功",0);
        }

        @Override
        public void onError(UiError arg0) {
            mToast("分享出错",0);
        }

    }
}

 

代码应该一目了然,actionbarLeft是BaseActivity中的,无视就行,接下来是几张截图,应该看的更明白 

 

技术分享技术分享技术分享技术分享技术分享

android接入微信分享(朋友、朋友圈)、QQ分享(好友、空间)

标签:

原文地址:http://www.cnblogs.com/juji-baleite/p/5812745.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!