1. 程式人生 > >Android APP分享功能實現 .

Android APP分享功能實現 .

安卓分享功能也可以使用mob提供的SDK來開發分享功能,簡訊驗證功能。http://www.mob.com/#/

        由於公司專案的需要,要實現在專案中使用第三方授權登入以及分享文字和圖片等這樣的效果,幾經波折,查閱了一番資料,做了一個Demo。實現起來的效果還是不錯的,不敢獨享,決定寫一個總結的教程,供大家互相交流、學習和參考,博主只求能和大家共同進步。希望能多多支援!

        這篇文章中,我們使用到了Share SDK,它是為iOS、Android、WP8的APP提供社會化功能的一個元件,目前支援如QQ、微信、新浪微博、騰訊微博、開心網、人人網、豆瓣、網易微博、搜狐微博、facebook、twitter、google+等國內外主流社交平臺。

一、實現的效果圖

主介面效果圖                                                                         

  

                           授權登入頁面效果圖

  

                        點選分享按鈕彈出分享分享介面                               

     

      

 

二、專案結構目錄

三、編碼前的準備工作

1、獲取Libs    

   

      Libs包含ShareSDK的類庫,具體包括三個資料夾,分別是ShareSDK的全域性依賴庫、ShareSDK當前支援的所有平臺工具庫和ShareSDK視覺化UI的一些支援庫。“全域性依賴庫”是整合ShareSDK的基礎,ShareSDK的任何平臺都依賴於這個庫,而“ShareSDK-GUI”提供的是一個測欄控制元件和一個快捷分享工具,以方便讀者更快速地整合ShareSDK。

2、匯入Libs

       <1> 將“Libs\Global-Dependences”下的jar包複製到您的libs目錄下。

       <2> 從“Libs\Platforms”中選擇您感興趣的平臺,比方說“新浪微博”、“QQ空間”、“騰訊微博”等,複製相應的壓縮包到你專案的libs目錄下並解壓

       <3> 如果你決定使用shareSDK提供的快捷分享工具,還需要複製“Libs\ShareSDK-GUI”中複製“cn.sharesdk.onekeyshare.jar”到你的專案中。

       <4> 一般來說,ADT會自動將你新增到libs目錄下的jar包新增到“Android Dependencies”中。但是如果你的開發環境不能自動載入ShareSDK的jar包到你的專案中,那麼只能手動新增,如下圖所示:

四、詳細的編碼實現

1、ShareSdK使用統一的格式管理你在不同平臺上註冊的開發者資訊。這些資訊都存放在專案的“assets/ShareSDKDevInfor.xml”中,ShareSDKDevInfor.xml:

[html] view plain copy print?在CODE上檢視程式碼片派生到我的程式碼片

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <DevInfor>  
  3.     <!--說明:        
  4.         1、表格中的第一項  
  5.             <ShareSDK AppKey="api20" />  
  6.         是必須的,其中的AppKey是你在Share SDK上註冊的開發者帳號的AppKey  
  7.         2、所有整合到你專案的平臺都應該為其在表格中填寫相對應的開發者資訊,以新浪微博為例:  
  8.             <SinaWeibo  
  9.                 SortId="此平臺在分享列表中的位置,由開發者自行定義,可以是任何整型數字,數值越大越靠後"  
  10.                 AppKey="填寫你在新浪微博上註冊的AppKey"  
  11.                 AppSecret="填寫你在新浪微博上註冊到的AppKey"  
  12.                 Id="自定義欄位,整形,用於你專案中對此平臺的識別符"  
  13.                 RedirectUrl="填寫你在新浪微博上註冊的RedirectUrl" />         
  14.         各個平臺註冊應用資訊的地址如下:  
  15.             新浪微博:http://open.weibo.com  
  16.             騰訊微博:http://dev.t.qq.com  
  17.             QQ空間:http://connect.qq.com/intro/login/  
  18.             網易微博:http://open.t.163.com  
  19.             搜狐微博:http://open.t.sohu.com  
  20.             豆瓣:http://developers.douban.com  
  21.             人人網:http://dev.renren.com  
  22.             開心網:http://open.kaixin001.com  
  23.             Instapaper:http://www.instapaper.com/main/request_oauth_consumer_token  
  24.             有道雲筆記:http://note.youdao.com/open/developguide.html#app  
  25.             facebook:https://developers.facebook.com  
  26.             twitter:https://dev.twitter.com  
  27.             搜狐隨身看:https://open.sohu.com  
  28.             QQ好友分享:http://mobile.qq.com/api/  
  29.             微信:http://open.weixin.qq.com-->  
  30.     <ShareSDK   
  31.         AppKey = "api20"/> <!-- AppKey="104972cdd48" "23a9371d3a8"-->  
  32.     <SinaWeibo  
  33.         SortId="1"  
  34.         AppKey="3201194191"  
  35.         AppSecret="0334252914651e8f76bad63337b3b78f"  
  36.         Id="1"  
  37.         RedirectUrl="http://appgo.cn" />  
  38.     <TencentWeibo  
  39.         SortId="2"  
  40.         AppKey="801307650"  
  41.         AppSecret="ae36f4ee3946e1cbb98d6965b0b2ff5c"  
  42.         RedirectUri="http://sharesdk.cn"  
  43.         Id="2" />  
  44.     <QZone  
  45.         SortId="3"  
  46.         AppId="100371282"  
  47.         AppKey="aed9b0303e3ed1e27bae87c33761161d"  
  48.         Id="3"  
  49.         RedirectUrl="http://www.shareSDK.cn" />  
  50.     <Renren  
  51.         SortId="4"  
  52.         AppId="226427"  
  53.         ApiKey="fc5b8aed373c4c27a05b712acba0f8c3"  
  54.         Id="4"  
  55.         SecretKey="f29df781abdd4f49beca5a2194676ca4" />    
  56. </DevInfor>  
