Skip to content

Commit

Permalink
增加SocialHelper的使用例子
Browse files Browse the repository at this point in the history
appId不匹配,调用会失败,需要在自己的app中按需使用。
  • Loading branch information
liujinwei committed Jun 28, 2018
1 parent e7c2284 commit 32d42fb
Show file tree
Hide file tree
Showing 11 changed files with 357 additions and 3 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ allprojects {
```
dependencies {
...
compile 'com.github.arvinljw:SocialHelper:v1.0.7'
compile 'com.github.arvinljw:SocialHelper:v1.0.8'
}
```

*注:如果在该module中使用了v7包,那么可使用exclude命令移除本库的引用避免重复,gson也是一样,大体方法如下*

```
compile ('com.github.arvinljw:SocialHelper:v1.0.7'){
compile ('com.github.arvinljw:SocialHelper:v1.0.8'){
exclude group: 'com.android.support'
}
```
Expand Down Expand Up @@ -226,6 +226,12 @@ protected void onNewIntent(Intent intent) {

### Release Log

**v1.0.8:**

* 优化appId等参数为空是不抛异常,只打印日志,避免使用奔溃
* 删除SocialHelper中上个版本Deprecate了的方法
* 增加使用SocialHelper的[使用例子](https://github.com/arvinljw/SocialHelper/blob/master/app/src/main/java/net/arvin/socialhelper/sample/TestLoginShareActivity.java),但是无法直接使用,因为各种appId不匹配,需要在自己的app中按需使用。

**v1.0.7:**

* 更新微信,qq,微博sdk版本
Expand Down
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ android {
dependencies {
api fileTree(dir: 'libs', include: ['*.jar'])
api 'com.android.support:appcompat-v7:'.concat(projectSupportVersion)
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
// compile project(':socialhelper')
api ('com.github.arvinljw:SocialHelper:v1.0.7'){
exclude group: 'com.android.support'
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<activity android:name=".TestLoginShareActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
package net.arvin.socialhelper.sample;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import net.arvin.socialhelper.SocialHelper;
import net.arvin.socialhelper.callback.SocialLoginCallback;
import net.arvin.socialhelper.callback.SocialShareCallback;
import net.arvin.socialhelper.entities.QQShareEntity;
import net.arvin.socialhelper.entities.ShareEntity;
import net.arvin.socialhelper.entities.ThirdInfoEntity;
import net.arvin.socialhelper.entities.WBShareEntity;
import net.arvin.socialhelper.entities.WXShareEntity;
import net.arvin.socialhelper.sample.utils.SocialUtil;

import java.util.ArrayList;

public class TestLoginShareActivity extends AppCompatActivity implements View.OnClickListener, SocialLoginCallback, SocialShareCallback {
private String imgUrl = "https://upload-images.jianshu.io/upload_images/3157525-afe6f0ba902eb523.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240";
private String localImgUrl = "/storage/emulated/0/DCIM/Camera/IMG_20180422_113944.jpg";
private String title = "个人博客";
private String summary = "好好学习";
private String targetUrl = "https://arvinljw.github.io";

private SocialHelper socialHelper;

private TextView tvLoginInfo;
private RadioGroup rgShareInfo;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_login_share);
setTitle("测试SocialHelper");
socialHelper = SocialUtil.getInstance().socialHelper();
initView();
initEvent();
}

private void initView() {
tvLoginInfo = findViewById(R.id.tv_login_info);
rgShareInfo = findViewById(R.id.rg_share_info);
}

private void initEvent() {
findViewById(R.id.img_qq).setOnClickListener(this);
findViewById(R.id.img_wx).setOnClickListener(this);
findViewById(R.id.img_wb).setOnClickListener(this);
findViewById(R.id.img_qq_share).setOnClickListener(this);
findViewById(R.id.img_wx_share).setOnClickListener(this);
findViewById(R.id.img_wb_share).setOnClickListener(this);
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.img_qq:
socialHelper.loginQQ(this, this);
break;
case R.id.img_wx:
socialHelper.loginWX(this, this);
break;
case R.id.img_wb:
socialHelper.loginWB(this, this);
break;
case R.id.img_qq_share:
socialHelper.shareQQ(this, createQQShareEntity(), this);
break;
case R.id.img_wx_share:
socialHelper.shareWX(this, createWXShareEntity(), this);
break;
case R.id.img_wb_share:
socialHelper.shareWB(this, createWBShareEntity(), this);
break;
}
}

private ShareEntity createQQShareEntity() {
ShareEntity shareEntity = null;
int checkedRadioButtonId = rgShareInfo.getCheckedRadioButtonId();
switch (checkedRadioButtonId) {
case R.id.rb_img:
shareEntity = QQShareEntity.createImageInfo(localImgUrl, "ni6");
break;
case R.id.rb_img_text:
shareEntity = QQShareEntity.createImageTextInfo(title, targetUrl,
imgUrl, summary, "ni6");
break;
case R.id.rb_web:
//分享到qq空间,因为qq图文就包含了targetUrl所以比较常用
ArrayList<String> imgUrls = new ArrayList<>();
imgUrls.add(imgUrl);
shareEntity = QQShareEntity.createImageTextInfoToQZone(title, targetUrl,
imgUrls, summary, "ni6");
break;
}
return shareEntity;
}

private ShareEntity createWXShareEntity() {
ShareEntity shareEntity = null;
int checkedRadioButtonId = rgShareInfo.getCheckedRadioButtonId();
switch (checkedRadioButtonId) {
case R.id.rb_img:
shareEntity = WXShareEntity.createImageInfo(false, localImgUrl);
break;
case R.id.rb_img_text:
//微信图文是分开的,但是在分享到朋友圈的web中是可以有混合的
shareEntity = WXShareEntity.createImageInfo(false, R.mipmap.ic_launcher);
break;
case R.id.rb_web:
shareEntity = WXShareEntity.createWebPageInfo(false, targetUrl, R.mipmap.ic_launcher,
title, summary);
break;
}
return shareEntity;
}

private ShareEntity createWBShareEntity() {
ShareEntity shareEntity = null;
int checkedRadioButtonId = rgShareInfo.getCheckedRadioButtonId();
switch (checkedRadioButtonId) {
case R.id.rb_img:
shareEntity = WBShareEntity.createImageTextInfo(localImgUrl, title);
break;
case R.id.rb_img_text:
shareEntity = WBShareEntity.createImageTextInfo(R.mipmap.ic_launcher, title);
break;
case R.id.rb_web:
shareEntity = WBShareEntity.createWebInfo(targetUrl,
title, summary, R.mipmap.ic_launcher, "这是要说的内容");
break;
}
return shareEntity;
}

//用处:qq登录和分享回调,以及微博登录回调
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (data != null && socialHelper != null) {//qq分享如果选择留在qq,通过home键退出,再进入app则不会有回调
socialHelper.onActivityResult(requestCode, resultCode, data);
}
}

//用处:微博分享回调
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (socialHelper != null) {
socialHelper.onNewIntent(intent);
}
}

@Override
public void loginSuccess(ThirdInfoEntity info) {
tvLoginInfo.setText(toString(info));
}

@Override
public void socialError(String msg) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}

@Override
public void shareSuccess() {
Toast.makeText(this, "分享成功", Toast.LENGTH_SHORT).show();
}

private String toString(ThirdInfoEntity info) {
return "登录信息 = {" +
"unionId='" + info.getUnionId() + '\'' +
", openId='" + info.getOpenId() + '\'' +
", nickname='" + info.getNickname() + '\'' +
", sex='" + info.getSex() + '\'' +
", avatar='" + info.getAvatar() + '\'' +
", platform='" + info.getPlatform() + '\'' +
'}';
}
}
153 changes: 153 additions & 0 deletions app/src/main/res/layout/activity_test_login_share.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="12dp"
tools:context=".TestLoginShareActivity">

