安卓学习日记——ShareSDK

官网:mob.com
在这里插入图片描述
具体配置步骤可以参考官网的开发文档

package com.example.sharesdk;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import com.mob.MobSDK;

import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MobSDK.init(this);
    }
    public void shareClick(View v){
        showShare();
    }
    private void showShare() {

        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();

// 分享时Notification的图标和文字  2.5.9以后的版本不调用此方法
        //oks.setNotification(R.drawable.ic_launcher, getString(R.string.app_name));
        // title标题,印象笔记、邮箱、信息、微信、人人网和QQ空间使用
        oks.setTitle(getString(R.string.app_name));
        // titleUrl是标题的网络链接,仅在人人网和QQ空间使用
        oks.setTitleUrl("http://www.baidu.com");
        // text是分享文本,所有平台都需要这个字段
        oks.setText("当你还不胖的时候,别人说,你好瘦啊赶紧增肥。当你微胖时,别人说,一点都不胖啊,多吃点。当你很胖的时候,别人说,这样肉肉的好看,不用减肥。只有当你积重难返,减肥比戒毒还难时,别人才会说,好吧你该减肥了。所以,千万别听旁人的话,对自己不够狠,再聪明也没法掌握命运。");
        // imagePath是图片的本地路径,Linked-In以外的平台都支持此参数
        //oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
        oks.setImageUrl("http://img5.duitang.com/uploads/item/201404/15/20140415221202_BkvGQ.thumb.700_0.jpeg");

        // url仅在微信(包括好友和朋友圈)中使用
        oks.setUrl("http://www.baidu.com");
        // comment是我对这条分享的评论,仅在人人网和QQ空间使用
        oks.setComment("我是测试评论文本");
        // site是分享此内容的网站名称,仅在QQ空间使用
        oks.setSite(getString(R.string.app_name));
        // siteUrl是分享此内容的网站地址,仅在QQ空间使用
        oks.setSiteUrl("http://www.baidu.com");

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

    @Override
    protected void onDestroy() {
        super.onDestroy();
//    ShareSDK.stopSDK(this);
    }
}

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.sharesdk">
    <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" />

    <application
        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=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <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>
        </activity>
        <!--微信分享回调 -->
        <activity
            android:name="cn.sharesdk.demo.wxapi.WXEntryActivity"
            android:theme="@android:style/Theme.Translucent.NoTitleBar"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:exported="true"
            android:screenOrientation="portrait" />
    </application>

</manifest>

在这里插入图片描述
第三方登录
这边以新浪微博为例,先去新浪微博开发者平台注册成为开发者
open.weibo.com
然后在里面创建应用,获取到应用的AppKey和AppSecret,授权管理获得RedirectUrl
将ShareSDK.xml里面新浪微博的相关数据进行替换
在这里插入图片描述

 public void sinaweiboLoginClick(View v){
//获取平台
            Platform plat= ShareSDK.getPlatform( SinaWeibo.NAME);
            plat.setPlatformActionListener(new PlatformActionListener() {
                @Override
                public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                    System.out.println(hashMap);
                    String name= (String) hashMap.get("name");
                    String description= (String) hashMap.get("description");
                }
                @Override
                public void onError(Platform platform, int i, Throwable throwable) {

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

                }
            });
            //要数据还是要功能
//判断指定平台是否已经完成授权
      /*  if (plat.isValid())
        {
            String userId=plat.getDb().getUserId();
            if (userId!=null)
            {
                System.out.println(plat.getName()+"---"+userId);
                plat.showUser(null);
                return;
            }
        }*/
            //true不适用SSO授权,false使用SSO授权
            plat.SSOSetting(true);
            //’“要功能,不要数据(用户)
            //plat.authorize();
            //获取用户资料
            //“要数据,不要功能
            plat.showUser(null);
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值