Share android WeChat with friends and friends

Source: Internet
Author: User

Android friend sharing and friend circle sharing

Android friend sharing and friend circle sharing


The package name must be com. weixin.

WXEntryActivity

Package com. weixin. wxapi;

Import android. app. Activity;
Import android. OS. Bundle;

Import com. tencent. mm. sdk. openapi. BaseReq;
Import com. tencent. mm. sdk. openapi. BaseResp;
Import com. tencent. mm. sdk. openapi. IWXAPI;
Import com. tencent. mm. sdk. openapi. IWXAPIEventHandler;
Import com. tencent. mm. sdk. openapi. wxapifacloud;

Public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
// IWXAPI is an openapi for third-party apps and communications
Private IWXAPI api;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Api = wxapifacloud. createWXAPI (this, "wxf6a32cee6aa658e2", false );
Api. handleIntent (getIntent (), this );
Super. onCreate (savedInstanceState );
}

@ Override
Public void onResp (BaseResp resp ){
Switch (resp. errCode ){
Case BaseResp. ErrCode. ERR_ OK:
// Shared successfully
Break;
Case BaseResp. ErrCode. ERR_USER_CANCEL:
// Cancel sharing
Break;
Case BaseResp. ErrCode. ERR_AUTH_DENIED:
// Share rejected
Break;
}
}

@ Override
Public void onReq (BaseReq arg0 ){
// TODO Auto-generated method stub
}
}

MainActivity

Package com. weixin;

Import android. app. Activity;
Import android. graphics. Bitmap;
Import android. graphics. drawable. BitmapDrawable;
Import android. OS. Bundle;

Import com. tencent. mm. sdk. openapi. IWXAPI;
Import com. tencent. mm. sdk. openapi. SendMessageToWX;
Import com. tencent. mm. sdk. openapi. wxapifacloud;
Import com. tencent. mm. sdk. openapi. WXMediaMessage;
Import com. tencent. mm. sdk. openapi. WXWebpageObject;

Public class MainActivity extends Activity {
Private IWXAPI wxApi;

@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
// Instantiate
WxApi = wxapifacloud. createWXAPI (this, "wxf6a32cee6aa658e2 ");
WxApi. registerApp ("wxf6a32cee6aa658e2 ");
// Add the Code where you want to share it:
WechatShare (0); // share with friends
// WechatShare (1); // share with your friends
}

/**
* Share (only one example of sharing web pages is provided here. For more information, see the sample code on the official website)
*
* @ Param flag
* (0: Share with friends, 1: Share with friends)
*/
Private void wechatShare (int flag ){
WXWebpageObject webpage = new WXWebpageObject ();
Webpage. webpageUrl = "Enter the url here ";
WXMediaMessage msg = new WXMediaMessage (webpage );
Msg. title = "Enter the title here ";
Msg. description = "Enter content here ";
// Replace the image resource in your project
BitmapDrawable BMP draw = (BitmapDrawable) getResources (). getDrawable (
R. drawable. pai_logo );
Bitmap thumb = BMP draw. getBitmap ();
Msg. setThumbImage (thumb );

SendMessageToWX. Req req = new SendMessageToWX. Req ();
Req. transaction = String. valueOf (System. currentTimeMillis ());
Req. message = msg;
Req. scene = flag = 0? SendMessageToWX. Req. WXSceneSession
: SendMessageToWX. Req. WXSceneTimeline;
WxApi. sendReq (req );
}

}

 

Configuration file needs to be written


Package = "com. weixin"
Android: versionCode = "1"
Android: versionName = "1.0" type = "codeph" text = "/codeph">
Android: minSdkVersion = "8"
Android: targetSdkVersion = "19"/>
Android: allowBackup = "true"
Android: icon = "@ drawable/ic_launcher"
Android: label = "@ string/app_name"
Android: theme = "@ style/AppTheme">
Android: name = "com. weixin. MainActivity"
Android: label = "@ string/app_name">





Android: name = ". wxapi. WXEntryActivity"
Android: exported = "true"
Android: screenOrientation = "portrait"
Android: theme = "@ android: style/Theme. Translucent. NoTitleBar"/>
 

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.