Android uses third-party SDK-allies for sharing

Posted by twigletmac on Sun, 09 Jun 2019 23:22:20 +0200

Today I want to write about the sharing of alliances. This period of time is useful for the project. Here is a brief introduction.

Links to the Alliance's official website: Click Open Link

First, to WeChat QQ, Weibo Open Platform Add Applications, Get key, List the links below:

QQ Open Platform Wechat Open Platform  Microblog Open Platform

To register on the Alliance's official website, add an application to get the only key, and download the SDK shared by the Alliance, we need to introduce it into our own project. I only used Weixin, QQ, Weibo, only selected some functions.



Add the corresponding res resource file to your project, and at this point, the preparation is complete.

Next, we begin to implement the sharing function. Alliance sharing has its own sharing interface, which can be used directly. The code is in the SDK integration document. Here we focus on using only the API provided by sharing to draw the sharing interface for our own projects.

Design sketch:


Click the Share button and a Share Diaog appears at the bottom of the screen.

The layout is very simple, no code to paste, write a ShareDIalog class inherits Dialog, introduces the layout file, transparent background settings, add an animation that appears and disappears.

  1. public class ShareDialog extends Dialog {  
  2.     private onClickback callback;  
  3.   
  4.     public ShareDialog(Context context, onClickback callback) {  
  5.         this(context, R.layout.share_dialog, R.style.my_dialog_style,  
  6.                 LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);  
  7.         this.callback = callback;  
  8.     }  
  9.   
  10.     public ShareDialog(final Context context, int layout, int style, int width,  
  11.             int height) {  
  12.         super(context, style);  
  13.         setContentView(layout);  
  14.         setCanceledOnTouchOutside(true);  
  15.         //Setting property values  
  16.         WindowManager.LayoutParams lp = getWindow().getAttributes();  
  17.         lp.width = width;  
  18.         lp.height = height;  
  19.         getWindow().setAttributes(lp);  
  20.           
  21.         setListener();  
  22.     }  
  23.   
  24.     //Setting Click Events  
  25.     private void setListener() {  
  26.         findViewById(R.id.tv_wx).setOnClickListener(  
  27.                 new android.view.View.OnClickListener() {  
  28.   
  29.                     @Override  
  30.                     public void onClick(View v) {  
  31.                         callback.onShare(1);  
  32.                         dismiss();  
  33.                     }  
  34.                 });  
  35.         findViewById(R.id.tv_wxp).setOnClickListener(  
  36.                 new android.view.View.OnClickListener() {  
  37.   
  38.                     @Override  
  39.                     public void onClick(View v) {  
  40.                         callback.onShare(2);  
  41.                         dismiss();  
  42.                     }  
  43.                 });  
  44.         findViewById(R.id.tv_wb).setOnClickListener(  
  45.                 new android.view.View.OnClickListener() {  
  46.   
  47.                     @Override  
  48.                     public void onClick(View v) {  
  49.                         callback.onShare(3);  
  50.                         dismiss();  
  51.                     }  
  52.                 });  
  53.         findViewById(R.id.tv_qq).setOnClickListener(  
  54.                 new android.view.View.OnClickListener() {  
  55.   
  56.                     @Override  
  57.                     public void onClick(View v) {  
  58.                         callback.onShare(4);  
  59.                         dismiss();  
  60.                     }  
  61.                 });  
  62.         findViewById(R.id.tv_qqz).setOnClickListener(  
  63.                 new android.view.View.OnClickListener() {  
  64.   
  65.                     @Override  
  66.                     public void onClick(View v) {  
  67.                         callback.onShare(5);  
  68.                         dismiss();  
  69.                     }  
  70.                 });  
  71.         findViewById(R.id.tv_cancal).setOnClickListener(  
  72.                 new android.view.View.OnClickListener() {  
  73.   
  74.                     @Override  
  75.                     public void onClick(View v) {  
  76.   
  77.                         dismiss();  
  78.                     }  
  79.                 });  
  80.     }  
  81.   
  82.     @Override  
  83.     public void show() {  
  84.         super.show();  
  85.         / Set up dialog Display animation  
  86.         getWindow().setWindowAnimations(R.style.dialogWindowAnim);  
  87.         //Set the display position to the bottom  
  88.         getWindow().setGravity(Gravity.BOTTOM);  
  89.     }  
  90.   
  91.     public interface onClickback {  
  92.   
  93.         abstract void onShare(int id);  
  94.     }  
  95. }  


