Android使用Google+分享功能

1.首先将<android-sdk-folder>/extras/google/google_play_services 导入到Eclipse中


我将google-play-services_lib复制到了新建的thirdlib文件中然后导入的,大家可以直接导入。


如果没有这个工程首先更新到最新的ADT,然后打开Android SDK Manager进行下载


2.然后将其作为 google-play-services_lib作为lib工程add到我们的工程上


wKioL1SGhlnyqdNwAAC8DMxOE0A603.jpg


3.主要的分享代码:


	//google+分享
	public void doGooglePlusShare(Activity mContext) {
		
		// 判断是否安装Google Service
		
		if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(mContext) == ConnectionResult.SUCCESS) {

		
				Intent shareIntent = new PlusShare.Builder(mContext).setType("text/plain")
						.setText("test share").setContentUrl(Uri.parse("www.baidu.com")).getIntent();

			    mContext.startActivityForResult(shareIntent,
						0);
		
		
		}else{
			
			Toast.makeText(mContext, "Please install Google Service", 0).show();
			
		}
		
	}

在Button的点击时间种调用这个方法

4.配置AndroidManifest.xml

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.googleplusshare.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>
           <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
    </application>
    
    <uses-permission android:name="android.permission.INTERNET"/>

</manifest>

5.大部分国产Android手机阉割了Google Service ,所以要想测试分享,需要安装这些东西。可以通过类似"谷歌应用下载器"的App来完成安装,安装成功后挂×××进行更新一下 

wKiom1SGgTqSdRg1AAGYRAxqPnE648.jpg

6.运行我们的程序,就是一个带Button的普通程序

wKioL1SGgpWhEKZZAAB_4jPbIQo299.jpg

点击Button进行测试

wKiom1SGgmnhILWvAABxW2qLVM8228.jpg

问题:这里的Google+分享是一个简单的分享,如果需要别的分享需求需要参见Google的官方guide进行

https://developers.google.com/+/mobile/android/share  进行相关设置


老样子,下面是源码的下载地址

Google+分享示例代码