Android-based WeChat sharing and precautions

Source: Internet
Author: User

Android implementation sharing and precautions, android precautions
1. Get help documents and download relevant materials

First Open Platform: https://open.weixin.qq.com/if not registered, please first register and upload developer information for review.

Resource Center-mobile application development-sharing and adding to favorites-Android development manual

Developer manual link: https://open.weixin.qq.com/zh_CN/htmledition/res/dev/document/sdk/android/index.html

Resource Center-mobile application development-access guide-Android access guide (refer to this document below)

Ii. Access sharing

1. Apply for AppID

If you do not have AppID, you can download the official Demo and use AppID in the Demo.

SDK and Demo: https://open.weixin.qq.com/cgi-bin/frame? T = resource/res_main_tmpl & verify = 1 & lang = zh_CN & target = res/app_download_android

Development Kit (SDK)

Use the libraries and files required for sharing, login, favorites, and payment. Click to download the Android Development Kit

Use the speech recognition interface and speech synthesis interface. Click to download the audio SDK + Demo + development document

Use the image recognition interface. Click to download image SDK + Demo + development document

Use the coupon function interface. Click to download the coupon SDK + development document

Sample Code
Contains a complete sample project. For more information about how to use this example, see HelloWeixin @ Android. Click to download
Signature generation tool
Obtains the apk package signed by a third-party app installed on the mobile phone. Click to download the signature generation tool
After the download is complete, we can refer to the Demo for integration.

2. Download The Development Kit (downloaded above)

3. Create a development environment and copy the libammsdk. jar file to the lib directory of the project.

Note: If you use AppID in the Demo, the package name must be the same as the package name in the Demo. Otherwise, an error occurs during sharing.