anim animation files can be used directly by allies:

styles:

  1. <resources>  
  2.     <style name="dialogWindowAnim" parent="android:Animation" mce_bogus="1">    
  3.         <item name="android:windowEnterAnimation">@anim/umeng_socialize_slide_in_from_bottom</item>    
  4.         <item name="android:windowExitAnimation">@anim/umeng_socialize_slide_out_from_bottom</item>    
  5.     </style>      
  6. </resources>  
The window enters the animation umeng_socialize_slide_in_from_bottom.xml:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <translate xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:duration="300"  
  4.     android:fromXDelta="0.0%"  
  5.     android:fromYDelta="100.0%"  
  6.     android:toXDelta="0.0%"  
  7.     android:toYDelta="0.0%" />  

Window disappearance animation umeng_socialize_slide_out_from_bottom.xml:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <translate xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:duration="300"  
  4.     android:fromXDelta="0.0%"  
  5.     android:fromYDelta="0.0%"  
  6.     android:toXDelta="0.0%"  
  7.     android:toYDelta="100.0%" />  


The click of the share button is implemented in MainActivity:

  1. public class MainActivity extends Activity {  
  2.   
  3.     private Button btn_share;  
  4.     final UMSocialService mController = UMServiceFactory  
  5.             .getUMSocialService("com.umeng.share");  
  6.   
  7.     private String title = "share", content = "Share content...",  
  8.             url = "http://www.baidu.com";  
  9.     SnsPostListener mSnsPostListener = null;  
  10.   
  11.     @Override  
  12.     protected void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.activity_main);  
  15.         btn_share = (Button) findViewById(R.id.btn_share);  
  16.         //  
  17.         btn_share.setOnClickListener(new OnClickListener() {  
  18.   
  19.             @Override  
  20.             public void onClick(View v) {  
  21.                 //Open the Sharing Panel (customize the interface without using the alliance default)  
  22.                 openShareDialog();  
  23.             }  
  24.   
  25.         });  
  26.         mSnsPostListener = new SnsPostListener() {  
  27.             @Override  
  28.             public void onStart() {  
  29.             }  
  30.   
  31.             @Override  
  32.             public void onComplete(SHARE_MEDIA platform, int eCode,  
  33.                     SocializeEntity entity) {  
  34.                 if (eCode == 200) {  
  35.                     Toast.makeText(MainActivity.this"Sharing Success",  
  36.                             Toast.LENGTH_SHORT).show();  
  37.                 }  
  38.             }  
  39.         };  
  40.         mController.registerListener(mSnsPostListener);  
  41.         //Initialization of Sharing Platform  
  42.         init();  
  43.         //Initialization of Sharing Platform Content  
  44.         initShare();  
  45.     }  
  46.   
  47.     private void openShareDialog() {  
  48.         new ShareDialog(MainActivity.thisnew onClickback() {  
  49.   
  50.             @Override  
  51.             public void onShare(int id) {  
  52.                 switch (id) {  
  53.                 case 1//Wechat  
  54.                     shareToMedia(SHARE_MEDIA.WEIXIN);  
  55.                     break;  
  56.                 case 2//Circle of friends  
  57.                     shareToMedia(SHARE_MEDIA.WEIXIN_CIRCLE);  
  58.                     break;  
  59.                 case 3//Microblogging  
  60.                     shareToMedia(SHARE_MEDIA.SINA);  
  61.                     break;  
  62.                 case 4// qq  
  63.                     shareToMedia(SHARE_MEDIA.QQ);  
  64.                     break;  
  65.                 case 5//qq space  
  66.                     shareToMedia(SHARE_MEDIA.QZONE);  
  67.                     break;  
  68.                 }  
  69.             }  
  70.         }).show();  
  71.     }  
  72.   
  73.     private void shareToMedia(SHARE_MEDIA share_MEDIA) {  
  74.         mController.postShare(MainActivity.this, share_MEDIA, snsPostListener());  
  75.   
  76.     }  
  77.   
  78.     private SnsPostListener snsPostListener() {  
  79.         return mSnsPostListener;  
  80.     }  
  81.   
  82.     /** 
  83.      * Initialization of Sharing Platform 
  84.      */  
  85.     private void init() {  
  86.         mController.setShareContent(title);  
  87.         mController.setShareMedia(new UMImage(MainActivity.this,  
  88.                 R.drawable.ic_launcher));  
  89.         mController.getConfig().closeToast();  
  90.         String appID = "Wechat Application id";  
  91.         String appSecret = "########################";  
  92.         //Adding Wechat Platform  
  93.         UMWXHandler wxHandler = new UMWXHandler(MainActivity.this, appID,  
  94.                 appSecret);  
  95.         wxHandler.setTitle(title);  
  96.         wxHandler.addToSocialSDK();  
  97.         wxHandler.showCompressToast(false);  
  98.         //Adding Wechat Friendship Circle  
  99.         UMWXHandler wxCircleHandler = new UMWXHandler(MainActivity.this, appID,  
  100.                 appSecret);  
  101.         wxCircleHandler.setTitle(title);  
  102.         wxCircleHandler.setToCircle(true);  
  103.         wxCircleHandler.addToSocialSDK();  
  104.         wxCircleHandler.showCompressToast(false);  
  105.         String qqID = "QQ application id";  
  106.         String qqSecret = "##############";  
  107.         UMQQSsoHandler qqSsoHandler = new UMQQSsoHandler(MainActivity.this,  
  108.                 qqID, qqSecret);  
  109.         qqSsoHandler.addToSocialSDK();  
  110.         qqSsoHandler.setTitle(title);  
  111.         QZoneSsoHandler qZoneSsoHandler = new QZoneSsoHandler(  
  112.                 MainActivity.this, qqID, qqSecret);  
  113.         qZoneSsoHandler.addToSocialSDK();  
  114.         SinaSsoHandler sinaSsoHandler = new SinaSsoHandler();  
  115.         sinaSsoHandler.setShareAfterAuthorize(true);  
  116.         sinaSsoHandler.addToSocialSDK();  
  117.   
  118.     }  
  119.   
  120.     /** 
  121.      * Initialization of Sharing Platform Content 
  122.      */  
  123.     private void initShare() {  
  124.         //Setting up Wechat Friends to Share Content  
  125.         WeiXinShareContent weixinContent = new WeiXinShareContent();  
  126.         weixinContent.setShareContent(content);  
  127.         weixinContent.setTitle(title);  
  128.         weixinContent.setTargetUrl(url);  
  129.   
  130.         weixinContent.setShareImage(new UMImage(MainActivity.this,  
  131.                 R.drawable.ic_launcher));  
  132.         mController.setShareMedia(weixinContent);  
  133.         //Setting up Wechat Friendship Circle to Share Content  
  134.         CircleShareContent circleMedia = new CircleShareContent();  
  135.         circleMedia.setShareContent(content);  
  136.         circleMedia.setTitle(title);  
  137.         circleMedia.setShareImage(new UMImage(MainActivity.this,  
  138.                 R.drawable.ic_launcher));  
  139.         circleMedia.setTargetUrl(url);  
  140.         mController.setShareMedia(circleMedia);  
  141.         //Setting up QQ Sharing Content  
  142.         QQShareContent qqShareContent = new QQShareContent();  
  143.         qqShareContent.setShareContent(content);  
  144.         qqShareContent.setTitle(title);  
  145.         qqShareContent.setShareImage(new UMImage(MainActivity.this,  
  146.                 R.drawable.ic_launcher));  
  147.         qqShareContent.setTargetUrl(url);  
  148.         mController.setShareMedia(qqShareContent);  
  149.         //Setting up QQ space to share content  
  150.         QZoneShareContent qzone = new QZoneShareContent();  
  151.         qzone.setShareContent(content);  
  152.         qzone.setTargetUrl(url);  
  153.         qzone.setTitle(title);  
  154.         qzone.setShareImage(new UMImage(MainActivity.this,  
  155.                 R.drawable.ic_launcher));  
  156.         mController.setShareMedia(qzone);  
  157.         //Setting up Sina Weibo to share content  
  158.         SinaShareContent sinaShareContent = new SinaShareContent();  
  159.         sinaShareContent.setShareContent(content);  
  160.         sinaShareContent.setTargetUrl(url);  
  161.         sinaShareContent.setTitle(title);  
  162.         sinaShareContent.setShareImage(new UMImage(MainActivity.this,  
  163.                 R.drawable.ic_launcher));  
  164.         mController.setShareMedia(sinaShareContent);  
  165.     }  
  166.   
  167.     @Override  
  168.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  169.         super.onActivityResult(requestCode, resultCode, data);  
  170.         /** The following code must be added to use SSO authorization*/  
  171.         UMSsoHandler ssoHandler = mController.getConfig().getSsoHandler(  
  172.                 requestCode);  
  173.         if (ssoHandler != null) {  
  174.             ssoHandler.authorizeCallBack(requestCode, resultCode, data);  
  175.         }  
  176.     }  
  177.   
  178.     @Override  
  179.     protected void onDestroy() {  
  180.         super.onDestroy();  
  181.         //Close the listener  
  182.         if (mController != null) {  
  183.             mController.getConfig().cleanListeners();  
  184.         }  
  185.     }  
  186. }  