<?xml version="1.0" encoding="utf-8"?>
<DevInfor>
    <!--說明:   	
    	1、表格中的第一項
    		<ShareSDK AppKey="api20" />
    	是必須的,其中的AppKey是你在Share SDK上註冊的開發者帳號的AppKey
    	
    	2、所有整合到你專案的平臺都應該為其在表格中填寫相對應的開發者資訊,以新浪微博為例:
    	    <SinaWeibo
                SortId="此平臺在分享列表中的位置,由開發者自行定義,可以是任何整型數字,數值越大越靠後"
                AppKey="填寫你在新浪微博上註冊的AppKey"
                AppSecret="填寫你在新浪微博上註冊到的AppKey"
                Id="自定義欄位,整形,用於你專案中對此平臺的識別符"
                RedirectUrl="填寫你在新浪微博上註冊的RedirectUrl" />   	  
    	
    	各個平臺註冊應用資訊的地址如下:
	    	新浪微博:http://open.weibo.com
			騰訊微博:http://dev.t.qq.com
			QQ空間:http://connect.qq.com/intro/login/
			網易微博:http://open.t.163.com
			搜狐微博:http://open.t.sohu.com
			豆瓣:http://developers.douban.com
			人人網:http://dev.renren.com
			開心網:http://open.kaixin001.com
			Instapaper:http://www.instapaper.com/main/request_oauth_consumer_token
			有道雲筆記:http://note.youdao.com/open/developguide.html#app
			facebook:https://developers.facebook.com
			twitter:https://dev.twitter.com
			搜狐隨身看:https://open.sohu.com
			QQ好友分享:http://mobile.qq.com/api/
			微信:http://open.weixin.qq.com-->
			  
    <ShareSDK 
        AppKey = "api20"/> <!-- AppKey="104972cdd48" "23a9371d3a8"-->
    
    <SinaWeibo
        SortId="1"
        AppKey="3201194191"
        AppSecret="0334252914651e8f76bad63337b3b78f"
        Id="1"
        RedirectUrl="http://appgo.cn" />

    <TencentWeibo
        SortId="2"
        AppKey="801307650"
        AppSecret="ae36f4ee3946e1cbb98d6965b0b2ff5c"
        RedirectUri="http://sharesdk.cn"
        Id="2" />

    <QZone
        SortId="3"
        AppId="100371282"
        AppKey="aed9b0303e3ed1e27bae87c33761161d"
        Id="3"
        RedirectUrl="http://www.shareSDK.cn" />
       
    <Renren
        SortId="4"
        AppId="226427"
        ApiKey="fc5b8aed373c4c27a05b712acba0f8c3"
        Id="4"
        SecretKey="f29df781abdd4f49beca5a2194676ca4" />  
       
</DevInfor>

2、配置AndroidManifest.xml,不同的整合度需要在AndroidManifest.xml中新增的內容也不一樣。但是首先你需要新增下面的許可權列表:

[html] view plain copy print?在CODE上檢視程式碼片派生到我的程式碼片

  1. <uses-permission android:name="android.permission.INTERNET" />  
  2.     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />  
  3.     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />  
  4.     <uses-permission android:name="android.permission.WRITE_APN_SETTINGS" />  
  5.     <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />  
  6.     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  
  7.     <uses-permission android:name="android.permission.READ_PHONE_STATE" />  
<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_APN_SETTINGS" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />


      這些許可權將允許你的專案和ShareSDK獲取連線網路的許可權、獲取你的裝置網路狀態的許可權、實現https安全連線的許可權、讀取手機裝置狀態的許可權和儲存必要配置的許可權。一般來說,即便不整合ShareSDK,大部分的專案也都會註冊申請這些許可權。

        注意:大家在加入這個"android.permission.WRITE_APN_SETTINGS"許可權的時候,可能有些讀者的編譯器會報錯,博主就遇到了這樣的情況,這個是ADT Lint工具的問題。

       解決的辦法是:依照下面的路徑“Window —> Preferences —> android—> lint error checking”開啟lint的配置頁面,然後去掉頁面頂部的兩個勾選,之後再clean專案就能處理。如下圖所示:



3、其次,為了授權操作可以順利完成,需要在application下注冊下面的Activity:

[html] view plain copy print?在CODE上檢視程式碼片派生到我的程式碼片

  1. <activity  
  2.             android:name="cn.sharesdk.framework.AuthorizeActivity"  
  3.             android:configChanges="keyboardHidden|orientation"  
  4.             android:screenOrientation="portrait"  
  5.             android:theme="@android:style/Theme.Translucent.NoTitleBar"  
  6.             android:windowSoftInputMode="stateHidden|adjustResize" >            
  7.         </activity>  
<activity
            android:name="cn.sharesdk.framework.AuthorizeActivity"
            android:configChanges="keyboardHidden|orientation"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Translucent.NoTitleBar"
            android:windowSoftInputMode="stateHidden|adjustResize" >          
        </activity>

AuthorizeActivity的路徑是固定的,一定要在“cn.sharesdk.framework”下,因為他在Share-Core包中。

4、添加布局頁面,首先是主介面的佈局頁面,activity_main.xml:

[html] view plain copy print?在CODE上檢視程式碼片派生到我的程式碼片

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent" >  
  5.     <Button  
  6.         android:id="@+id/btnLogin"  
  7.         android:layout_width="fill_parent"  
  8.         android:layout_height="44dp"  
  9.         android:layout_above="@+id/btnShareAllGui"  
  10.         android:layout_centerHorizontal="true"  
  11.         android:layout_margin="5dp"  
  12.         android:background="@drawable/btn_back"  
  13.         android:text="使用者授權登入"   
  14.         android:textSize="16dp"/>  
  15.     <Button  
  16.         android:id="@+id/btnShareAllGui"  
  17.         android:layout_width="fill_parent"  
  18.         android:layout_height="44dp"  
  19.         android:layout_above="@+id/btnShareAll"  
  20.         android:layout_margin="5dp"  
  21.         android:background="@drawable/btn_back"  
  22.         android:text="分享全部(有分享介面)"  
  23.         android:textSize="16dp" />  
  24.     <Button  
  25.         android:id="@+id/btnShareAll"  
  26.         android:layout_width="fill_parent"  
  27.         android:layout_height="44dp"  
  28.         android:layout_alignParentRight="true"  
  29.         android:layout_centerVertical="true"  
  30.         android:layout_margin="5dp"  
  31.         android:background="@drawable/btn_back"  
  32.         android:text="分享全部(無介面,直接分享)"  
  33.         android:textSize="16dp" />  
  34.     <Button  
  35.         android:id="@+id/btnUserInfo"  
  36.         android:layout_width="fill_parent"  
  37.         android:layout_height="44dp"  
  38.         android:layout_below="@+id/btnShareAll"  
  39.         android:layout_margin="5dp"  
  40.         android:layout_marginTop="41dp"  
  41.         android:background="@drawable/btn_back"  
  42.         android:text="獲取授權使用者資料"   
  43.         android:textSize="16dp" />  
  44. </RelativeLayout>  
<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" >

    <Button
        android:id="@+id/btnLogin"
        android:layout_width="fill_parent"
        android:layout_height="44dp"
        android:layout_above="@+id/btnShareAllGui"
        android:layout_centerHorizontal="true"
        android:layout_margin="5dp"
        android:background="@drawable/btn_back"
        android:text="使用者授權登入" 
        android:textSize="16dp"/>
       
    <Button
        android:id="@+id/btnShareAllGui"
        android:layout_width="fill_parent"
        android:layout_height="44dp"
        android:layout_above="@+id/btnShareAll"
        android:layout_margin="5dp"
        android:background="@drawable/btn_back"
        android:text="分享全部(有分享介面)"
        android:textSize="16dp" />

    <Button
        android:id="@+id/btnShareAll"
        android:layout_width="fill_parent"
        android:layout_height="44dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_margin="5dp"
        android:background="@drawable/btn_back"
        android:text="分享全部(無介面,直接分享)"
        android:textSize="16dp" />

    <Button
        android:id="@+id/btnUserInfo"
        android:layout_width="fill_parent"
        android:layout_height="44dp"
        android:layout_below="@+id/btnShareAll"
        android:layout_margin="5dp"
        android:layout_marginTop="41dp"
        android:background="@drawable/btn_back"
        android:text="獲取授權使用者資料" 
        android:textSize="16dp" />

</RelativeLayout>

5、使用者授權登入的佈局頁面,activity_auth.xml:

[html] view plain copy print?在CODE上檢視程式碼片派生到我的程式碼片

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:background="#fff5f5f5"  
  6.     android:orientation="vertical" >  
  7.     <!--ShareSDK-Core包下封裝的一個標題欄-->  
  8.     <cn.sharesdk.framework.TitleLayout  
  9.         android:id="@+id/llTitle"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:background="@drawable/title_back" />  
  13.     <LinearLayout  
  14.         android:id="@+id/llBody"  
  15.         android:layout_width="fill_parent"  
  16.         android:layout_height="wrap_content"  
  17.         android:layout_alignParentLeft="true"  
  18.         android:layout_alignParentTop="true"  
  19.         android:layout_marginTop="58dp"  
  20.         android:orientation="vertical"  
  21.         android:padding="10dp" >  
  22.         <LinearLayout  
  23.             android:layout_width="fill_parent"  
  24.             android:layout_height="50dp"  
  25.             android:background="@drawable/list_item_first_normal"  
  26.             android:paddingLeft="10dp"  
  27.             android:paddingRight="10dp" >  
  28.             <ImageView  
  29.                 android:layout_width="30dp"  
  30.                 android:layout_height="30dp"  
  31.                 android:layout_gravity="center_vertical"  
  32.                 android:layout_marginRight="10dp"  
  33.                 android:scaleType="centerInside"  
  34.                 android:src="@drawable/sina_weibo" />  
  35.             <CheckedTextView  
  36.                 android:id="@+id/ctvSw"  
  37.                 android:layout_width="fill_parent"  
  38.                 android:layout_height="fill_parent"  
  39.                 android:layout_gravity="center_vertical"  
  40.                 android:drawablePadding="10dp"  
  41.                 android:drawableRight="@drawable/cb_drw"  
  42.                 android:gravity="center_vertical"  
  43.                 android:singleLine="true"  
  44.                 android:text="@string/not_yet_authorized"  
  45.                 android:textColor="#ff000000"  
  46.                 android:textSize="20dp" />  
  47.         </LinearLayout>  
  48.         <LinearLayout  
  49.             android:layout_width="fill_parent"  
  50.             android:layout_height="50dp"  
  51.             android:background="@drawable/list_item_middle_normal"  
  52.             android:paddingLeft="10dp"  
  53.             android:paddingRight="10dp" >  
  54.             <ImageView  
  55.                 android:layout_width="30dp"  
  56.                 android:layout_height="30dp"  
  57.                 android:layout_gravity="center_vertical"  
  58.                 android:layout_marginRight="10dp"  
  59.                 android:scaleType="centerInside"  
  60.                 android:src="@drawable/tencent_weibo" />  
  61.             <CheckedTextView  
  62.                 android:id="@+id/ctvTc"  
  63.                 android:layout_width="fill_parent"  
  64.                 android:layout_height="fill_parent"  
  65.                 android:layout_gravity="center_vertical"  
  66.                 android:drawablePadding="10dp"  
  67.                 android:drawableRight="@drawable/cb_drw"  
  68.                 android:gravity="center_vertical"  
  69.                 android:singleLine="true"  
  70.                 android:text="@string/not_yet_authorized"  
  71.                 android:textColor="#ff000000"  
  72.                 android:textSize="20dp" />  
  73.         </LinearLayout>  
  74.         <LinearLayout  
  75.             android:layout_width="fill_parent"  
  76.             android:layout_height="50dp"  
  77.             android:background="@drawable/list_item_middle_normal"  
  78.             android:paddingLeft="10dp"  
  79.             android:paddingRight="10dp" >  
  80.             <ImageView  
  81.                 android:layout_width="30dp"  
  82.                 android:layout_height="30dp"  
  83.                 android:layout_gravity="center_vertical"  
  84.                 android:layout_marginRight="10dp"  
  85.                 android:scaleType="centerInside"  
  86.                 android:src="@drawable/renren" />  
  87.             <CheckedTextView  
  88.                 android:id="@+id/ctvRr"  
  89.                 android:layout_width="fill_parent"  
  90.                 android:layout_height="fill_parent"  
  91.                 android:layout_gravity="center_vertical"  
  92.                 android:drawablePadding="10dp"  
  93.                 android:drawableRight="@drawable/cb_drw"  
  94.                 android:gravity="center_vertical"  
  95.                 android:singleLine="true"  
  96.                 android:text="@string/not_yet_authorized"  
  97.                 android:textColor="#ff000000"  
  98.                 android:textSize="20dp" />  
  99.         </LinearLayout>  
  100.         <LinearLayout  
  101.             android:layout_width="fill_parent"  
  102.             android:layout_height="50dp"  
  103.             android:background="@drawable/list_item_last_normal"  
  104.             android:paddingLeft="10dp"  
  105.             android:paddingRight="10dp" >  
  106.             <ImageView  
  107.                 android:layout_width="30dp"  
  108.                 android:layout_height="30dp"  
  109.                 android:layout_gravity="center_vertical"  
  110.                 android:layout_marginRight="10dp"  
  111.                 android:scaleType="centerInside"  
  112.                 android:src="@drawable/qzone" />  
  113.             <CheckedTextView  
  114.                 android:id="@+id/ctvQz"  
  115.                 android:layout_width="fill_parent"  
  116.                 android:layout_height="fill_parent"  
  117.                 android:layout_gravity="center_vertical"  
  118.                 android:drawablePadding="10dp"  
  119.                 android:drawableRight="@drawable/cb_drw"  
  120.                 android:gravity="center_vertical"  
  121.                 android:singleLine="true"  
  122.                 android:text="@string/not_yet_authorized"  
  123.                 android:textColor="#ff000000"  
  124.                 android:textSize="20dp" />  
  125.         </LinearLayout>  
  126.     </LinearLayout>  
  127. </RelativeLayout>  
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#fff5f5f5"
    android:orientation="vertical" >

    <!--ShareSDK-Core包下封裝的一個標題欄-->
    <cn.sharesdk.framework.TitleLayout
        android:id="@+id/llTitle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/title_back" />

    <LinearLayout
        android:id="@+id/llBody"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="58dp"
        android:orientation="vertical"
        android:padding="10dp" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:background="@drawable/list_item_first_normal"
            android:paddingLeft="10dp"
            android:paddingRight="10dp" >

            <ImageView
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_gravity="center_vertical"
                android:layout_marginRight="10dp"
                android:scaleType="centerInside"
                android:src="@drawable/sina_weibo" />

            <CheckedTextView
                android:id="@+id/ctvSw"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_gravity="center_vertical"
                android:drawablePadding="10dp"
                android:drawableRight="@drawable/cb_drw"
                android:gravity="center_vertical"
                android:singleLine="true"
                android:text="@string/not_yet_authorized"
                android:textColor="#ff000000"
                android:textSize="20dp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:background="@drawable/list_item_middle_normal"
            android:paddingLeft="10dp"
            android:paddingRight="10dp" >

            <ImageView
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_gravity="center_vertical"
                android:layout_marginRight="10dp"
                android:scaleType="centerInside"
                android:src="@drawable/tencent_weibo" />

            <CheckedTextView
                android:id="@+id/ctvTc"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_gravity="center_vertical"
                android:drawablePadding="10dp"
                android:drawableRight="@drawable/cb_drw"
                android:gravity="center_vertical"
                android:singleLine="true"
                android:text="@string/not_yet_authorized"
                android:textColor="#ff000000"
                android:textSize="20dp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:background="@drawable/list_item_middle_normal"
            android:paddingLeft="10dp"
            android:paddingRight="10dp" >

            <ImageView
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_gravity="center_vertical"
                android:layout_marginRight="10dp"
                android:scaleType="centerInside"
                android:src="@drawable/renren" />

            <CheckedTextView
                android:id="@+id/ctvRr"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_gravity="center_vertical"
                android:drawablePadding="10dp"
                android:drawableRight="@drawable/cb_drw"
                android:gravity="center_vertical"
                android:singleLine="true"
                android:text="@string/not_yet_authorized"
                android:textColor="#ff000000"
                android:textSize="20dp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:background="@drawable/list_item_last_normal"
            android:paddingLeft="10dp"
            android:paddingRight="10dp" >

            <ImageView
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_gravity="center_vertical"
                android:layout_marginRight="10dp"
                android:scaleType="centerInside"
                android:src="@drawable/qzone" />

            <CheckedTextView
                android:id="@+id/ctvQz"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_gravity="center_vertical"
                android:drawablePadding="10dp"
                android:drawableRight="@drawable/cb_drw"
                android:gravity="center_vertical"
                android:singleLine="true"
                android:text="@string/not_yet_authorized"
                android:textColor="#ff000000"
                android:textSize="20dp" />
        </LinearLayout>
    </LinearLayout>

