首先是申请微信开放平台https://open.weixin.qq.com/
根据提示在管理中心创建一个移动应用,最好是自己的公司的上线APP,这样审批容易通过,
审批通过后,会为创建的移动应用下发APP_ID、AppSecret
另外在网站上下载android版本的jar包即可libammsdk.jar 即可进行操作了
~~~~~~~~~~~~~~~~开始吧,用的测试机,没有sd卡,另外我的台式机没有wifi,所以代码没测试,我后期会补上的~~~~~~~~~~~~~~~~~
activity_main.xml
package com.godinsec.seland;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import com.tencent.a.a.a.a.e;
import com.tencent.mm.sdk.modelmsg.SendMessageToWX;
import com.tencent.mm.sdk.modelmsg.WXEmojiObject;
import com.tencent.mm.sdk.modelmsg.WXImageObject;
import com.tencent.mm.sdk.modelmsg.WXMediaMessage;
import com.tencent.mm.sdk.modelmsg.WXMusicObject;
import com.tencent.mm.sdk.modelmsg.WXTextObject;
import com.tencent.mm.sdk.modelmsg.WXVideoObject;
import com.tencent.mm.sdk.modelmsg.SendMessageToWX.Req;
import com.tencent.mm.sdk.modelmsg.WXWebpageObject;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.style.BulletSpan;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
// 是否分享到朋友圈
private CheckBox mShareFriends;
// 申请微信开放平台的APP_ID(自己公司上线的APP)
public static final String APP_ID = "wxf46ddde7de655653";
// IWXAPI-api
private IWXAPI api;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mShareFriends = (CheckBox) findViewById(R.id.mShareFriends);
// 创建api
api = WXAPIFactory.createWXAPI(this, APP_ID);
// 将app_id注册微信中
api.registerApp(APP_ID);
}
/**
* 启动微信客户段
*/
public void launchWeiXin(View view) {
// 打开微信--versionCode:2
api.openWXApp();
}
// 为请求构造唯一的标示符
private String buildTransaction(String type) {
return (type == null) ? String.valueOf(System.currentTimeMillis())
: type + System.currentTimeMillis();
}
/*********************************************************************
* 向微信中分享文本信息
*/
public void sendText(View view) {
// 創建文本
final EditText editText = new EditText(this);
editText.setText("默认的分享文本");
// 創建alertdialog對話框
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher);//icon
builder.setTitle("共享文本");//title
builder.setMessage("请输入要分享的文本");//内容
builder.setView(editText);//设置
builder.setPositiveButton("分享", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String text = editText.getText().toString();
if (text == null || text.length() == 0) {
return;
}
// 创建一个用于封装待分享文本的WXTextObject对象
WXTextObject textObject = new WXTextObject();
//设置文本信息
textObject.text = text;
// 创建WXMediaMessage对像,用于客户端向微信发送数据
WXMediaMessage msg = new WXMediaMessage();
msg.mediaObject = textObject;
msg.description = text;
// 创建一个用于请求微信客户端的SendMessageTowx.Reg对象
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.message = msg;
req.transaction = buildTransaction("text");// 唯一的标示
// 场景-标示发送给朋友还是朋友圈
req.scene = mShareFriends.isChecked() ? SendMessageToWX.Req.WXSceneTimeline
: SendMessageToWX.Req.WXSceneSession;
// 发送微信客户端口
api.sendReq(req);
Toast.makeText(MainActivity.this,
String.valueOf(api.sendReq(req)), 0).show();
finish();
}
});
builder.setNegativeButton("取消", null);
final AlertDialog alertDialog = builder.create();
alertDialog.show();
}
/***********************************************************************
* 分享图片信息
*/
// 将bitmap转为byte格式的数组
private byte[] bmpToByteArray(final Bitmap bitmap, final boolean needRecycle) {
//创建字节数组输出流
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
//Bitmap.compress()方法的参数format可设置JPEG或PNG格式;quality可选择压缩质量;fOut是输出流(OutputStream)
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
if (needRecycle) {
bitmap.recycle();
}
//将字节数组输出流转为byte数组
byte[] result = outputStream.toByteArray();
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
public void send_binary_image(View view) {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);
// 创建WXImageObject对象,包装bitmap
WXImageObject image = new WXImageObject(bitmap);
// 创建对象,并且包装WXImageObject对象
WXMediaMessage msg = new WXMediaMessage();
msg.mediaObject = image;
// 压缩图片--宽度120 高度150的图像
Bitmap thumbBmp = Bitmap.createScaledBitmap(bitmap, 120, 150, true);
bitmap.recycle();// 回收图片占用的内存资源
// 设置缩略图
msg.thumbData = bmpToByteArray(thumbBmp, true);
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = buildTransaction("send_binary_image");
req.message = msg;
// 场景-标示发送给朋友还是朋友圈
req.scene = mShareFriends.isChecked() ? SendMessageToWX.Req.WXSceneTimeline
: SendMessageToWX.Req.WXSceneSession;
api.sendReq(req);
Toast.makeText(MainActivity.this, String.valueOf(api.sendReq(req)), 0)
.show();
finish();
}
public void send_local_image(View view) {
// 判断图片文件是否存在
String path = "/sdcard/图片名字.png";
File file = new File(path);
if (!file.exists()) {
// 如果不存咋
Toast.makeText(this, "文件不存在", 0).show();
return;
}
// 创建WXImageObject对象,包装bitmap
WXImageObject image = new WXImageObject();
// 设置图片文件的路径
image.setImagePath(path);
// 创建对象,并且包装WXImageObject对象
WXMediaMessage msg = new WXMediaMessage();
msg.mediaObject = image;
Bitmap bitmap = BitmapFactory.decodeFile(path);
// 压缩图片--宽度120 高度150的图像
Bitmap thumbBmp = Bitmap.createScaledBitmap(bitmap, 120, 150, true);
bitmap.recycle();// 回收图片占用的内存资源
// 设置缩略图
msg.thumbData = bmpToByteArray(thumbBmp, true);
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = buildTransaction("send_local_image");
req.message = msg;
// 场景-标示发送给朋友还是朋友圈
req.scene = mShareFriends.isChecked() ? SendMessageToWX.Req.WXSceneTimeline
: SendMessageToWX.Req.WXSceneSession;
api.sendReq(req);
Toast.makeText(MainActivity.this, String.valueOf(api.sendReq(req)), 0)
.show();
finish();
}
/**
* 有小问题??????????????
*/
public void send_url_image(View view) {
Thread thread = new Thread() {
public void run() {
try {
String url = "http://img.mukewang.com/55237dcc0001128c06000338-300-170.jpg";
// 创建WXImageObject对象,包装bitmap
WXImageObject image = new WXImageObject();
image.imageUrl = url;
// 创建对象,并且包装WXImageObject对象
WXMediaMessage msg = new WXMediaMessage();
msg.mediaObject = image;
Bitmap bitmap = BitmapFactory.decodeStream(new URL(url)
.openStream());
// 压缩图片--宽度120 高度150的图像
Bitmap thumbBmp = Bitmap.createScaledBitmap(bitmap, 12,
15, true);
bitmap.recycle();// 回收图片占用的内存资源
// 设置缩略图
msg.thumbData = bmpToByteArray(thumbBmp, true);
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = buildTransaction("send_url_image");
req.message = msg;
// 场景-标示发送给朋友还是朋友圈
req.scene = mShareFriends.isChecked() ? SendMessageToWX.Req.WXSceneTimeline
: SendMessageToWX.Req.WXSceneSession;
api.sendReq(req);
Toast.makeText(MainActivity.this,
String.valueOf(api.sendReq(req)), 0).show();
finish();
} catch (Exception e) {
e.printStackTrace();
}
}
};
thread.start();
}
/*********************************************************************
* 发送url地址的音频
*/
public void send_audio(View view) {
WXMusicObject musicObject = new WXMusicObject();
musicObject.musicUrl = "http://music.baidu.com/song/999104?pst=sug";
WXMediaMessage msg = new WXMediaMessage();
msg.mediaObject = musicObject;
msg.title = "向天在借500年";
msg.description = "演唱:韩磊";
// 设置说略图
Bitmap thumb = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);
msg.thumbData = bmpToByteArray(thumb, true);
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = buildTransaction("send_audio");
req.message = msg;
// 场景-标示发送给朋友还是朋友圈
req.scene = mShareFriends.isChecked() ? SendMessageToWX.Req.WXSceneTimeline
: SendMessageToWX.Req.WXSceneSession;
api.sendReq(req);
Toast.makeText(this, String.valueOf(api.sendReq(req)), 0).show();
}
/**********************************************************
* 发送视频到微信
*/
public void send_video(View view) {
// 创建WXVideoObject对象,用于制定视频url
WXVideoObject videoObject = new WXVideoObject();
videoObject.videoUrl = "http://v.youku.com/v_show/id_XNTUxNDY1NDY4.html";
WXMediaMessage msg = new WXMediaMessage();
msg.mediaObject = videoObject;
msg.title = "乔布斯访谈";
msg.description = "看看乔帮主都说了写什么";
// 设置说略图
Bitmap thumb = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);
msg.thumbData = bmpToByteArray(thumb, true);
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = buildTransaction("send_video");
req.message = msg;
// 场景-标示发送给朋友还是朋友圈
req.scene = mShareFriends.isChecked() ? SendMessageToWX.Req.WXSceneTimeline
: SendMessageToWX.Req.WXSceneSession;
api.sendReq(req);
Toast.makeText(this, String.valueOf(api.sendReq(req)), 0).show();
}
/**************************************************************
* 发送webpage url地址
* @param view
*/
public void send_url(View view) {
// 创建一个WXWebPageObject对象,封装url
WXWebpageObject webpageObject = new WXWebpageObject();
webpageObject.webpageUrl = "http://www.imooc.com/course/list";
WXMediaMessage msg = new WXMediaMessage(webpageObject);
msg.title = "网站";
msg.description = "在线教育";
// 缩略图片
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);
msg.thumbData = bmpToByteArray(bitmap, true);
// 创建
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = "send_url";
req.message = msg;
req.scene = mShareFriends.isChecked() ? SendMessageToWX.Req.WXSceneTimeline
: SendMessageToWX.Req.WXSceneSession;
api.sendReq(req);
finish();
}
/*************************************************
* 发送表情。gif动态图片
* @param view
*/
public void send_motion(View view) {
String emotion_path = "sd卡";
WXEmojiObject mEmojiObject = new WXEmojiObject();
mEmojiObject.emojiPath = emotion_path;
WXMediaMessage msg = new WXMediaMessage(mEmojiObject);
msg.title = "表情标题";
msg.description = "表情描述";
// 缩略图
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);
msg.thumbData = bmpToByteArray(bitmap, true);
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = buildTransaction("send_motion");
req.message = msg;
req.scene = mShareFriends.isChecked() ? SendMessageToWX.Req.WXSceneTimeline
: SendMessageToWX.Req.WXSceneSession;
api.sendReq(req);
finish();
}
}