复制代码
|
复制代码
|
1
2
3
|
private
static
final
String APP_ID =
"wx81115d2aa55710fa"
;
//AppID,从第四步获取
private
static
IWXAPI api;
//微信API接口
private
static
AppActivity instance;
//类静态实例,为了方便后面静态函数的调用
|
1
2
3
4
5
6
7
8
9
10
11
12
|
protected
void
onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super
.onCreate(savedInstanceState);
instance =
this
;
regToWX();
}
private
void
regToWX(){
api = WXAPIFactory.createWXAPI(
this
, APP_ID,
true
);
api.registerApp(APP_ID);
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
public
static
void
sendMsgToFriend(){
if
(api.openWXApp())
{
WXWebpageObject webpage =
new
WXWebpageObject();
webpage.webpageUrl =
"http://www.fusijie.com"
;
WXMediaMessage msg =
new
WXMediaMessage(webpage);
msg.title =
"Tittle"
;
msg.description =
"Description"
;
Bitmap thumb = BitmapFactory.decodeResource(instance.getResources(), R.drawable.icon);
msg.thumbData = Util.bmpToByteArray(thumb,
true
);
SendMessageToWX.Req req =
new
SendMessageToWX.Req();
req.transaction = buildTransaction(
"webpage"
);
req.message = msg;
req.scene = SendMessageToWX.Req.WXSceneSession;
api.sendReq(req);
}
else
{
Toast.makeText(instance,
"未安装微信"
, Toast.LENGTH_SHORT).show();
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
public
static
void
sendMsgToTimeLine(){
if
(api.openWXApp())
{
if
(api.getWXAppSupportAPI() >=
0x21020001
)
{
WXWebpageObject webpage =
new
WXWebpageObject();
webpage.webpageUrl =
"http://www.fusijie.com"
;
WXMediaMessage msg =
new
WXMediaMessage(webpage);
msg.title =
"Tittle"
;
msg.description =
"Description"
;
Bitmap thumb = BitmapFactory.decodeResource(instance.getResources(), R.drawable.icon);
msg.thumbData = Util.bmpToByteArray(thumb,
true
);
SendMessageToWX.Req req =
new
SendMessageToWX.Req();
req.transaction = buildTransaction(
"webpage"
);
req.message = msg;
req.scene = SendMessageToWX.Req.WXSceneTimeline;
api.sendReq(req);
}
else
{
Toast.makeText(instance,
"微信版本过低"
, Toast.LENGTH_SHORT).show();
}
}
else
{
Toast.makeText(instance,
"未安装微信"
, Toast.LENGTH_SHORT).show();
}
}
|
1
2
3
|
private
static
String buildTransaction(
final
String type) {
return
(type ==
null
) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
void
WeiXinShare::sendToFriend()
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) //判断当前是否为Android平台
JniMethodInfo minfo;
bool
isHave = JniHelper::getStaticMethodInfo(minfo,
"org/cocos2dx/cpp/AppActivity"
,
"sendMsgToFriend"
,
"()V"
);
if
(!isHave) {
log
(
"jni:sendMsgToFriend is null"
);
}
else
{
//调用此函数
minfo.env->CallStaticVoidMethod(minfo.classID, minfo.methodID);
}
#endif
}
void
WeiXinShare::sendToTimeLine()
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) //判断当前是否为Android平台
JniMethodInfo minfo;
bool
isHave = JniHelper::getStaticMethodInfo(minfo,
"org/cocos2dx/cpp/AppActivity"
,
"sendMsgToTimeLine"
,
"()V"
);
if
(!isHave) {
log
(
"jni:sendMsgToTimeLine is null"
);
}
else
{
//调用此函数
minfo.env->CallStaticVoidMethod(minfo.classID, minfo.methodID);
}
#endif
}
|
1
2
3
4
5
6
7
|
void
HelloWorld::menuCloseCallback(Ref* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
//WeiXinShare::sendToFriend();
WeiXinShare::sendToTimeLine();
#endif
}
|
复制代码
|