1.需要的权限
//必须的权限
2.清单文件中注册
//QQ登录注册清单单文件
android:launchMode="singleTask"
android:noHistory="true" >
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:configChanges="orientation|keyboardHidden|screenSize"/>
//onCreat方法上面
//A.定义装平台的容器
public ArrayList
private SHARE_MEDIA[] list = {SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE};
private UMShareAPI mUMShareAPI;
if (Build.VERSION.SDK_INT >= 23) {//QQ需要申请写入权限
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);
}
initView();
//A.三方平台,添加到遍历的集合中
initPlatforms();
//A.获取UM的对象
mUMShareAPI = UMShareAPI.get(MainActivity.this);
//A.获取是否授权
final boolean isauth = UMShareAPI.get(this).isAuthorize(this, platforms.get(0).mPlatform);
iv_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (isauth) {
Toast.makeText(MainActivity.this, "授权成功", Toast.LENGTH_SHORT).show();
mUMShareAPI.deleteOauth(MainActivity.this, platforms.get(0).mPlatform, authListener);
} else {
mUMShareAPI.doOauthVerify(MainActivity.this, platforms.get(0).mPlatform, authListener);
}
mUMShareAPI.getPlatformInfo(MainActivity.this, platforms.get(0).mPlatform, authListener);
}
});
ImageView iv_share = (ImageView) findViewById(R.id.iv_share);
final UMImage image = new UMImage(MainActivity.this, "http://b.hiphotos.baidu.com/zhidao/pic/item/63d9f2d3572c11df28e42e30602762d0f703c2e8.jpg");
//网络图片
final UMImage imagelocal = new UMImage(this, R.mipmap.ic_launcher);
imagelocal.setThumb(new UMImage(this, R.mipmap.ic_launcher));
imagelocal.setTitle("哈哈");
iv_share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new ShareAction(MainActivity.this).withMedia(image).setPlatform(platforms.get(0).mPlatform)
.setCallback(shareListener).share();
new ShareAction(MainActivity.this).setPlatform(SHARE_MEDIA.QQ).withText("hello").setCallback(shareListener).share();
}
});
private void initPlatforms() { //A.集合清空
platforms.clear();
//A.通过for循环,把数组数据添加到集合中
for (SHARE_MEDIA e : list) {
if (!e.toString().equals(SHARE_MEDIA.GENERIC.toString())) {
platforms.add(e.toSnsPlatform());
}
}
}
//MainActivity中UMAuthListener()方法:
UMAuthListener authListener = new UMAuthListener() {
@Override
public void onStart(SHARE_MEDIA platform) {
//授权开始的回调,可以用来处理等待框,或相关的文字提示
}
@Override//授权成功时回调
public void onComplete(SHARE_MEDIA platform, int action, Map
//获取用户授权后的信息
Set
data.get("profile_image_url");
String temp = "";
for (String key : strings) {
temp = temp + key + " :" + data.get(key) + "\n";
}
tv_result.setText(temp);
}
@Override
public void onError(SHARE_MEDIA platform, int action, Throwable t) {
Toast.makeText(MainActivity.this, "失败:" + t.getMessage(), Toast.LENGTH_LONG).show();
}
@Override
public void onCancel(SHARE_MEDIA platform, int action) {
Toast.makeText(MainActivity.this, "取消了", Toast.LENGTH_LONG).show();
}
};
//MainActivity中UMShareListener方法
private UMShareListener shareListener = new UMShareListener() {
@Override
public void onStart(SHARE_MEDIA platform) {
}
@Override
public void onResult(SHARE_MEDIA platform) {
Toast.makeText(MainActivity.this, "成功了", Toast.LENGTH_LONG).show();
}
@Override
public void onError(SHARE_MEDIA platform, Throwable t) {
Toast.makeText(MainActivity.this, "失败" + t.getMessage(), Toast.LENGTH_LONG).show();
}
@Override
public void onCancel(SHARE_MEDIA platform) {
Toast.makeText(MainActivity.this, "取消了", Toast.LENGTH_LONG).show();
}
};
//MainActivity中onActivityResult方法和initview()
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 123) {
UMShareAPI.get(this).getPlatformInfo(this, SHARE_MEDIA.QQ, authListener);
}
}
private void initView() {
iv_login = (ImageView) findViewById(R.id.iv_login);
iv_share = (ImageView) findViewById(R.id.iv_share);
tv_result = (TextView) findViewById(R.id.tv_result);
}
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
/>
android:layout_width="wrap_content"
android:layout_height="wrap_content" />