友盟整合QQ第三方登入獲取顯示頭像
阿新 • • 發佈:2018-11-03
- 匯入jar
2.複製官方佈局裡面的程式碼======================
3.匯入依賴
compile 'com.umeng.sdk:common:latest.integration'
compile 'com.github.bumptech.glide:glide:3.7.0'
許可權
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
4.以下程式碼複製到清單檔案的Application中
<activity android:name="com.tencent.tauth.AuthActivity" android:launchMode="singleTask" android:noHistory="true" > <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="tencent100424468" /> </intent-filter> </activity> <activity android:name="com.tencent.connect.common.AssistActivity" android:theme="@android:style/Theme.Translucent.NoTitleBar" android:configChanges="orientation|keyboardHidden|screenSize"/>
5.寫佈局檔案
<ImageView
android:id="@+id/iv_head"
android:src="@mipmap/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
6.Activity主要程式碼
public class MainActivity extends AppCompatActivity { private UMShareAPI umShareAPI; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); umShareAPI = UMShareAPI.get(this); final ImageView iv_head = (ImageView) findViewById(R.id.iv_head); iv_head.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { UMAuthListener authListener = new UMAuthListener() { /** * @desc 授權開始的回撥 * @param platform 平臺名稱 */ @Override public void onStart(SHARE_MEDIA platform) { } /** * @desc 授權成功的回撥 * @param platform 平臺名稱 * @param action 行為序號,開發者用不上 * @param data 使用者資料返回 */ @Override public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) { Toast.makeText(MainActivity.this, "成功了", Toast.LENGTH_LONG).show(); Toast.makeText(MainActivity.this, "授權成功後的回撥資料,使用者資訊"+data, Toast.LENGTH_LONG).show(); String iconurl = data.get("iconurl"); Glide.with(MainActivity.this).load(iconurl).into(iv_head); } /** * @desc 授權失敗的回撥 * @param platform 平臺名稱 * @param action 行為序號,開發者用不上 * @param t 錯誤原因 */ @Override public void onError(SHARE_MEDIA platform, int action, Throwable t) { Toast.makeText(MainActivity.this, "失敗:" + t.getMessage(),Toast.LENGTH_LONG).show(); } /** * @desc 授權取消的回撥 * @param platform 平臺名稱 * @param action 行為序號,開發者用不上 */ @Override public void onCancel(SHARE_MEDIA platform, int action) { Toast.makeText(MainActivity.this, "取消了", Toast.LENGTH_LONG).show(); } }; umShareAPI.getPlatformInfo(MainActivity.this, SHARE_MEDIA.QQ, authListener); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data); } }
7.註冊App
public class App extends Application{
@Override
public void onCreate() {
super.onCreate();
UMConfigure.init(this,"5a12384aa40fa3551f0001d1"
,"umeng",UMConfigure.DEVICE_TYPE_PHONE,"");//58edcfeb310c93091c000be2 5965ee00734be40b580001a0
PlatformConfig.setQQZone("100424468", "c7394704798a158208a74ab60104f0ba");
UMConfigure.setLogEnabled(true);
}
}
不要忘記註冊APP;