Click the Share button to open the Share Dialog. SnsPostListener is a shared callback interface.

@Override
public void onComplete(SHARE_MEDIA platform, int eCode,
SocializeEntity entity) {
if (eCode == 200) {
Toast.makeText(MainActivity.this,'Sharing Success', Toast.LENGTH_SHORT).show();
}
}

The first parameter of onComplete, SHARE_MEDIA platform, is a shared platform. The second parameter, int eCode, is the status code returned, and 200 is successful.

When sharing, which platform is the input, such as Wechat: SHARE_MEDIA.WEIXIN

  1. case 1//Wechat  
  2.     shareToMedia(SHARE_MEDIA.WEIXIN);  
  3.     break;  
  4.   
  5.   
  6. private void shareToMedia(SHARE_MEDIA share_MEDIA) {  
  7.     mController.postShare(MainActivity.this, share_MEDIA, snsPostListener());  
  8. }  

When the sharing interface is closed, you must remember to close the listener at the same time in the onDestroy method:
  1. @Override  
  2.     protected void onDestroy() {  
  3.         super.onDestroy();  
  4.         //Close the listener  
  5.         if (mController != null) {  
  6.             mController.getConfig().cleanListeners();  
  7.         }  
  8. }  

Notes are written in more detail, let alone elaborate.