</RelativeLayout>


6、獲得使用者資訊佈局介面,activity_userinfo.xml:

[html] view plain copy print?在CODE上檢視程式碼片派生到我的程式碼片

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:background="#fff5f5f5"  
  6.     android:orientation="vertical" >  
  7.     <!--ShareSDK-Core包下封裝的一個標題欄-->  
  8.     <cn.sharesdk.framework.TitleLayout  
  9.         android:id="@+id/llTitle"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:background="@drawable/title_back" />  
  13.             <Button  
  14.                 android:id="@+id/btnQz"  
  15.                 android:layout_width="fill_parent"  
  16.                 android:layout_height="44dp"  
  17.                 android:layout_centerVertical="true"  
  18.                 android:layout_margin="5dp"  
  19.                 android:background="@drawable/btn_back"  
  20.                 android:text="@string/get_user_info_qz"  
  21.                 android:textSize="16dp" />  
  22.             <Button  
  23.                 android:id="@+id/btnRr"  
  24.                 android:layout_width="fill_parent"  
  25.                 android:layout_height="44dp"  
  26.                 android:layout_above="@+id/btnQz"  
  27.                 android:layout_margin="5dp"  
  28.                 android:background="@drawable/btn_back"  
  29.                 android:text="@string/get_user_info_rr"  
  30.                 android:textSize="16dp" />  
  31.             <Button  
  32.                 android:id="@+id/btnSw"  
  33.                 android:layout_width="fill_parent"  
  34.                 android:layout_height="44dp"  
  35.                 android:layout_above="@+id/btnRr"  
  36.                 android:layout_margin="5dp"  
  37.                 android:background="@drawable/btn_back"  
  38.                 android:text="@string/get_user_info_sw"  
  39.                 android:textSize="16dp" />  
  40.             <Button  
  41.                 android:id="@+id/btnTc"  
  42.                 android:layout_width="fill_parent"  
  43.                 android:layout_height="44dp"  
  44.                 android:layout_below="@+id/btnQz"  
  45.                 android:layout_centerHorizontal="true"  
  46.                 android:layout_margin="5dp"  
  47.                 android:background="@drawable/btn_back"  
  48.                 android:text="@string/get_user_info_tc"  
  49.                 android:textSize="16dp" />  
  50. </RelativeLayout>  
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#fff5f5f5"
    android:orientation="vertical" >

    <!--ShareSDK-Core包下封裝的一個標題欄-->
    <cn.sharesdk.framework.TitleLayout
        android:id="@+id/llTitle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/title_back" />

            <Button
                android:id="@+id/btnQz"
                android:layout_width="fill_parent"
                android:layout_height="44dp"
                android:layout_centerVertical="true"
                android:layout_margin="5dp"
                android:background="@drawable/btn_back"
                android:text="@string/get_user_info_qz"
                android:textSize="16dp" />

            <Button
                android:id="@+id/btnRr"
                android:layout_width="fill_parent"
                android:layout_height="44dp"
                android:layout_above="@+id/btnQz"
                android:layout_margin="5dp"
                android:background="@drawable/btn_back"
                android:text="@string/get_user_info_rr"
                android:textSize="16dp" />

            <Button
                android:id="@+id/btnSw"
                android:layout_width="fill_parent"
                android:layout_height="44dp"
                android:layout_above="@+id/btnRr"
                android:layout_margin="5dp"
                android:background="@drawable/btn_back"
                android:text="@string/get_user_info_sw"
                android:textSize="16dp" />

            <Button
                android:id="@+id/btnTc"
                android:layout_width="fill_parent"
                android:layout_height="44dp"
                android:layout_below="@+id/btnQz"
                android:layout_centerHorizontal="true"
                android:layout_margin="5dp"
                android:background="@drawable/btn_back"
                android:text="@string/get_user_info_tc"
                android:textSize="16dp" />