4. Create an Activity and place a button in it.

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 {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button button = (Button) findViewById(R.id.share_weibo);button.setOnClickListener(new OnClickListener() {@Overridepublic 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/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/plugin" tools: context = ". mainActivity "> <Button android: id =" @ + id/pai_weibo "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: text = "share to"/> </RelativeLayout>
4. Start to create our sharing logic class and implement the required sharing function.

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. wxapifacloud;/*** core class for sharing functions * @ author Administrator **/public class WeixinShareManager {private static final int THUMB_SIZE = 150; /*** text */public static final int weixin_gate_way_text = 1;/*** image */public static final int weixin_gate_way_pic = 2 ;/*** Link */public static final int weixin_gate_way_webpage = 3;/*** session */public static final int weixin_assist_type_talk = SendMessageToWX. req. WXSceneSession;/*** circle of friends */public static final int weixin_pai_type_frends = SendMessageToWX. req. WXSceneTimeline; private static WeixinShareManager instance; private static String weixinAppId; private IWXAPI wxApi; private Context context Context; private WeixinShar EManager (Context context) {this. context = context; // initialize weixinAppId = WeixiShareUtil. getWeixinAppId (context); // initialize the sharing code if (weixinAppId! = Null) {initWeixinShare (context) ;}/ *** get WeixinShareManager instance * Non-thread security, please operate * @ return */public static WeixinShareManager getInstance (Context context) {if (instance = null) {instance = new WeixinShareManager (context) in the UI thread );} return instance;} private void initWeixinShare (Context context) {wxApi = wxapifacloud. createWXAPI (context, weixinAppId, true); wxApi. registerApp (weixinAppId);}/*** share * @ param shar EWay sharing method (text, images, links) * @ param shareType sharing type (circle of friends, sessions) */public void shareByWeixin (Response content contains content, int shareType) {switch (Response content. getgateway () {case weixin_gate_way_text: shareText (shareType, shareContent); break; case when: sharePicture (shareType, shareContent); break; case when: shareWebPage (shareType, shareContent ); break;} private abstract class Sh AreContent {protected abstract int getgateway (); protected abstract String getContent (); protected abstract String getTitle (); protected abstract String getURL (); protected abstract int getPicResource ();} /*** set the content of shared text * @ author Administrator **/public class internal contenttext extends internal content {private String content; /*** construct the text shared by the shared text class * @ param text */public Character contenttext (String content) {this. Content = content ;}@ Overrideprotected String getContent () {return content ;}@ Overrideprotected String getTitle () {return null ;}@ Overrideprotected String getURL () {return null ;} @ Overrideprotected int getPicResource () {return-1 ;}@ Overrideprotected int getgateway () {return weixin_gate_way_text ;}} /*** set the image sharing content * @ author Administrator **/public class internal contentpic extends internal content {private I Nt picResource; public parameter contentpic (int picResource) {this. picResource = picResource ;}@ Overrideprotected String getContent () {return null ;}@ Overrideprotected String getTitle () {return null ;}@ Overrideprotected String getURL () {return null ;} @ Overrideprotected int getPicResource () {return picResource;} @ Overrideprotected int getgateway () {return weixin_gate_way_pic;}/*** set the content of the Sharing link * @ author Admin Istrator **/public class extends contentwebpage extends content {private String title; private String content; private String url; private int picResource; public extends contentwebpage (String title, String content, String url, int picResource) {this. title = title; this. content = content; this. url = url; this. picResource = picResource ;}@ Overrideprotected String getContent () {return content ;}@ Overrideprotec Ted String getTitle () {return title ;}@ Overrideprotected String getURL () {return url ;}@ Overrideprotected int getPicResource () {return picResource ;}@ Overrideprotected int getgateway () {return weixin_gate_way_webpage;}/** share text */private void shareText (int shareType, ShareContent shareContent) {String text = shareContent. getContent (); // initialize a WXTextObject object WXTextObject textObj = new WXTextObject (); t ExtObj. text = text; // use the WXTextObject object to initialize a WXMediaMessage object WXMediaMessage msg = new WXMediaMessage (); msg. mediaObject = textObj; msg. description = text; // construct a ReqSendMessageToWX. req req = new SendMessageToWX. req (); // The transaction field uniquely identifies a request req. transaction = buildTransaction ("textshare"); req. message = msg; // target scenario for sending. You can choose to send the message to the WXSceneSession session or the WXSceneTimeline in the circle of friends. Sent to the session by default. Req. scene = shareType; wxApi. sendReq (req);}/** share image */private void sharePicture (int shareType, ShareContent shareContent) {Bitmap bmp = BitmapFactory. decodeResource (context. getResources (), response content. getPicResource (); WXImageObject imgObj = new WXImageObject (bmp); WXMediaMessage msg = new WXMediaMessage (); msg. mediaObject = imgObj; Bitmap thumbBmp = Bitmap. createScaledBitmap (bmp, THUMB_SIZE, THUMB_S IZE, true); bmp. recycle (); msg. thumbData = WeixiShareUtil.bmp ToByteArray (thumbBmp, true); // set the thumbnail SendMessageToWX. req req = new SendMessageToWX. req (); req. transaction = buildTransaction ("img1_appdata"); req. message = msg; req. scene = shareType; wxApi. sendReq (req);}/** sharing link */private void publish webpage (int shareType, ShareContent publish content) {WXWebpageObject webpage = new WXWebpageObject (); webpage. we BpageUrl = Invalid Content. getURL (); WXMediaMessage msg = new WXMediaMessage (webpage); msg. title = Invalid Content. getTitle (); msg. description = Invalid Content. getContent (); Bitmap thumb = BitmapFactory. decodeResource (context. getResources (), response content. getPicResource (); if (thumb = null) {Toast. makeText (context, "the image cannot be blank", Toast. LENGTH_SHORT ). show ();} else {msg. thumbData = WeixiShareUtil.bmp ToByteArray (thumb, t Rue);} 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 ();}}
Only text, images, and links can be shared. You can add videos and audios if you want to share them.

5. Implement feedback on shared results

To implement feedback on shared results, we have encapsulated the results into WXEntryActivity (in the jar package). Therefore, we need to inherit this class to obtain feedback results.

Create a wxapi directory under the corresponding directory of your package name, and add a WXEntryActivity class under the wxapi directory. This class inherits from Activity (for example, the package name of the application is net. sourceforge. simcpux, the newly added class is shown in)

package net.sourceforge.simcpux.wxapi;import com.example.weixinsharedemo.share.WXCallbackActivity;public class CallBackActivity extends WXCallbackActivity{@Overridepublic void weixinResp(int respCode) {System.out.println("respCode = " + respCode);}}
Note: Remember to register the Activity in the Mainfest file.
<? 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 = "Your AppId"/> </application> </manifest>
3. If you are running the code using AppId in the Demo, please set the signature file (which is included in the SDK downloaded above) as follows:

Click Browser to select and run.

If you have any questions, please leave a message below. You can also add the QQ group (142979499) next to your blog for detailed communication. In addition, let's take a look at my sunshine stack (www.sunhome.aliapp.com ).



Q: How can I share my notes on Android notepad, Weibo, and other social software?

Intent intent = new Intent (Intent. ACTION_SEND );

Intent. setType ("text/plain ");
Intent. putExtra (Intent. EXTRA_STREAM, u );
Intent. putExtra (Intent. EXTRA_SUBJECT, "share ");
Intent. putExtra (Intent. EXTRA_TEXT, "shared content ");
Intent. setFlags (Intent. FLAG_ACTIVITY_NEW_TASK );
This. startActivity (Intent. createChooser (intent, "share "));

The android development process can be shared. The Demo is similar to the sharing on the Internet,

Getting started with Android: pan.baidu.com/..149558
Share Demo after purchase
Bytes -------------------------------------------------------------------------------------
Sina Weibo: Android mutual help Platform

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.