More detailed links to the Alliance's official website: Click Open Link


PS:

If you have a Wechat or Wechat Friendship Circle integrated into your project and you need to share callbacks accurately, you need to register the following callback Activity in Android Manifest. xml.

<activity
   android:name=".wxapi.WXEntryActivity"
   android:theme="@android:style/Theme.Translucent.NoTitleBar"
   android:configChanges="keyboardHidden|orientation|screenSize"
   android:exported="true"
   android:screenOrientation="portrait" />

Note that there is no need to modify it, otherwise it may cause incorrect execution of Wechat callback and affect the login and sharing function of Wechat.

Then copy the wxapi folder in the platforms/weixin directory of the SDK package to the package directory configured in your AndroidMainFest.xml file application tag, and then modify the full path of WXEntry Activity.

  • Take our Demo project as an example

The package name of social_sdk_example is com.umeng.soexample, so copy the wxapi folder to com.umeng.soexample, and the complete path of WXEntryActivity is com.umeng.soexample.wxapi.WXEntryActivity.

Note WXEntry Activity. Java The complete path must be correct, otherwise the Weichat share callback can not be executed properly, and the Weichat login function can not be realized properly.

The content of the WXEntryActivity.java class can be empty, integrating the WXCallbackActivity class:

  1. import com.umeng.socialize.weixin.view.WXCallbackActivity;  
  2. public class WXEntryActivity extends WXCallbackActivity {  
  3.   
  4. }  

