第三方QQ登入--分享
activity_main
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <ImageView android:id="@+id/iv_login" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:src="@drawable/umeng_socialize_qq"/> <ImageView android:id="@+id/iv_share" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:src="@drawable/umeng_socialize_qzone"/> </LinearLayout> <TextView android:id="@+id/tv_result" android:text="易宸鋒" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </RelativeLayout>
MainActivity
package wanghuiqi.bawie.com.qq_login;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.shareboard.SnsPlatform;
import java.util.ArrayList;
import java.util.Map;
import java.util.Set;
public class MainActivity extends AppCompatActivity { private ImageView iv_login; private ImageView iv_share; private TextView tv_result; private UMShareAPI mUMShareAPI; //A.定義裝平臺的容器 public ArrayList<SnsPlatform> platforms = new ArrayList<SnsPlatform>(); private SHARE_MEDIA[] list = {SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); //A.遍歷第三方平臺 initPlatforms(); //A.獲取友盟物件 mUMShareAPI = UMShareAPI.get(MainActivity.this); //A.獲取是否授權 final boolean isauth = UMShareAPI.get(this).isAuthorize(this, platforms.get(0).mPlatform); //A.點選QQ的頭像,進行授權 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); } }); //B.分享的邏輯程式碼 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(); } }); } //A. 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()); } } } //A. UMAuthListener authListener = new UMAuthListener() { @Override public void onStart(SHARE_MEDIA platform) { //授權開始的回撥,可以用來處理等待框,或相關的文字提示 } @Override//授權成功時回撥 public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) { //獲取使用者授權後的資訊 Set<String> strings = data.keySet(); 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(); } }; //A. @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data); } //B.分享的邏輯程式碼 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(); } }; 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); } }
MyApplication
package wanghuiqi.bawie.com.qq_login;
import android.app.Application;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.UMShareAPI;
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
//Umeng初始化
UMShareAPI.get(this);
}
{
PlatformConfig.setQQZone("1106036236", "mjFCi0oxXZKZEWJs");
}
}
許可權
<!--友盟所用的許可權-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission
android:name="android.permission.READ_LOGS"
tools:ignore="ProtectedPermissions" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.GET_TASKS" />
android:name=".MyApplication"
清單檔案
<meta-data
android:name="UMENG_APPKEY"
android:value=" 5be634d6b465f5f5fd00042a">
</meta-data>
<!-- 友盟所需配置的Activity資訊-->
<!--注意:在自定義Application中的keyID必須要和清單檔案的AuthActivity下的scheme="tencent???"保持一致-->
<activity
android:name="com.umeng.qq.tencent.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="tencent1106036236"/>
</intent-filter>
</activity>
<activity
android:name="com.umeng.qq.tencent.AssistActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Translucent.NoTitleBar"/>
Moudle下的build
signingConfigs {
debug {
storeFile file('debug.keystore')
storePassword "android"
keyAlias "androiddebugkey"
keyPassword "android"
}
}
(https://img-blog.csdnimg.cn/20181115193842210.png)