Android微信开发(二):代码分析

代码分析:界面很简单


main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    android:background="@color/navpage">


    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/text_title" />


    <Button
        android:id="@+id/reg_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/reg_to_wx" />


    <Button
        android:id="@+id/goto_send_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/goto_sendMessage" />


    <Button
        android:id="@+id/launch_wx_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/start_wx" />


    <Button
        android:id="@+id/check_timeline_supported_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/check_on_version" />


</LinearLayout>



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();

}
     

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值