1. 程式人生 > >Android微信開發(二):程式碼分析

Android微信開發(二):程式碼分析

package com.example.teststudyshare.wxapi;


import java.io.ObjectOutputStream.PutField;


import android.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.SyncStateContract.Constants;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;


import com.example.teststudyshare.GetFromWXActivity;
import com.example.teststudyshare.SendToWXActivity;
import com.example.teststudyshare.ShowFromWXActivity;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.modelmsg.ShowMessageFromWX;
import com.tencent.mm.sdk.modelmsg.WXAppExtendObject;
import com.tencent.mm.sdk.modelmsg.WXMediaMessage;
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 {
    private static final int TIMELINE_SUPPORT_VERSION = 0X1200120;
private Button regBtn,gotoBtn,checkBtn,luanchBtn;
    private IWXAPI api;
private final String APP_ID = "88888888888";

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
api = WXAPIFactory.createWXAPI(this, APP_ID, false);

regBtn = (Button) findViewById(R.id.button1);
regBtn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
//註冊到微信
api.registerApp(APP_ID);
}
});
gotoBtn = (Button) findViewById(R.id.button2);
gotoBtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
startActivity(new Intent(WXEntryActivity.this,SendToWXActivity.class));
   finish();
}
});
//點選進入微信按鈕
luanchBtn = (Button) findViewById(R.id.button3);
luanchBtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Toast.makeText(WXEntryActivity.this, "luanch startWX = " + api.openWXApp(), Toast.LENGTH_LONG).show();

}
});
checkBtn = (Button) findViewById(R.id.background);
checkBtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
int wxSDKVersion = api.getWXAppSupportAPI();
if(wxSDKVersion >= TIMELINE_SUPPORT_VERSION)
{
Toast.makeText(WXEntryActivity.this, "wx_Sdk_version =" + Integer.toHexString(wxSDKVersion) +"\n timeLine support", Toast.LENGTH_LONG).show();
}else
Toast.makeText(WXEntryActivity.this, "wx_sdk_version = "+ Integer.toHexString(wxSDKVersion)+"\n timeLine not support", Toast.LENGTH_LONG).show();

}
});
api.handleIntent(getIntent(), this);
}
@Override
protected void onNewIntent(Intent intent) {

super.onNewIntent(intent);
setIntent(intent);
api.handleIntent(intent, this);
}
  //微信傳送請求到第三方,會呼叫這個方法
@Override
public void onReq(BaseReq req) {
// req請求
switch(req.getType())
{
case ConstantsAPI.COMMAND_GETMESSAGE_FROM_WX:
gotoGetMseeage();
break;
case ConstantsAPI.COMMAND_SHOWMESSAGE_FROM_WX:
gotoShowMessage((ShowMessageFromWX.Req) req);
break;
default:
break;
}
}


private void gotoShowMessage(ShowMessageFromWX.Req showReq) {
  WXMediaMessage wxMsg = showReq.message;
  WXAppExtendObject obj = (WXAppExtendObject) wxMsg.mediaObject;
  StringBuffer msg = new StringBuffer();
  msg.append("description: ");
msg.append(wxMsg.description);
msg.append("\n");
msg.append("extInfo: ");
msg.append(obj.extInfo);
msg.append("\n");
msg.append("filePath: ");
msg.append(obj.filePath);

Intent intent = new Intent(this, ShowFromWXActivity.class);
intent.putExtra(Constants.ShowMsgActivity.STitle, wxMsg.title);
intent.putExtra(Constants.ShowMsgActivity.SMessage, msg.toString());
intent.putExtra(Constants.ShowMsgActivity.BAThumbData, wxMsg.thumbData);
startActivity(intent);
finish();

}
private void gotoGetMseeage() {
Intent intent = new Intent(this, GetFromWXActivity.class);
intent.putExtras(getIntent());
startActivity(intent);
finish();

}
@Override
public void onResp(BaseResp resp) {
// 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();

}
     

}