首先打開微信開放平臺:https://open.weixin.qq.com/ 若是沒有註冊,請先註冊並上傳開發者資料等待審覈。html
資源中心----移動應用開發----分享與收藏功能----Android開發手冊java
開發手冊連接地址:https://open.weixin.qq.com/zh_CN/htmledition/res/dev/document/sdk/android/index.htmlandroid
資源中心----移動應用開發----接入指南---Android接入指南(下面咱們主要參考這個)
web
一、申請AppIDapi
如何如今沒有AppID能夠先下載官方的Demo使用Demo中的AppID便可。安全
SDK和Demo下載地址:https://open.weixin.qq.com/cgi-bin/frame?t=resource/res_main_tmpl&verify=1&lang=zh_CN&target=res/app_download_android微信
使用微信分享、登陸、收藏、支付等功能須要的庫以及文件。點擊下載 Android開發工具包微信開發
使用微信語音識別接口、語音合成接口。點擊下載 語音SDK+Demo+開發文檔app
使用微信圖像識別接口。點擊下載 圖像SDK+Demo+開發文檔ide
使用微信卡券功能接口。點擊下載 卡券SDK+開發文檔
二、下載微信開發工具包(在上面已經下載)
三、開始搭建開發環境而後將libammsdk.jar文件拷貝到工程lib目錄下。
注意:若是使用的是Demo中的AppID則包名必定要先和Demo的包名一致,不然分享會出錯。
四、新建一個Activity在裏面放置一個按鈕
package net.sourceforge.simcpux; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import com.example.weixinsharedemo.share.WeixinShareManager; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button = (Button) findViewById(R.id.share_weibo); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { WeixinShareManager wsm = WeixinShareManager.getInstance(MainActivity.this); wsm.shareByWeixin(wsm.new ShareContentPic(R.drawable.ic_launcher), WeixinShareManager.WEIXIN_SHARE_TYPE_TALK); } }); } }
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/share_weibo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="分享到微信"/> </RelativeLayout>四、開始建立咱們的分享邏輯類並實現須要的分享功能
package com.example.weixinsharedemo.share; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.widget.Toast; import com.example.weixinsharedemo.util.WeixiShareUtil; import com.tencent.mm.sdk.modelmsg.SendMessageToWX; import com.tencent.mm.sdk.modelmsg.WXImageObject; import com.tencent.mm.sdk.modelmsg.WXMediaMessage; import com.tencent.mm.sdk.modelmsg.WXTextObject; import com.tencent.mm.sdk.modelmsg.WXWebpageObject; import com.tencent.mm.sdk.openapi.IWXAPI; import com.tencent.mm.sdk.openapi.WXAPIFactory; /** * 實現微信分享功能的核心類 * @author Administrator * */ public class WeixinShareManager{ private static final int THUMB_SIZE = 150; /** * 文字 */ public static final int WEIXIN_SHARE_WAY_TEXT = 1; /** * 圖片 */ public static final int WEIXIN_SHARE_WAY_PIC = 2; /** * 連接 */ public static final int WEIXIN_SHARE_WAY_WEBPAGE = 3; /** * 會話 */ public static final int WEIXIN_SHARE_TYPE_TALK = SendMessageToWX.Req.WXSceneSession; /** * 朋友圈 */ public static final int WEIXIN_SHARE_TYPE_FRENDS = SendMessageToWX.Req.WXSceneTimeline; private static WeixinShareManager instance; private static String weixinAppId; private IWXAPI wxApi; private Context context; private WeixinShareManager(Context context){ this.context = context; //初始化數據 weixinAppId = WeixiShareUtil.getWeixinAppId(context); //初始化微信分享代碼 if(weixinAppId != null){ initWeixinShare(context); } } /** * 獲取WeixinShareManager實例 * 非線程安全,請在UI線程中操做 * @return */ public static WeixinShareManager getInstance(Context context){ if(instance == null){ instance = new WeixinShareManager(context); } return instance; } private void initWeixinShare(Context context){ wxApi = WXAPIFactory.createWXAPI(context, weixinAppId, true); wxApi.registerApp(weixinAppId); } /** * 經過微信分享 * @param shareWay 分享的方式(文本、圖片、連接) * @param shareType 分享的類型(朋友圈,會話) */ public void shareByWeixin(ShareContent shareContent, int shareType){ switch (shareContent.getShareWay()) { case WEIXIN_SHARE_WAY_TEXT: shareText(shareType, shareContent); break; case WEIXIN_SHARE_WAY_PIC: sharePicture(shareType, shareContent); break; case WEIXIN_SHARE_WAY_WEBPAGE: shareWebPage(shareType, shareContent); break; } } private abstract class ShareContent{ protected abstract int getShareWay(); protected abstract String getContent(); protected abstract String getTitle(); protected abstract String getURL(); protected abstract int getPicResource(); } /** * 設置分享文字的內容 * @author Administrator * */ public class ShareContentText extends ShareContent{ private String content; /** * 構造分享文字類 * @param text 分享的文字內容 */ public ShareContentText(String content){ this.content = content; } @Override protected String getContent() { return content; } @Override protected String getTitle() { return null; } @Override protected String getURL() { return null; } @Override protected int getPicResource() { return -1; } @Override protected int getShareWay() { return WEIXIN_SHARE_WAY_TEXT; } } /** * 設置分享圖片的內容 * @author Administrator * */ public class ShareContentPic extends ShareContent{ private int picResource; public ShareContentPic(int picResource){ this.picResource = picResource; } @Override protected String getContent() { return null; } @Override protected String getTitle() { return null; } @Override protected String getURL() { return null; } @Override protected int getPicResource() { return picResource; } @Override protected int getShareWay() { return WEIXIN_SHARE_WAY_PIC; } } /** * 設置分享連接的內容 * @author Administrator * */ public class ShareContentWebpage extends ShareContent{ private String title; private String content; private String url; private int picResource; public ShareContentWebpage(String title, String content, String url, int picResource){ this.title = title; this.content = content; this.url = url; this.picResource = picResource; } @Override protected String getContent() { return content; } @Override protected String getTitle() { return title; } @Override protected String getURL() { return url; } @Override protected int getPicResource() { return picResource; } @Override protected int getShareWay() { return WEIXIN_SHARE_WAY_WEBPAGE; } } /* * 分享文字 */ private void shareText(int shareType, ShareContent shareContent) { String text = shareContent.getContent(); //初始化一個WXTextObject對象 WXTextObject textObj = new WXTextObject(); textObj.text = text; //用WXTextObject對象初始化一個WXMediaMessage對象 WXMediaMessage msg = new WXMediaMessage(); msg.mediaObject = textObj; msg.description = text; //構造一個Req SendMessageToWX.Req req = new SendMessageToWX.Req(); //transaction字段用於惟一標識一個請求 req.transaction = buildTransaction("textshare"); req.message = msg; //發送的目標場景, 能夠選擇發送到會話 WXSceneSession 或者朋友圈 WXSceneTimeline。 默認發送到會話。 req.scene = shareType; wxApi.sendReq(req); } /* * 分享圖片 */ private void sharePicture(int shareType, ShareContent shareContent) { Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), shareContent.getPicResource()); WXImageObject imgObj = new WXImageObject(bmp); WXMediaMessage msg = new WXMediaMessage(); msg.mediaObject = imgObj; Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, THUMB_SIZE, THUMB_SIZE, true); bmp.recycle(); msg.thumbData = WeixiShareUtil.bmpToByteArray(thumbBmp, true); //設置縮略圖 SendMessageToWX.Req req = new SendMessageToWX.Req(); req.transaction = buildTransaction("imgshareappdata"); req.message = msg; req.scene = shareType; wxApi.sendReq(req); } /* * 分享連接 */ private void shareWebPage(int shareType, ShareContent shareContent) { WXWebpageObject webpage = new WXWebpageObject(); webpage.webpageUrl = shareContent.getURL(); WXMediaMessage msg = new WXMediaMessage(webpage); msg.title = shareContent.getTitle(); msg.description = shareContent.getContent(); Bitmap thumb = BitmapFactory.decodeResource(context.getResources(), shareContent.getPicResource()); if(thumb == null){ Toast.makeText(context, "圖片不能爲空", Toast.LENGTH_SHORT).show(); }else{ msg.thumbData = WeixiShareUtil.bmpToByteArray(thumb, true); } SendMessageToWX.Req req = new SendMessageToWX.Req(); req.transaction = buildTransaction("webpage"); req.message = msg; req.scene = shareType; wxApi.sendReq(req); } private String buildTransaction(final String type) { return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis(); } }上面只是實現了文字、圖片、連接的分享功能,如需分享視頻、音頻能夠添加。
五、實現分享結果反饋
要實現分享結果的反饋,微信已經將結果的處理封裝到了WXEntryActivity中(jar包中),因此咱們要繼承該類獲取反饋結果。
在你的包名相應目錄下新建一個wxapi目錄,並在該wxapi目錄下新增一個WXEntryActivity類,該類繼承自Activity(例如應用程序的包名爲net.sourceforge.simcpux,則新添加的類以下圖所示)
package net.sourceforge.simcpux.wxapi; import com.example.weixinsharedemo.share.WXCallbackActivity; public class CallBackActivity extends WXCallbackActivity{ @Override public void weixinResp(int respCode) { System.out.println("respCode = " + respCode); } }注意:記得去Mainfest文件中註冊Activity
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="net.sourceforge.simcpux" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="net.sourceforge.simcpux.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="net.sourceforge.simcpux.wxapi.CallBackActivity" android:label="@string/app_name" android:exported="true"></activity> <meta-data android:name="WEIXIN_APP_ID" android:value="你的AppId"/> </application> </manifest>
點擊Browser進行選擇,運行便可。
其他兩篇關於QQ和微博的分享請參看《Android實現QQ分享及注意事項》《Android實現微博分享及其注意事項》
若是有什麼問題請在下面留言,也能夠加博客旁邊的QQ羣(142979499)進行詳細交流。