<TextView
android:id="@+id/tv_login_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第三方登录"
android:textColor="@color/black"
android:textSize="@dimen/sp_16"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
/>

<ImageView
android:id="@+id/img_qq"
style="@style/WW"
android:layout_marginTop="10dp"
android:padding="12dp"
android:src="@mipmap/img_qq_p"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/img_wx"
app:layout_constraintTop_toBottomOf="@+id/tv_login_title"
/>


<ImageView
android:id="@+id/img_wx"
style="@style/WW"
android:padding="12dp"
android:src="@mipmap/img_wx_p"
app:layout_constraintBottom_toBottomOf="@id/img_qq"
app:layout_constraintLeft_toRightOf="@+id/img_qq"
app:layout_constraintRight_toLeftOf="@+id/img_wb"
/>

<ImageView
android:id="@+id/img_wb"
style="@style/WW"
android:padding="12dp"
android:src="@mipmap/img_wb_p"
app:layout_constraintBottom_toBottomOf="@id/img_qq"
app:layout_constraintLeft_toRightOf="@+id/img_wx"
app:layout_constraintRight_toRightOf="parent"
/>

<TextView
android:id="@+id/tv_login_info"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="20dp"
android:padding="8dp"
android:text=""
android:textColor="@color/black"
app:layout_constraintBottom_toTopOf="@+id/divider"
app:layout_constraintTop_toBottomOf="@+id/img_qq"
/>

<View
android:id="@+id/divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#ccc"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.6"
/>

<TextView
android:id="@+id/tv_share_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="第三方分享"
android:textColor="@color/black"
android:textSize="@dimen/sp_16"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/divider"
/>

<ImageView
android:id="@+id/img_qq_share"
style="@style/WW"
android:layout_marginTop="10dp"
android:padding="12dp"
android:src="@mipmap/img_qq_p"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/img_wx_share"
app:layout_constraintTop_toBottomOf="@+id/tv_share_title"
/>


<ImageView
android:id="@+id/img_wx_share"
style="@style/WW"
android:padding="12dp"
android:src="@mipmap/img_wx_p"
app:layout_constraintBottom_toBottomOf="@id/img_qq_share"
app:layout_constraintLeft_toRightOf="@+id/img_qq_share"
app:layout_constraintRight_toLeftOf="@+id/img_wb_share"
/>

<ImageView
android:id="@+id/img_wb_share"
style="@style/WW"
android:padding="12dp"
android:src="@mipmap/img_wb_p"
app:layout_constraintBottom_toBottomOf="@id/img_qq_share"
app:layout_constraintLeft_toRightOf="@+id/img_wx_share"
app:layout_constraintRight_toRightOf="parent"
/>

<RadioGroup
android:id="@+id/rg_share_info"
android:layout_width="match_parent"
android:layout_height="0dp"
android:checkedButton="@+id/rb_img_text"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/img_qq_share">

<RadioButton
android:id="@+id/rb_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="纯图片"
/>

<RadioButton
android:id="@+id/rb_img_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="图文"
/>

<RadioButton
android:id="@+id/rb_web"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="网页"
/>

</RadioGroup>

</android.support.constraint.ConstraintLayout>
Binary file added app/src/main/res/mipmap-xxhdpi/img_qq_p.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/mipmap-xxhdpi/img_wb_p.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/mipmap-xxhdpi/img_wx_p.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="black">#000</color>
</resources>
4 changes: 4 additions & 0 deletions app/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="sp_16">16sp</dimen>
</resources>
Loading

0 comments on commit 32d42fb

Please sign in to comment.