第三方登入分享微信qq
阿新 • • 發佈:2018-11-28
1參考https://developer.umeng.com/docs/66632/detail/66639
2Main
package com.umeng.soexample; import android.Manifest; 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 AppCompatActivity { private String TAG = this.getClass().getSimpleName(); private ImageView mImage; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mImage = findViewById(R.id.image); } 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(mImage); 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) { } }
3two
package com.umeng.soexample; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.ImageView; import com.squareup.picasso.Picasso; public class TwoActivity extends AppCompatActivity { 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); } }
4two_Xml
<?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=".TwoActivity"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/image_pic" /> </android.support.constraint.ConstraintLayout>
5main_XML
<?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=".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/image"/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>