</RelativeLayout>


7、顯示使用者獲得的資訊佈局介面,activity_userinfo.xml:

[html] view plain copy print?在CODE上檢視程式碼片派生到我的程式碼片

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:background="#fff5f5f5"  
  6.     android:orientation="vertical" >  
  7.     <!--ShareSDK-Core包下封裝的一個標題欄-->  
  8.     <cn.sharesdk.framework.TitleLayout  
  9.         android:id="@+id/llTitle"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:background="@drawable/title_back" />  
  13.     <ScrollView  
  14.         android:layout_width="fill_parent"  
  15.         android:layout_height="wrap_content"  
  16.         android:layout_alignParentBottom="true"  
  17.         android:layout_below="@id/llTitle"  
  18.         android:paddingBottom="10dp"  
  19.         android:paddingLeft="10dp"  
  20.         android:paddingTop="10dp" >  
  21.         <TextView  
  22.             android:id="@+id/tvJson"  
  23.             android:layout_width="fill_parent"  
  24.             android:layout_height="wrap_content"  
  25.             android:layout_marginRight="10dp"  
  26.             android:autoLink="all"  
  27.             android:background="@drawable/list_item_single_normal"  
  28.             android:textColor="#ff000000" />  
  29.     </ScrollView>  
  30. </RelativeLayout>  
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#fff5f5f5"
    android:orientation="vertical" >

    <!--ShareSDK-Core包下封裝的一個標題欄-->
    <cn.sharesdk.framework.TitleLayout
        android:id="@+id/llTitle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/title_back" />

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_below="@id/llTitle"
        android:paddingBottom="10dp"
        android:paddingLeft="10dp"
        android:paddingTop="10dp" >

        <TextView
            android:id="@+id/tvJson"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:autoLink="all"
            android:background="@drawable/list_item_single_normal"
            android:textColor="#ff000000" />
    </ScrollView>

</RelativeLayout>

8、主介面入口Activity類,MainActivity.java:

