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>