1,在类AppActivity里加上
public static IWeiboShareAPI mWeiboShareAPI = null;,
2,在protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
后面加上
// 创建微博分享接口实例
mWeiboShareAPI = WeiboShareSDK.createWeiboAPI(this, Constants.APP_KEY);
// 注册第三方应用到微博客户端中,注册成功后该应用将显示在微博的应用列表中。
// 但该附件栏集成分享权限需要合作申请,详情请查看 Demo 提示
// NOTE:请务必提前注册,即界面初始化的时候或是应用程序初始化时,进行注册
mWeiboShareAPI.registerApp();
if (savedInstanceState != null) {
mWeiboShareAPI.handleWeiboResponse(getIntent(), this);
}
并把public class AppActivity extends Cocos2dxActivity { 修改为
public class AppActivity extends Cocos2dxActivity implements Response {
3,创建分享函数(只实现了文字+图片的分享,有无安装客户端都可)
//
public static void shareWeibo() throws FileNotFoundException{
if (isEnter == true){
@SuppressWarnings("unused")
int supportApi = mWeiboShareAPI.getWeiboAppSupportAPI();
WeiboMultiMessage weiboMessage = new WeiboMultiMessage();
weiboMessage.textObject = getTextObj(); //分享的文字
weiboMessage.imageObject = getImageObj();//分享的图片
// 2. 初始化从第三方到微博的消息请求
SendMultiMessageToWeiboRequest request = new SendMultiMessageToWeiboRequest();
// 用transaction唯一标识一个请求
request.transaction = String.valueOf(System.currentTimeMillis());
request.multiMessage = weiboMessage;
// 3. 发送请求消息到微博,唤起微博分享界面
AuthInfo authInfo = new AuthInfo(app, Constants.APP_KEY, Constants.REDIRECT_URL, Constants.SCOPE);
Oauth2AccessToken accessToken = mAccessToken;
String token = "";
if (accessToken != null) {
token = accessToken.getToken();
}
mWeiboShareAPI.sendRequest(app, request, authInfo, token, new WeiboAuthListener() {
@Override
public void onWeiboException( WeiboException arg0 ) {
}
@Override
public void onComplete( Bundle bundle ) {
// TODO Auto-generated method stub
Oauth2AccessToken newToken = Oauth2AccessToken.parseAccessToken(bundle);
}
@Override
public void onCancel() {
}
});
}
}
/**
* @see {@link Activity#onNewIntent}
*/
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// 从当前应用唤起微博并进行分享后,返回到当前应用时,需要在此处调用该函数
// 来接收微博客户端返回的数据;执行成功,返回 true,并调用
// {@link IWeiboHandler.Response#onResponse};失败返回 false,不调用上述回调
mWeiboShareAPI.handleWeiboResponse(intent, this);
}
/**
* 创建文本消息对象。
*
* @return 文本消息对象。
*/
private static TextObject getTextObj() {
TextObject textObject = new TextObject();
textObject.text = "www.baidu.com myWeibo FenXiang success!";
return textObject;
}
/**
* 创建图片消息对象。
*
* @return 图片消息对象。
* @throws FileNotFoundException
*/
@SuppressWarnings("deprecation")
private static ImageObject getImageObj() throws FileNotFoundException {
String imgFilePath = "/data/data/org.cocos2dx.Crazy/files/muguShare1.png";
FileInputStream fis = new FileInputStream(new File(imgFilePath));//文件输入流
Bitmap bmp = BitmapFactory.decodeStream(fis);
ImageObject imageObject = new ImageObject();
BitmapDrawable bitmapDrawable = new BitmapDrawable(bmp);
imageObject.setImageObject(bitmapDrawable.getBitmap());
return imageObject;
}
@Override
public void onResponse(BaseResponse arg0) {
// TODO Auto-generated method stub
}
PS:分享需要修改AndroidManifest.xml里的权限(文档里有说明)
4,cocos2dx 直接调用shareWeibo()即可实现分享。