One, foreword

Recently, I did a project that needed to integrate the sharing function. It was less than two months since the integration of the sharing function in the last project. I thought it could be done in minutes, but it took me half a day (it was a waste of life……). , the integration process still encountered some pits, resulting in the waste of some unnecessary time, so, sometimes learned, used things or have to record summary, easy to meet similar problems in the future to solve quickly, improve efficiency.

Second, the pit

For details, please refer to the ShareSDK quick integration document v3.x on the official website. Here are some considerations:

Step 1: Get ShareSDK;

Step 2: Import ShareSDK;

XML, AppId and AppSecret will be replaced by your own application, and BypassApproval will be set to “false” so that you can share correctly. If true, A shared image is a file. As long as it’s a formal environment share, BypassApproval should be false.

 <Wechat
        Id="4"
        SortId="1"
        AppId="xxx"
        AppSecret="xxx"
        userName="gh_afb25ac019c9"
        path="pages/index/index.html? id=1"
        BypassApproval="false"
        Enable="true" />Copy the code

Step 3: Configure Android manifest.xml.

3.1 There is a missing SD card reading permission in the official document, which needs to be added by myself. The permission should be used to read whether the mobile phone has a shared App (QQ, wechat, etc.) installed, and the 6.0+ system needs to dynamically apply for this permission:

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />Copy the code

3.2 Application configuration, the official explanation is given, but it is not easy to find, so I also summarize it:

Application configuration. PNG


<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <application
        android:name=".App"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"> <! --> <activity Android :name=".wxapi.WXEntryActivity"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:exported="true"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Translucent.NoTitleBar" />
        <activity
            android:name="cn.sharesdk.tencent.qq.ReceiveActivity"
            android:launchMode="singleTask"
            android:noHistory="true">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

                <data android:scheme="tencentxxx" />
            </intent-filter>
        </activity>

        <meta-data
            android:name="Mob-AppKey"
            android:value="xxx" />
        <meta-data
            android:name="Mob-AppSecret"
            android:value="xxx"/ > <! -- ShareSdk end -->Copy the code

3.3 There are three items that need to be replaced. Just replace “XXX” with relevant key of your app. Note that the first prefix must be kept “Tencent” :

    <data android:scheme="tencentxxx" />
       <meta-data
            android:name="Mob-AppKey"
            android:value="xxx" />
        <meta-data
            android:name="Mob-AppSecret"
            android:value="xxx" />Copy the code

At this point, the Android manifest.xml part is configured.

Step 4: Add share code;

 private void showShare() { OnekeyShare oks = new OnekeyShare(); / / close the sso authorization oks. DisableSSOWhenAuthorize (); Oks.setnotification (r.draable.ic_launcher, getString(r.string.app_name)); // Title title, evernote, email, message, wechat, Renren and Qzone use oks.setTitle(getString(r.sing.app_name)); // titleUrl is the title of the network link, only in renren and Qzone use oks.setTitleUrl("www.qq.com"); // Text is shared text. All platforms require this field oks.settext ("Text Text content www.qq.com"); // imagePath is the image address, which is supported by platforms other than Linked-in. // Never call this method if you don't use native images!! // oks.setImagePath("http://f1.sharesdk.cn/imgs/2014/02/26/owWpLZo_638x960.jpg");
        oks.setImageUrl("http://f1.sharesdk.cn/imgs/2014/02/26/owWpLZo_638x960.jpg"); // Url is only used in wechat (including friends and moments)."www.qq.com"); // comment is my comment on this share. Use oks.setcomment (only on renren.com and qzone)"I'm testing the comment text."); // site is the name of the site to share this content, only use oks.setsite (getString(r.sing.app_name)) in QQ space; // siteUrl is the address of the website to share this content. Only in Qzone use oks.setsiteurl ("www.qq.com");
        oks.setCallback(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                KLog.i("share onComplete...");
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
                KLog.i("share onError..." + throwable);
            }

            @Override
            public void onCancel(Platform platform, int i) {
                KLog.i("share onCancel..."); }}); // Start sharing GUI oks.show(getActivity()); }Copy the code

4.1 One pit in particular is this line of code:

// Don't call this method if you don't have a local image!! oks.setImagePath("http://f1.sharesdk.cn/imgs/2014/02/26/owWpLZo_638x960.jpg");Copy the code

Do not call this method when sharing to QQ friends if your pictures are not local pictures!! Do not call this method when sharing to QQ friends if your pictures are not local pictures!! Do not call this method when sharing to QQ friends if your pictures are not local pictures!! Otherwise share failure, this is the biggest pit THAT I share this time encounter, other share plus this line of code have no problem, share to QQ friend dead or alive not line, finally put this line of code after annotation finally succeed, in the heart immediately ten thousand grass mud horse pentum but pass……

4.2 when it comes to URL related, it is best to use formal link test first, because your link if it is the blacklist of the platform will also share failure, ensure that the sharing function is normal, and then replace back to their own links for testing. Such as

        oks.setTitleUrl("www.qq.com");Copy the code

4.3 wechat sharing must be signed to be successful; 4.4 Delete data from time to time. Pro test: If the signature of the wechat developer platform has been modified, it may be necessary to empty the wechat data or uninstall and reinstall to share successfully, so I ask you if you are afraid? So back up your data first.

4.5 Prepare two more phones for the test. Why do you say so? Because in this project I only responsible for your sharing, sharing of specific content is responsible for other children’s shoes, one of the children’s shoes in the use of the sharing function I wrote to QQ friends, life is not successful, share out the contents of all kinds of wrong, but another children’s shoes are has been successful, so I believe that the code should be ok, Then I asked him to debug it by himself. After all kinds of debugging and logging failed, I asked him to change his phone and finally succeeded! I asked him how successful, he said the new phone installed mobile QQ, the old phone did not install…… At that moment, I sprayed full screen blood, feeling the whole body was hollowed out, I finally realized: not afraid of god like opponents, afraid of not installed mobile phone QQ teammates also forced to share QQ friends!

Step 5: Finish;

Three, afterword.

Overall, using ShareSDK to implement third party sharing is relatively simple, but there are a few hicks that we need to pay attention to, and the integration will be smooth. If you have any questions, please leave comments and feedback. Finally, please attach the official guide to climb the pit. Hope the old driver can get out of the pit as soon as possible.