Android integrated ShareSDK third-party sharing and login

This article records how to integrate ShareSDK on the Android platform to realize third-party login and sharing, and the pitfalls that may be encountered.

Mob official website

download

Select the platform you want to share as shown in the picture :
Write picture description here

integrated

Unzip the downloaded file, and then open it to QuickIntegrater.jar in ShareSDK for Android (Java environment variables need to be configured correctly), as shown in the figure:

Write picture description here

Fill in the project name and package name consistent with your own project, and then click OK to generate an eclipse project with the same name as the project file you filled in as shown in the figure below. We need to integrate it into Android Studio.

Write picture description here

In AS, copy the assets file to the directory at the same level as main, copy all the res and libs files to the corresponding libs folder, copy the folders starting with cn and com in src to the java directory, and finally in AS The entire project directory is as follows:

Write picture description here
The preparatory work is finally done here = =, the focus is on the ShareSDK.xml file, and the appids of all our platforms are configured in it. Not much to say here, the official website has a detailed introduction. We started to call third-party sharing and login in the project.

The first is third-party sharing

Initialize ShareSDk first


public class MyApplication extends Application {
    
    

    @Override
    public void onCreate() {
        super.onCreate();
        ShareSDK.initSDK(this);
    }
}

Permission configuration and activity declaration

<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.MANAGE_ACCOUNTS"/>
    <uses-permission android:name="android.permission.GET_ACCOUNTS"/>
    <!-- 蓝牙分享所需的权限 -->
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

        <activity
            android:name="com.mob.tools.MobUIShell"
            android:theme="@android:style/Theme.Translucent.NoTitleBar"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="stateHidden|adjustResize" >

            <intent-filter>
                <data android:scheme="tencent100371282" />
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.BROWSABLE" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>

            <!-- 调用新浪原生SDK,需要注册的回调activity -->
            <intent-filter>
                <action android:name="com.sina.weibo.sdk.action.ACTION_SDK_REQ_ACTIVITY" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>

            <!--集成line客户端登录授权,需要添如下格式的过滤器-->
            <intent-filter>
                <data android:scheme="line.1477692153" />
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.BROWSABLE" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>

        </activity>
        <!--微信分享回调 -->
        <activity
            android:name=".wxapi.WXEntryActivity"
            android:theme="@android:style/Theme.Translucent.NoTitleBar"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:exported="true"
            android:screenOrientation="portrait" />

Just select the platform you need and configure it.

Invoke third-party sharing where needed

private void showShare() {
 OnekeyShare oks = new OnekeyShare();
 //关闭sso授权
 oks.disableSSOWhenAuthorize(); 
 // title标题,印象笔记、邮箱、信息、微信、人人网、QQ和QQ空间使用
 oks.setTitle("标题");
 // titleUrl是标题的网络链接,仅在Linked-in,QQ和QQ空间使用
 oks.setTitleUrl("http://sharesdk.cn");
 // text是分享文本,所有平台都需要这个字段
 oks.setText("我是分享文本");
 //分享网络图片,新浪微博分享网络图片需要通过审核后申请高级写入接口,否则请注释掉测试新浪微博
 oks.setImageUrl("http://f1.sharesdk.cn/imgs/2014/02/26/owWpLZo_638x960.jpg");
 // imagePath是图片的本地路径,Linked-In以外的平台都支持此参数
 //oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
 // url仅在微信(包括好友和朋友圈)中使用
 oks.setUrl("http://sharesdk.cn");
 // comment是我对这条分享的评论,仅在人人网和QQ空间使用
 oks.setComment("我是测试评论文本");
 // site是分享此内容的网站名称,仅在QQ空间使用
 oks.setSite("ShareSDK");
 // siteUrl是分享此内容的网站地址,仅在QQ空间使用
 oks.setSiteUrl("http://sharesdk.cn");

// 启动分享GUI
 oks.show(this);
 }

At this point, the third-party sharing is basically completed.

Third party login

Take QQ login as an example



    private void login() {
        Platform platform = ShareSDK.getPlatform(this, QQ.NAME);
        // 如果用户已经授权过
        if (platform.isAuthValid()) {
            // 进行登录操作
            Toast.makeText(this, "已经登录过了", Toast.LENGTH_SHORT).show();
            return;
        }

        platform.setPlatformActionListener(new PlatformActionListener() {

            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                // 进行登录操作,要进行UI操作,请回调至主线程
                // 获取昵称
                   String nickname = obj.get("nickname").toString();
                // 获取用户头像
                String icon = obj.get("figureurl_qq_1").toString();
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
                // 如果授权错误清楚授权缓存的信息
                platform.removeAccount(true);
            }

            @Override
            public void onCancel(Platform platform, int i) {

            }
        });
        // 开启SSO授权,如QQ登录,当手机有QQ应用时,先从客户端申请授权,无则网页授权
        // false标识开启SSO授权
        platform.SSOSetting(false);
        platform.showUser(null);
        // 请求授权
        platform.authorize();
    }

It should be noted here that the login monitoring callback is not the main thread, so if there is a UI operation, we need to call it back to the main thread for operation. If you want to log in from other platforms, you only need to modify the corresponding platform name. To clear the authorization, log out and call platform.removeAccount(true) .

At this point, the third-party login and sharing has basically been completed. The third-party things are originally designed to make developers easy to use, and all development documents are the final learning materials. An error may be reported when packaging at the end, we also need to add in build.gradle:

    lintOptions{
        checkReleaseBuilds false
        abortOnError false
    }

For more detailed information, please visit the official website

Guess you like

Origin blog.csdn.net/Leavessilent/article/details/58597478