Android實(shí)現(xiàn)QQ的第三方登錄和分享
本文實(shí)例為大家分享了Android實(shí)現(xiàn)QQ的第三方登錄的具體代碼,供大家參考,具體內(nèi)容如下
MainActivity.java
/**
* 實(shí)現(xiàn)QQ的第三方登錄
* 1.搭建環(huán)境
(添加Jar包,添加Res圖片,布局,Values資源,添加權(quán)限,配置Activity信息,修改Key值,build簽名配置,Application初始化)
* 2.寫布局
* 3.登錄的代碼
* 注意:必須用真機(jī)測(cè)試
*/
public class MainActivity extends AppCompatActivity {
private ImageView iv_login;
private TextView tv_result;
//A.定義裝平臺(tái)的容器
public ArrayList<SnsPlatform> platforms = new ArrayList<SnsPlatform>();
private SHARE_MEDIA[] list = {SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE};
private UMShareAPI mUMShareAPI;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv_login = (ImageView) findViewById(R.id.iv_login);
tv_result = (TextView) findViewById(R.id.tv_result);
//A.三方平臺(tái),添加到遍歷的集合中
initPlatforms();
//A.獲取UM的對(duì)象
mUMShareAPI = UMShareAPI.get(MainActivity.this);
//A.獲取是否授權(quán)
final boolean isauth = UMShareAPI.get(this).isAuthorize(this, platforms.get(0).mPlatform);
//A.點(diǎn)擊QQ的頭像,進(jìn)行授權(quán)
iv_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (isauth){
Toast.makeText(MainActivity.this, "授權(quán)成功", 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");//網(wǎng)絡(luò)圖片
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.通過(guò)for循環(huán),把數(shù)組數(shù)據(jù)添加到集合中
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) {
//授權(quán)開始的回調(diào),可以用來(lái)處理等待框,或相關(guān)的文字提示
}
@Override//授權(quán)成功時(shí)回調(diào)
public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
//獲取用戶授權(quán)后的信息
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();
}
};
}
activity_main.xml
<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="ggg" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </RelativeLayout>
MyApp.java
public class MyAPP extends Application {
@Override
public void onCreate() {
super.onCreate();
//U盟SDK初始化
UMShareAPI.get(this);
}
{
PlatformConfig.setQQZone("1106036236","mjFCi0oxXZKZEWJs");
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.sn.qqlogin">
<!--友盟所用的權(quán)限-->
<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"/>
<application
android:name=".MyAPP"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<!--配置友盟上你應(yīng)用注冊(cè)的Key值,替換value-->
<meta-data
android:name="UMENG_APPKEY"
android:value="573f0e9267e58e8e48001545">
</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"/>
</application>
</manifest>
build.gradle
signingConfigs {
debug {
storeFile file('debug.keystore')
storePassword "android"
keyAlias "androiddebugkey"
keyPassword "android"
}
}
compile files('libs/SocialSDK_QQ_Simplify.jar')
compile files('libs/umeng_social_api.jar')
compile files('libs/umeng_social_net.jar')
compile files('libs/umeng_social_shareboard.jar')
compile files('libs/umeng_social_tool.jar')
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
簡(jiǎn)單介紹Android中Activity的四種啟動(dòng)模式
在Android中每個(gè)界面都是一個(gè)Activity,切換界面操作其實(shí)是多個(gè)不同Activity之間的實(shí)例化操作。本文給大家介紹Android中Activity的四種啟動(dòng)模式,需要的朋友參考下吧2016-04-04
Flutter學(xué)習(xí)教程之Route跳轉(zhuǎn)以及數(shù)據(jù)傳遞
這篇文章主要給大家介紹了關(guān)于Flutter學(xué)習(xí)教程之Route跳轉(zhuǎn)以及數(shù)據(jù)傳遞的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Flutter具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
Android實(shí)現(xiàn)紙飛機(jī)的簡(jiǎn)單操作
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)紙飛機(jī)的簡(jiǎn)單操作,類似于漂流瓶功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
Android WebView無(wú)法彈出軟鍵盤的原因及解決辦法
這篇文章主要介紹了Android WebView無(wú)法彈出軟鍵盤的原因及解決辦法的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06
刷新Activity中的scrollview示例(局部ui刷新)
代碼很簡(jiǎn)單,但是很實(shí)用,適合在一個(gè)Activity中要刷新局部的UI,比如在掃描一維碼的時(shí)候,要把每次掃描的結(jié)果都顯示在界面上2014-01-01
基于Android10渲染Surface的創(chuàng)建過(guò)程
這篇文章主要介紹了基于Android10渲染Surface的創(chuàng)建過(guò)程,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-08-08

