Joke Collection Website - Blessing messages - How to apply for the key of Sina Android sharesdk?

How to apply for the key of Sina Android sharesdk?

Because it is the first time to use it, you need to authorize Sina and authorize the interface first.

Let's begin to introduce how to use the ShareSDK to realize the sharing function of Weibo (similar to other platforms).

ShareSDK portal

Before using the shareSDK, we need to register on the open platform of Sina Weibo to obtain information such as appkey.

Sina Weibo Open Platform Portal

The parts crossed out in the picture below should be highlighted.

It is particularly important to note that the following callback URL must be filled in and involved in the code, so the default URL can be used.

At this point, the preparation work before development has been done, and the next step is still to paste the code.

First of all, look at the layout file code, which is very simple and has only one button.

& 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. the public class MainActivity extends Activity to realize PlatformActionListener.

029. Call Back {

030.

03 1 . 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.//Picture name in SD card

036.private static final string file _ name = "/share _ pic.jpg";

037.public static string TEST _ IMAGE

038.

039.@ Overwrite

040 . public boolean handle Message(Message msg){

04 1.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. break;

047.case MSG_ACTION_CCALLBACK: {

048.switch (msg.arg 1) {

049.case 1: // Send a notification after success.

050.showNotification(2000, "sharing completed");

05 1 . break;

052. Case 2: // Send notification after failure.

053.showNotification(2000, "Sharing Failure");

054. rest;

055. Situation 3: // Cancelled

056.showNotification(2000, "unsharing");

057. Break;

058.}

059.}

060. rest;

06 1.case message _ cancel _ notification:

062.notification manager nm =(notification manager)msg . obj;

063.if (nm! = null) {

064 . nm . cancel(msg . arg 1);

065.}

066. break;

067.}

068. Returns false.

069.}

070.

07 1.@ Overlay

072 . protected void onCreate(Bundle saved instancestate){

073 . super . oncreate(savedInstanceState);

074 . setcontentview(r . layout . activity _ main);

075.// Initialize ShareSDK

076.shares dk . init SDK(this);

077.// Initialize the image path

078. New thread () {

079. public void run() {

080 . initimagepath();

08 1.}

082.}.start();

083.}

084.

085.// Click events shared with one click

086.public void click (view v) {

087.// Instantiate the OnekeyShare object.

088.onekey share oks = new onekey share();

089.// Set the display icon and text of the notification.

090 . oks . set notification(r . draw able . IC _ launcher," shares dk demo ");

09 1.// Set the SMS address or email address. If not, you don't need to set it.

092 . oks . set address(" 1234567890 1 ");

093.// Title of shared content

094.oks.setTitle ("Title of Shared Content");

095.// The URL corresponding to the title cannot be set without it.

096 . oks . settitleurl("/imgs/shares dk/content/20 13/07/25/ 1374723 172663 . jpg ");

103.// If there is no shared network connection between WeChat and Yixin, it cannot be set.

104 . oks . seturl(" ");

105.// The comment field unique to Renren platform can be left unchecked if it is not available.

106 . oks . set comment(" comment ");

107.// Program name or site name

108 . oks . set site(" site ");

109.// Link address of program name or website name.

1 10 . oks . setsiteurl(" ");

1 1 1.// Set the latitude.

1 12 . oks . set latitude(23. 1226 19f);

1 13.// Set the precision.

1 14 . oks . set longitude( 1 13.372338 f);

1 15.// Is the setting shared directly?

1 16 . oks . set silent(false);

1 17.// Display

1 18 . oks . show(main activity . this);

1 19.}

120.

12 1 . private void initImagePath(){

Try {

123.if (environment. MEDIA_MOUNTED.equals (environment

124..getExternalStorageState())

125.& amp& ampenvironment . getexternalstoratedirectory()。 exists()) {

126.TEST _ IMAGE = environment . getexternalstoratedirectory()

127 ... getaabsolutepath ()+file name;

128.} Otherwise {

129.TEST_IMAGE = getApplication()。 getFilesDir()。 getAbsolutePath()

130.+ file name;

13 1.}

132.// Create a picture folder.

133.File file = new 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();

14 1 . fos . close();

142.}

143.} catch (Throwable t) {

144 . t . printstacktrace();

145.TEST _ IMAGE = null

146.}

147.}

148.

149.@ Overwrite

150 . protected void on destroy(){

15 1 . super . ondestroy();

152.// Stop the active ShareSDK.

153.shares dk . stop SDK(this);

154.}

155.