[java] view plain copy print?在CODE上檢視程式碼片派生到我的程式碼片

  1. package com.yangyu.activity;  
  2. import java.io.File;  
  3. import java.io.FileOutputStream;  
  4. import android.app.Activity;  
  5. import android.content.Intent;  
  6. import android.graphics.Bitmap;  
  7. import android.graphics.Bitmap.CompressFormat;  
  8. import android.graphics.BitmapFactory;  
  9. import android.os.Bundle;  
  10. import android.os.Environment;  
  11. import android.view.View;  
  12. import android.view.View.OnClickListener;  
  13. import android.widget.Button;  
  14. import cn.sharesdk.framework.AbstractWeibo;  
  15. import cn.sharesdk.onekeyshare.ShareAllGird;  
  16. import com.yangyu.mysharethings.R;  
  17. /** 
  18.  * @author yangyu 
  19.  *  功能描述:主Activity類,程式的入口類 
  20.  */  
  21. public class MainActivity extends Activity implements OnClickListener {  
  22.     //定義圖片存放的地址   
  23.     public static String TEST_IMAGE;  
  24.     //定義"賬號登陸"按鈕,"有分享介面按鈕","無分享介面"按鈕,"得到使用者資料"按鈕  
  25.     private Button authLoginBtn,shareGuiBtn,shareBtn,getInfoBtn;  
  26.     @Override  
  27.     protected void onCreate(Bundle savedInstanceState) {  
  28.         super.onCreate(savedInstanceState);  
  29.         setContentView(R.layout.activity_main);  
  30.         //初始化ShareSDK   
  31.         AbstractWeibo.initSDK(this);      
  32.         initImagePath();                      
  33.         initView();  
  34.         initData();  
  35.     }  
  36.     /** 
  37.      * 初始化元件 
  38.      */  
  39.     private void initView(){  
  40.         authLoginBtn = (Button)findViewById(R.id.btnLogin);  
  41.         shareGuiBtn = (Button)findViewById(R.id.btnShareAllGui);  
  42.         shareBtn = (Button)findViewById(R.id.btnShareAll);  
  43.         getInfoBtn = (Button)findViewById(R.id.btnUserInfo);  
  44.     }  
  45.     /** 
  46.      * 初始化資料 
  47.      */  
  48.     private void initData(){  
  49.         //設定按鈕監聽事件   
  50.         authLoginBtn.setOnClickListener(this);  
  51.         shareGuiBtn.setOnClickListener(this);  
  52.         shareBtn.setOnClickListener(this);  
  53.         getInfoBtn.setOnClickListener(this);  
  54.     }  
  55.     /** 
  56.      * 初始化分享的圖片 
  57.      */  
  58.     private void initImagePath() {  
  59.         try {//判斷SD卡中是否存在此資料夾  
  60.             if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())  
  61.                     && Environment.getExternalStorageDirectory().exists()) {  
  62.                 TEST_IMAGE = Environment.getExternalStorageDirectory().getAbsolutePath() + "/pic.png";  
  63.             }  
  64.             else {  
  65.                 TEST_IMAGE = getApplication().getFilesDir().getAbsolutePath() + "/pic.png";  
  66.             }  
  67.             File file = new File(TEST_IMAGE);  
  68.             //判斷圖片是否存此資料夾中   
  69.             if (!file.exists()) {  
  70.                 file.createNewFile();  
  71.                 Bitmap pic = BitmapFactory.decodeResource(getResources(), R.drawable.pic);  
  72.                 FileOutputStream fos = new FileOutputStream(file);  
  73.                 pic.compress(CompressFormat.JPEG, 100, fos);  
  74.                 fos.flush();  
  75.                 fos.close();  
  76.             }  
  77.         } catch(Throwable t) {  
  78.             t.printStackTrace();  
  79.             TEST_IMAGE = null;  
  80.         }  
  81.     }  
  82.     /** 
  83.      * 按鈕監聽事件 
  84.      */  
  85.     @Override  
  86.     public void onClick(View v) {  
  87.         switch (v.getId()) {  
  88.         case R.id.btnLogin:  
  89.             startActivity(new Intent(MainActivity.this,AuthActivity.class));  
  90.             break;  
  91.         case R.id.btnShareAllGui:  
  92.             showGrid(false);  
  93.             break;  
  94.         case R.id.btnShareAll:  
  95.             showGrid(true);  
  96.             break;  
  97.         case R.id.btnUserInfo:  
  98.             // 獲取自己的資料   
  99.             Intent i = new Intent(this, GetInforActivity.class);  
  100.             startActivity(i);  
  101.             break;  
  102.         default:  
  103.             break;  
  104.         }  
  105.     }  
  106.     /** 
  107.      * 使用快捷分享完成圖文分享 
  108.      */  
  109.     private void showGrid(boolean silent) {  
  110.         Intent i = new Intent(this, ShareAllGird.class);  
  111.         // 分享時Notification的圖示   
  112.         i.putExtra("notif_icon", R.drawable.ic_launcher);  
  113.         // 分享時Notification的標題   
  114.         i.putExtra("notif_title", this.getString(R.string.app_name));  
  115.         // title標題,在印象筆記、郵箱、資訊、微信(包括好友和朋友圈)、人人網和QQ空間使用,否則可以不提供  
  116.         i.putExtra("title", this.getString(R.string.share));  
  117.         // titleUrl是標題的網路連結,僅在人人網和QQ空間使用,否則可以不提供   
  118.         i.putExtra("titleUrl", "http://sharesdk.cn");  
  119.         // text是分享文字,所有平臺都需要這個欄位   
  120.         i.putExtra("text", this.getString(R.string.share_content));  
  121.         // imagePath是本地的圖片路徑,所有平臺都支援這個欄位,不提供,則表示不分享圖片   
  122.         i.putExtra("imagePath", MainActivity.TEST_IMAGE);  
  123.         // url僅在微信(包括好友和朋友圈)中使用,否則可以不提供   
  124.         i.putExtra("url", "http://sharesdk.cn");  
  125.         // thumbPath是縮圖的本地路徑,僅在微信(包括好友和朋友圈)中使用,否則可以不提供  
  126.         i.putExtra("thumbPath", MainActivity.TEST_IMAGE);  
  127.         // appPath是待分享應用程式的本地路勁,僅在微信(包括好友和朋友圈)中使用,否則可以不提供  
  128.         i.putExtra("appPath", MainActivity.TEST_IMAGE);  
  129.         // comment是我對這條分享的評論,僅在人人網和QQ空間使用,否則可以不提供   
  130.         i.putExtra("comment", this.getString(R.string.share));  
  131.         // site是分享此內容的網站名稱,僅在QQ空間使用,否則可以不提供   
  132.         i.putExtra("site", this.getString(R.string.app_name));  
  133.         // siteUrl是分享此內容的網站地址,僅在QQ空間使用,否則可以不提供   
  134.         i.putExtra("siteUrl", "http://sharesdk.cn");  
  135.         // 是否直接分享   
  136.         i.putExtra("silent", silent);  
  137.         this.startActivity(i);  
  138.     }  
  139.     /** 
  140.      * 將action轉換為String 
  141.      */  
  142.     public static String actionToString(int action) {  
  143.         switch (action) {  
  144.             case AbstractWeibo.ACTION_AUTHORIZING: return "ACTION_AUTHORIZING";  
  145.             case AbstractWeibo.ACTION_GETTING_FRIEND_LIST: return "ACTION_GETTING_FRIEND_LIST";  
  146.             case AbstractWeibo.ACTION_FOLLOWING_USER: return "ACTION_FOLLOWING_USER";  
  147.             case AbstractWeibo.ACTION_SENDING_DIRECT_MESSAGE: return "ACTION_SENDING_DIRECT_MESSAGE";  
  148.             case AbstractWeibo.ACTION_TIMELINE: return "ACTION_TIMELINE";  
  149.             case AbstractWeibo.ACTION_USER_INFOR: return "ACTION_USER_INFOR";  
  150.             case AbstractWeibo.ACTION_SHARE: return "ACTION_SHARE";  
  151.             default: {  
  152.                 return "UNKNOWN";  
  153.             }  
  154.         }  
  155.     }  
  156.     protected void onDestroy() {  
  157.         //結束ShareSDK的統計功能並釋放資源   
  158.         AbstractWeibo.stopSDK(this);  
  159.         super.onDestroy();  
  160.     }  
  161. }  
