android第三方登录以及第三方分享

Android 很多应用中都会涉及到第三方登录或者第三方分享等功能。

友盟和百度的产品对于解决我们现有的问题还是非常棒的,多余的话不说了,直接上干货.....

在百度开放云平台找到第三方登录,网址如下所示

http://developer.baidu.com/frontia/sociallogin

        选中第三方用户登录以及第三方分享

android第三方登录以及第三方分享_第1张图片

       然后选择生成SDK

       android第三方登录以及第三方分享_第2张图片

       下载SDK并解压,解压之后的目录如下所示:

      android第三方登录以及第三方分享_第3张图片

        把libs目录中的文件拷贝到新建的Android项目中,如下图所示:

android第三方登录以及第三方分享_第4张图片


             项目中的核心代码如下所示:

     布局文件 activity_main.xml

     

<RelativeLayout 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"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/status"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="状态" />
	<TextView
        android:id="@+id/info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/msg"
        android:layout_marginTop="126dp"
        android:text="信息" />
    <Button
        android:id="@+id/login"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:onClick="onBtnClick"
        android:text="第三方登录" />

    <Button
        android:id="@+id/share"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/login"
        android:layout_alignParentLeft="true"
        android:onClick="onBtnClick"
        android:text="第三方分享" />

</RelativeLayout>
        

       MainActivity.java

package com.baidu.test;

import com.baidu.frontia.Frontia;
import com.baidu.frontia.FrontiaUser;
import com.baidu.frontia.api.FrontiaAuthorization;
import com.baidu.frontia.api.FrontiaSocialShare;
import com.baidu.frontia.api.FrontiaSocialShareContent;
import com.baidu.frontia.api.FrontiaSocialShareListener;
import com.baidu.frontia.api.FrontiaAuthorization.MediaType;
import com.baidu.frontia.api.FrontiaAuthorizationListener.AuthorizationListener;
import com.baidu.frontia.api.FrontiaAuthorizationListener.UserInfoListener;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	private TextView mResultTextView,info;
	private FrontiaAuthorization mAuthorization;  //登录校验
	boolean isInit=false;   //校验通过的标识
    private FrontiaSocialShare mSocialShare;	//社交分享
	private FrontiaSocialShareContent mImageContent = new FrontiaSocialShareContent(); //社交分享的内容
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mResultTextView=(TextView) findViewById(R.id.status);
		info=(TextView) findViewById(R.id.info);
	    isInit = Frontia.init(getApplicationContext(), "ihNyvzqUAbrHl8V4ts5VZnew");
		mAuthorization=Frontia.getAuthorization();
		mSocialShare=Frontia.getSocialShare(); 
//		
		mSocialShare.setContext(this);
//		mSocialShare.setClientId(MediaType.SINAWEIBO.toString(), "ihNyvzqUAbrHl8V4ts5VZnew");
//		mSocialShare.setClientName(MediaType.SINAWEIBO.toString(), "百度");	
		mImageContent.setTitle("百度开发中心");
		mImageContent.setContent("欢迎使用百度社会化分享组件,相关问题请邮件[email protected]");
		mImageContent.setLinkUrl("http://developer.baidu.com/");
		mImageContent.setImageUri(Uri.parse("http://apps.bdimg.com/developer/static/04171450/developer/images/icon/terminal_adapter.png"));
	
	}

	public void onBtnClick(View view){
		int id=view.getId();
		switch (id) {
		case R.id.login:
				login();   //第三方登录
			break;

		case R.id.share:
			   share();
			break;
		}
	}
	public void login(){		
		if(isInit){// 初始化成功
			mAuthorization.enableSSO(MediaType.SINAWEIBO.toString(),"ihNyvzqUAbrHl8V4ts5VZnew");
			mAuthorization.authorize(this,
					FrontiaAuthorization.MediaType.SINAWEIBO.toString(),
					new AuthorizationListener() {
						@Override
						public void onSuccess(FrontiaUser result) {
						    Frontia.setCurrentAccount(result);
							
								String log = "social id: " + result.getId() + "\n"
	                                    + "token: " + result.getAccessToken() + "\n"
	                                    + "expired: " + result.getExpiresIn();
								mResultTextView.setText(log);
								userinfo(MediaType.SINAWEIBO.toString()); // 登录成功获得用户信息
							
						}
						@Override //登录失败
						public void onFailure(int errorCode, String errorMessage) {
							if (null != mResultTextView) {
								mResultTextView.setText("errCode:" + errorCode
										+ ", errMsg:" + errorMessage);
							}
						}

						@Override
						public void onCancel() {
							if (null != mResultTextView) {
								mResultTextView.setText("cancel");
							}
						}

					});
		   }
	   }
    private void userinfo(String accessToken) {
	  mAuthorization.getUserInfo(accessToken, new UserInfoListener() {
		@Override
		public void onSuccess(FrontiaUser.FrontiaUserDetail result) {
			if (null != mResultTextView) {
				String resultStr = "username:" + result.getName() + "\n"
						+ "birthday:" + result.getBirthday() + "\n"
						+ "city:" + result.getCity() + "\n"
						+ "province:" + result.getProvince() + "\n"
						+ "sex:" + result.getSex() + "\n"
						+ "pic url:" + result.getHeadUrl() + "\n";
				
				info.setText(resultStr);
			}
		}

		@Override
		public void onFailure(int errCode, String errMsg) {
			if (null != mResultTextView) {
				info.setText("errCode:" + errCode
						+ ", errMsg:" + errMsg);
			}
		}
		
	  });
    }
	public void share(){
		mSocialShare.share(mImageContent,MediaType.BATCHSHARE.toString(),new ShareListener(),true);
	}
	private class ShareListener implements FrontiaSocialShareListener {

		@Override
		public void onSuccess() {
			Toast.makeText(MainActivity.this, "success", 1).show();
		}

		@Override
		public void onFailure(int errCode, String errMsg) {
			Log.d("Test","share errCode "+errCode);
		}

		@Override
		public void onCancel() {
			Log.d("Test","cancel ");
		}
		
	}
	
}
Manifest.xml 配置文件

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.baidu.test"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.BROADCAST_STICKY" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER" />
    <uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
    <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="10" />

    <application
        android:name="com.baidu.frontia.FrontiaApplication"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.baidu.test.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

     
    </application>

</manifest>
           项目最终运行的结果如下:

        android第三方登录以及第三方分享_第5张图片

  android第三方登录以及第三方分享_第6张图片

     android第三方登录以及第三方分享_第7张图片


   在MainAcitivity中 

  Frontia.init(getApplicationContext(), "ihNyvzqUAbrHl8V4ts5VZnew"); 第二个参数应该是你的应用密钥。




你可能感兴趣的:(android第三方登录以及第三方分享)