activity_main页面
<?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”
tools:context=“com.umeng.soexample.MainActivity”>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="qqLogin"
android:text="QQ登入" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="qqshare"
android:text="QQ分享" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="weiXinLogin"
android:text="微信登录" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageV"/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
MainActivity页面
package com.umeng.soexample;
import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import com.squareup.picasso.Picasso;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareConfig;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import java.util.Iterator;
import java.util.Map;
public class MainActivity extends Activity {
private String TAG = this.getClass().getSimpleName();
private ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = findViewById(R.id.imageV);
}
public void qqshare(View view) {
//6.0以上主动请求权限
if (Build.VERSION.SDK_INT >= 23) {
String[] mPermissionList = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.CALL_PHONE, Manifest.permission.READ_LOGS, Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.SET_DEBUG_APP, Manifest.permission.SYSTEM_ALERT_WINDOW, Manifest.permission.GET_ACCOUNTS, Manifest.permission.WRITE_APN_SETTINGS};
ActivityCompat.requestPermissions(this, mPermissionList, 123);
}
UMImage umImage = new UMImage(MainActivity.this, R.mipmap.ic_launcher);
UMWeb web = new UMWeb("https://www.umeng.com/");
web.setTitle("This is music title");//标题
web.setThumb(umImage); //缩略图
web.setDescription("my description");//描述
//开始分享
new ShareAction(MainActivity.this).withText("hello").withMedia(web)
.withMedia(web)
.setDisplayList(SHARE_MEDIA.SINA, SHARE_MEDIA.QQ, SHARE_MEDIA.WEIXIN)
//.share();无面板分享
.open();
}
public void qqLogin(View view) {
//每次登录授权
UMShareConfig config = new UMShareConfig();
config.isNeedAuthOnGetUserInfo(true);
UMShareAPI.get(MainActivity.this).setShareConfig(config);
authorization(SHARE_MEDIA.QQ);
}
public void weiXinLogin(View view) {
authorization(SHARE_MEDIA.WEIXIN);
}
public void sinaLogin(View view) {
authorization(SHARE_MEDIA.SINA);
}
//授权
private void authorization(SHARE_MEDIA share_media) {
UMShareAPI.get(this).getPlatformInfo(this, share_media, new UMAuthListener() {
@Override
public void onStart(SHARE_MEDIA share_media) {
Log.d(TAG, "onStart " + "授权开始");
}
@Override
public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
Log.d(TAG, "onComplete " + "授权完成");
//sdk是6.4.4的,但是获取值的时候用的是6.2以前的(access_token)才能获取到值,未知原因
String uid = map.get("uid");
String openid = map.get("openid");//微博没有
String unionid = map.get("unionid");//微博没有
String access_token = map.get("access_token");
String refresh_token = map.get("refresh_token");//微信,qq,微博都没有获取到
String expires_in = map.get("expires_in");
String name = map.get("name");
String gender = map.get("gender");
String iconurl = map.get("iconurl");
Toast.makeText(getApplicationContext(), "name=" + name + ",gender=" + gender, Toast.LENGTH_SHORT).show();
Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> next = iterator.next();
if (next.getKey().equals("iconurl")) {
Picasso.with(MainActivity.this).load(next.getValue()).into(image);
Intent intent = new Intent(MainActivity.this, TwoActivity.class);
intent.putExtra("iconurl", iconurl);
intent.putExtra("name", name);
startActivity(intent);
finish();
}
}
//拿到信息去请求登录接口。。。
}
@Override
public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
Log.d(TAG, "onError " + "授权失败");
}
@Override
public void onCancel(SHARE_MEDIA share_media, int i) {
Log.d(TAG, "onCancel " + "授权取消");
}
});
}
//分享
private UMShareListener shareListener = new UMShareListener() {
/**
* @descrption 分享开始的回调
* @param platform 平台类型
*/
@Override
public void onStart(SHARE_MEDIA platform) {
}
/**
* @descrption 分享成功的回调
* @param platform 平台类型
*/
@Override
public void onResult(SHARE_MEDIA platform) {
Toast.makeText(MainActivity.this, "成功了", Toast.LENGTH_LONG).show();
}
/**
* @descrption 分享失败的回调
* @param platform 平台类型
* @param t 错误原因
*/
@Override
public void onError(SHARE_MEDIA platform, Throwable t) {
Toast.makeText(MainActivity.this, "失败" + t.getMessage(), Toast.LENGTH_LONG).show();
}
/**
* @descrption 分享取消的回调
* @param platform 平台类型
*/
@Override
public void onCancel(SHARE_MEDIA platform) {
Toast.makeText(MainActivity.this, "取消了", Toast.LENGTH_LONG).show();
}
};
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
//PublicStaticData.mShareAPI.onActivityResult(requestCode, resultCode, data);
}
//防止内存泄漏
@Override
protected void onDestroy() {
super.onDestroy();
UMShareAPI.get(this).release();
}
public void weixinshare(View view) {
}
}
activity_two页面
<?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”
tools:context=“com.umeng.soexample.TwoActivity”>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/image_pic"
/>
</android.support.constraint.ConstraintLayout>
TwoActivity页面
package com.umeng.soexample;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import com.squareup.picasso.Picasso;
public class TwoActivity extends Activity {
private ImageView image_pic;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
//初始化
initView();
//传头像
initPic();
}
private void initPic() {
Intent intent = getIntent();
String iconurl = intent.getStringExtra("iconurl");
Picasso.with(TwoActivity.this).load(iconurl).into(image_pic);
}
private void initView() {
image_pic = (ImageView) findViewById(R.id.image_pic);
}
}
App页面
package com.umeng.soexample;
import android.app.Application;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.socialize.PlatformConfig;
/**
- Created by 迁就 on 2018/11/18.
*/
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
UMConfigure.init(this,"5a12384aa40fa3551f0001d1"
,"umeng",UMConfigure.DEVICE_TYPE_PHONE,"");//58edcfeb310c93091c000be2 5965ee00734be40b580001a0
PlatformConfig.setWeixin("wxdc1e388c3822c80b", "3baf1193c85774b3fd9d18447d76cab0");
PlatformConfig.setQQZone("100424468", "c7394704798a158208a74ab60104f0ba");
}
}