Part of Android Manifest. XML code:

  1. <uses-sdk  
  2.         android:minSdkVersion="10"  
  3.         android:targetSdkVersion="19" />  
  4.   
  5.     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />  
  6.     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />  
  7.     <uses-permission android:name="android.permission.READ_PHONE_STATE" />  
  8.     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  
  9.     <uses-permission android:name="android.permission.INTERNET" />  
  10.     <uses-permission android:name="android.permission.READ_LOGS" />  
  11.     <uses-permission android:name="android.permission.CALL_PHONE" />  
  12.     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />  
  13.     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />  
  14.     <uses-permission android:name="android.permission.GET_TASKS" />  
  15.     <uses-permission android:name="android.permission.SET_DEBUG_APP" />  
  16.     <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />  
  17.     <uses-permission android:name="android.permission.GET_ACCOUNTS" />  
  18.     <uses-permission android:name="android.permission.USE_CREDENTIALS" />  
  19.     <uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />  
  20.   
  21.     <application  
  22.         android:allowBackup="true"  
  23.         android:icon="@drawable/ic_launcher"  
  24.         android:label="@string/app_name"  
  25.         android:theme="@style/AppTheme" >  
  26.   
  27.         <activity  
  28.             android:name="com.huodao.hdphone.MainActivity"  
  29.             android:configChanges="orientation|keyboard"  
  30.             android:label="@string/app_name" >  
  31.             <intent-filter>  
  32.                 <action android:name="android.intent.action.MAIN" />  
  33.   
  34.                 <category android:name="android.intent.category.LAUNCHER" />  
  35.             </intent-filter>  
  36.         </activity>  
  37.   
  38.         <!-- Share Edit Pages -->  
  39.         <activity  
  40.             android:name="com.umeng.socialize.view.ShareActivity"  
  41.             android:configChanges="orientation|keyboard"  
  42.             android:launchMode="singleTask"  
  43.             android:noHistory="true"  
  44.             android:theme="@style/Theme.UMDialog"  
  45.             android:windowSoftInputMode="stateVisible|adjustResize" >  
  46.         </activity>  
  47.   
  48.         <!-- ############ QQ Space and QQ SSO Authorized Activity register ############ -->  
  49.         <activity  
  50.             android:name="com.tencent.tauth.AuthActivity"  
  51.             android:launchMode="singleTask"  
  52.             android:noHistory="true" >  
  53.             <intent-filter>  
  54.                 <action android:name="android.intent.action.VIEW" />  
  55.   
  56.                 <category android:name="android.intent.category.DEFAULT" />  
  57.                 <category android:name="android.intent.category.BROWSABLE" />  
  58.   
  59.                 <data android:scheme="tencentQQ application ID" />  
  60.             </intent-filter>  
  61.         </activity>  
  62.         <activity  
  63.             android:name="com.tencent.connect.common.AssistActivity"  
  64.             android:screenOrientation="portrait"  
  65.             android:theme="@android:style/Theme.Translucent.NoTitleBar" >  
  66.         </activity>  
  67.         <activity  
  68.             android:name=".wxapi.WXEntryActivity"  
  69.             android:configChanges="keyboardHidden|orientation|screenSize"  
  70.             android:exported="true"  
  71.             android:screenOrientation="portrait"  
  72.             android:theme="@android:style/Theme.Translucent.NoTitleBar" />  
  73.         <!-- ###################Add to UmengAppkey###################### -->  
  74.   
  75.   
  76.         <!-- Alliance Sharing -->  
  77.         <meta-data  
  78.             android:name="UMENG_APPKEY"  
  79.             android:value="Alliance application key" >  
  80.         </meta-data>  
  81.     </application>  

When integrating alliance sharing, we must pay attention to applying for application id and KEY values in various open platforms. Packet names and signatures officially issued by applications must not make mistakes, otherwise the sharing will not be successful.

Sharing effect map:

top

Topics: Android xml SDK encoding