package com.yangyu.activity;

import java.io.File;
import java.io.FileOutputStream;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import cn.sharesdk.framework.AbstractWeibo;
import cn.sharesdk.onekeyshare.ShareAllGird;

import com.yangyu.mysharethings.R;

/**
 * @author yangyu
 *	功能描述:主Activity類,程式的入口類
 */
public class MainActivity extends Activity implements OnClickListener {
	//定義圖片存放的地址
	public static String TEST_IMAGE;

	//定義"賬號登陸"按鈕,"有分享介面按鈕","無分享介面"按鈕,"得到使用者資料"按鈕
	private Button authLoginBtn,shareGuiBtn,shareBtn,getInfoBtn;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		//初始化ShareSDK
		AbstractWeibo.initSDK(this);	
		
		initImagePath();					
		
		initView();
		
		initData();
	}

	/**
	 * 初始化元件
	 */
	private void initView(){
		authLoginBtn = (Button)findViewById(R.id.btnLogin);
		shareGuiBtn = (Button)findViewById(R.id.btnShareAllGui);
		shareBtn = (Button)findViewById(R.id.btnShareAll);
		getInfoBtn = (Button)findViewById(R.id.btnUserInfo);
	}
	
	/**
	 * 初始化資料
	 */
	private void initData(){
		//設定按鈕監聽事件
		authLoginBtn.setOnClickListener(this);
		shareGuiBtn.setOnClickListener(this);
		shareBtn.setOnClickListener(this);
		getInfoBtn.setOnClickListener(this);
	}
	
	/**
	 * 初始化分享的圖片
	 */
	private void initImagePath() {
		try {//判斷SD卡中是否存在此資料夾
			if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
					&& Environment.getExternalStorageDirectory().exists()) {
				TEST_IMAGE = Environment.getExternalStorageDirectory().getAbsolutePath() + "/pic.png";
			}
			else {
				TEST_IMAGE = getApplication().getFilesDir().getAbsolutePath() + "/pic.png";
			}
			File file = new File(TEST_IMAGE);
			//判斷圖片是否存此資料夾中
			if (!file.exists()) {
				file.createNewFile();
				Bitmap pic = BitmapFactory.decodeResource(getResources(), R.drawable.pic);
				FileOutputStream fos = new FileOutputStream(file);
				pic.compress(CompressFormat.JPEG, 100, fos);
				fos.flush();
				fos.close();
			}
		} catch(Throwable t) {
			t.printStackTrace();
			TEST_IMAGE = null;
		}
	}
	
	/**
	 * 按鈕監聽事件
	 */
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnLogin:
			startActivity(new Intent(MainActivity.this,AuthActivity.class));
			break;
		case R.id.btnShareAllGui:
			showGrid(false);
			break;
		case R.id.btnShareAll:
			showGrid(true);
			break;
		case R.id.btnUserInfo:
			// 獲取自己的資料
			Intent i = new Intent(this, GetInforActivity.class);
			startActivity(i);
			break;
		default:
			break;
		}
		
	}

	/**
	 * 使用快捷分享完成圖文分享
	 */
	private void showGrid(boolean silent) {
		Intent i = new Intent(this, ShareAllGird.class);
		// 分享時Notification的圖示
		i.putExtra("notif_icon", R.drawable.ic_launcher);
		// 分享時Notification的標題
		i.putExtra("notif_title", this.getString(R.string.app_name));

		// title標題,在印象筆記、郵箱、資訊、微信(包括好友和朋友圈)、人人網和QQ空間使用,否則可以不提供
		i.putExtra("title", this.getString(R.string.share));
		// titleUrl是標題的