ShareSDK 官方的例子很是好,但代碼太多看起來不方便。我這裏整理了一個只有一個微博分享的例子。java
不過Android導入工做比較麻煩android
首先,導入jar包,要參考官方提供的說明文檔選本身須要的我這裏只留了新浪和騰訊微博的web
添加ShareSDK.conf AppKey的資料都在這個裏面 這個文件用記事本打開,將裏面的appkey等資料換成本身就OK,要看官方的文檔啊。app
接下來是大量的圖片資源ide
以上圖片資源都是在官網提供的例子中複製的我只留了必要的圖片資源和新浪和騰訊微博的。佈局
接下來是配置文件,在考配置文件時,要注意,有些文件以前系統就有的要將裏面的內容複製到對應的文件中,好比:string.xml 這裏要多多注意啊。否則程序可能會出錯this
最後考入快捷分享的包,其實用這個庫最大的好處就是分享方便不用本身寫界面。spa
記得寫配置文件:androidmanifest.xml啊code
<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.CHANGE_WIFI_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.MANAGE_ACCOUNTS"/> <uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<activity android:name="cn.sharesdk.framework.ShareSDKUIShell" android:theme="@android:style/Theme.Translucent.NoTitleBar" android:configChanges="keyboardHidden|orientation" android:screenOrientation="portrait" android:windowSoftInputMode="stateHidden|adjustResize" > <!-- Adapter表示一個繼承自cn.sharesdk.framework.authorize.AuthorizeAdapter的類, 這個類能夠監聽到頁面的生命週期,也能夠獲取受權頁面的各類UI控件。 開發者能夠經過繼承AuthorizeAdapter,重寫其方法,並獲取各類UI來自定義這個頁面的行爲。 --> <meta-data android:name="Adapter" android:value="cn.sharesdk.demo.MyAdapter" /> </activity>
作了這麼多準備工做,接下來咱們進入最後的環節orm
下面的代碼中有一個Button,本身在佈局文件中拖一個吧 這裏不給代碼了
package com.example.sharesdkandroid; import java.util.HashMap; import cn.sharesdk.framework.Platform; import cn.sharesdk.framework.PlatformActionListener; import cn.sharesdk.framework.ShareSDK; import cn.sharesdk.onekeyshare.OnekeyShare; import cn.sharesdk.sina.weibo.SinaWeibo; import android.os.Bundle; import android.R.string; import android.app.Activity; import android.content.Context; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity implements OnClickListener,PlatformActionListener{ private Button btnButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ShareSDK.initSDK(this); btnButton = (Button)findViewById(R.id.btn1); btnButton.setOnClickListener(this); Platform qzone = ShareSDK.getPlatform(this, SinaWeibo.NAME); String openID = qzone.getDb().getUserId(); String accessTokenString = qzone.getDb().getToken(); String nickname = (String)qzone.getDb().get("nickname"); System.out.println(openID); System.out.println(nickname); System.out.println(accessTokenString); String headicon = qzone.getDb().get("icon"); System.out.println(headicon); } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); ShareSDK.stopSDK(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn1: OnekeyShare oksOnekeyShare = new OnekeyShare(); oksOnekeyShare.setNotification(R.drawable.ic_launcher, "ffff"); oksOnekeyShare.setText("ffffffffffffffff"); oksOnekeyShare.setPlatform(SinaWeibo.NAME); oksOnekeyShare.setSilent(true); oksOnekeyShare.show(this); System.out.println("==================================="); Platform qzone = ShareSDK.getPlatform(this, SinaWeibo.NAME); String openID = qzone.getDb().getUserId(); String nickname = (String)qzone.getDb().get("nickname"); System.out.println(openID); System.out.println(nickname); // weiboPlatform.setPlatformActionListener(this); // // weiboPlatform.authorize(); // Platform.ShareParams sParams = new SinaWeibo.ShareParams(); // sParams.text = "text"; // Platform weibo = ShareSDK.getPlatform(this, SinaWeibo.NAME); // weibo.share(sParams); break; default: break; } } @Override public void onCancel(Platform arg0, int arg1) { // TODO Auto-generated method stub } @Override public void onComplete(Platform arg0, int arg1, HashMap<String, Object> arg2) { // TODO Auto-generated method stub System.out.println("==================================="); Platform qzonePlatform = ShareSDK.getPlatform(this, SinaWeibo.NAME); String accessTokenString = qzonePlatform.getDb().getToken(); System.out.println(accessTokenString); } @Override public void onError(Platform arg0, int arg1, Throwable arg2) { // TODO Auto-generated method stub } }