下面開始介紹如何使用ShareSDK實現微博的分享功能(類似於其他平臺)。
ShareSDK門戶
在使用shareSDK之前,我們需要在新浪微博的開放平臺註冊,以獲取appkey等信息。
新浪微博開放平臺門戶
下面圖片中劃掉的部分要重點看。
特別需要註意的是,下面的回調URL必須填寫,並且在代碼中涉及到了,所以可以使用默認的URL。
至此,開發前的準備工作已經做好,接下來仍然是粘貼代碼。
首先看布局文件代碼,很簡單,只有壹個按鈕。
& ltlinear layout xmlns:Android = " . shares dk . framework . platform;
023 . import cn . shares dk . framework . platformactionlistener;
024 . import cn . shares dk . framework . shares dk;
025 . import cn . shares dk . framework . utils . ui handler;
026 . import cn . shares dk . onekey share . onekey share;
027.
028 .公共類MainActivity擴展Activity實現PlatformActionListener,
029.回撥{
030.
031 . private static final int MSG _ TOAST = 1;
032 . private static final int MSG _ ACTION _ CCALLBACK = 2;
033 . private static final int MSG _ CANCEL _ NOTIFY = 3;
034.
035.//sd卡中的圖片名稱
036.private靜態最終字符串FILE _ NAME = "/share _ pic . jpg ";
037.public靜態字符串TEST _ IMAGE
038.
039.@覆蓋
040 . public boolean handle Message(Message msg){
041.switch (msg.what) {
042.case MSG_TOAST: {
043.string text = string . value of(msg . obj);
044.Toast . make Text(main activity . this,text,Toast。LENGTH_SHORT)。show();
045.}
046 .打破;
047.case MSG_ACTION_CCALLBACK: {
048.switch (msg.arg1) {
049.case 1: //成功後發送通知。
050.showNotification(2000,“共享完成”);
051 . break;
052 .案例2: //失敗後發送通知。
053.showNotification(2000,“共享失敗”);
054 .休息;
055 .情況3: //取消
056.showNotification(2000,“取消共享”);
057 .決裂;
058.}
059.}
060 .休息;
061.case消息_取消_通知:
062.notification manager nm =(notification manager)msg . obj;
063.if (nm!= null) {
064 . nm . cancel(msg . arg 1);
065.}
066 .打破;
067.}
068 .返回false
069.}
070.
071.@覆蓋
072 . protected void onCreate(Bundle saved instancestate){
073 . super . oncreate(savedInstanceState);
074 . setcontentview(r . layout . activity _ main);
075.//初始化ShareSDK
076.shares dk . init SDK(this);
077.//初始化圖像路徑
078 .新線程(){
079 .公共void run() {
080 . initimagepath();
081.}
082.}.start();
083.}
084.
085.//壹鍵分享的點擊事件
086.public void click(視圖v) {
087.//實例化OnekeyShare對象。
088.onekey share oks = new onekey share();
089.//設置通知的顯示圖標和文本。
090 . oks . set notification(r . draw able . IC _ launcher," shares dk demo ");
091.//設置短信地址或郵箱地址,如果沒有,則不需要設置。
092 . oks . set address(" 12345678901 ");
093.//共享內容的標題
094.oks.setTitle(“共享內容的標題”);
095.//標題對應的URL,如果沒有,就無法設置。
096 . oks . settitleurl("/imgs/shares dk/content/2013/07/25/1374723172663 . jpg ");
103.//微信和易信的共享網絡連接,如果沒有,則無法設置。
104 . oks . seturl(" ");
105.//人人平臺特有的評論字段,如果沒有可以不勾選。
106 . oks . set comment(" comment ");
107.//程序名或站點名
108 . oks . set site(" site ");
109.//程序名稱或網站名稱的鏈接地址。
110 . oks . setsiteurl(" ");
111.//設置緯度
112 . oks . set latitude(23.122619f);
113.//設置精度。
114 . oks . set longitude(113.372338 f);
115.//設置是直接分享嗎?
116 . oks . set silent(false);
117.//顯示
118 . oks . show(main activity . this);
119.}
120.
121 . private void initImagePath(){
嘗試{
123.if(環境。MEDIA_MOUNTED.equals(環境
124..getExternalStorageState())
125.& amp& ampenvironment . getexternalstoratedirectory()。exists()) {
126.TEST _ IMAGE = environment . getexternalstoratedirectory()
127..getAbsolutePath() +文件名;
128.}否則{
129.TEST_IMAGE = getApplication()。getFilesDir()。getAbsolutePath()
130.+文件名;
131.}
132.//創建壹個圖片文件夾
133.File file =新文件(TEST _ IMAGE);
134.if(!file.exists()) {
135 . file . create new file();
136.bitmap pic = bitmap factory . decode resource(get resources(),
137.r . draw able . pic);
138.file output stream fos = new file output stream(file);
139 . pic . compress(compress format。JPEG,100,fos);
140 . fos . flush();
141 . fos . close();
142.}
143.} catch (Throwable t) {
144 . t . printstacktrace();
145.TEST _ IMAGE = null
146.}
147.}
148.
149.@覆蓋
150 . protected void on destroy(){
151 . super . ondestroy();
152.//停止活動中的ShareSDK。
153.shares dk . stop SDK(this);
154.}
155.