Android ShareSDK quickly implements the sharing function, androidsharesdk

Source: Internet
Author: User

Android ShareSDK quickly implements the sharing function, androidsharesdk
Step 1: Get ShareSDK

To integrate ShareSDK, you must first register with the ShareSDK official website and create an application to obtain the ShareSDK Appkey, and then download the SDK compressed package on the SDK download page, after decompression, you can get the directory structure as shown in:

In the "ShareSDK for Android" directory, "Libs" in this directory contains "MainLibs" and "OnekeyShare", which are the core library of ShareSDK and the source code library of "quick sharing" respectively, the description is also in the "ShareSDK for Android" directory. Please read carefully before integrating ShareSDK. "ShareSDK for Android ApiDoc"Contains the JavaDoc of Mainlibs and OnekeyShare for developers to check during development. "ShareSDK for Android Sample"Contains the ShareSDK function DEMO code and apk file. The JavaDoc of the Sample source code is in. "ShareSDK for Android Services"Includes the" plug-in service "That ShareSDK has released. Currently, we only provide the" comment and like "service, the package provides the dependent libraries, sample code, JavaDoc, and apk files of this plug-in service.

Step 2: Import ShareSDK

ShareSDKIntegration Method:

1. Copy the jar package directly to the libs directory of the target project.

For more information about how to directly copy jar files and resources, see the ShareSDK Sample project. To use the jar COPY method, in addition to copying the jar in MainLibs/libs, you also need to copy the images and strings in MainLibs/res. Otherwise, the resource cannot be found during authorization. If your project also integrates quick sharing, you also need to copy the source code in OneKeyShare/src and resources in OneKeyShare/res, if your project has no android-support-v4 jar, you need to put the jar of the android-support-v4 under OneKeyShare/libs into your project.

2. quickly generate a project

Because it is difficult to directly copy jar packages and integrate resources,ShareSDKProvides a fast integration program.WindowsYou can directly double-click to execute,After that, you can copy the files in the target directory to overwrite them in your project. This greatly simplifies the steps for directly copying jar files and resources.If your project is just developed or you want to writeDemoTestShareSDKFunction, you can use this method to quickly integrate

Step 3: Add application information

There are three methods

First: Configure in the ShareSDK application management background. to use this method, you must call ShareSDK. the initSDK (context, the AppKey returned by your application during sharesdk Registration) method is initialized. If you change the registration information later, you can directly change it from the Internet without publishing a new version for updates.(Highest priority).

Method 2: use the code to configure the "setPlatformDevInfo (String, HashMap <String, Object>)" method. to use this method, you must call ShareSDK. init (context, the AppKey returned by your application during sharesdk Registration) method for initialization.(Priority).

The third method is to use the "assets/ShareSDK. xml" file to configure the settings. The secondary method will exist in plain text, but this method is the easiest.(Lowest priority).

Developers can choose the three methods, but the three methods have their own differences: the first method can achieve the "dynamic configuration of application information" function, but once the network is disconnected, ShareSDK may not work; the third method is the method with the lowest priority, but it is the most convenient and centralized. Finally, the method with the code settings is the most flexible, in the second way, developers can write dead application information in the Code, or dynamically obtain Application Registration Information from their servers through the private protocol. The priority is between the first and second.UseShareSDK. xmlConfigure Registration Information

<ShareSDK AppKey = "Enter the AppKey you registered on ShareSDK"/> <SinaWeibo o SortId = "the location of this platform in your shared list, integer type, the greater the value, the closer the value is to the back. "AppKey =" fill in the AppKey you registered on Sina Weibo "AppSecret =" fill in the Custom AppSecret "Id =" you registered on Sina Weibo, integer. The identifier "RedirectUrl =" for this platform in your project is filled in with the RedirectUrl "ShareByAppClient =" whether to use a client to share "Enable =" Boolean value, mark whether the platform is valid "/>

ShareSDK. xml stores data in XML format. Each platform has a block. Apart from social platforms, the Appkey obtained by developers when registering an application with ShareSDK must be filled in the block "ShareSDK, if this Appkey is not the developer's own Appkey, the future statistics on the ShareSDK application background will be incorrect. Each ShareSDK platform has four fields: SortId, Id, and Enable. Other fields (such as AppKey, AppSecret, RedirectUrl, and ShareByAppClient of Sina Weibo) you need to register the application on the target platform. Please enter the data of these fields correctly. Otherwise, ShareSDK cannot complete authorization, and other subsequent operations will not be performed.

Sample Code configuration Registration Information(Demonstrate initializing Sina Weibo configuration information)The configuration information that can be set for each sharing platform is different. For details about the configuration required for the sharing platform, see the ShareSDK. xml configuration file in the sample.

ShareSDK. initSDK (Context, "AppKey returned by your application during Sharesdk registration"); HashMap <String, Object> hashMap = new HashMap <String, Object> (); hashMap. put ("Id", "1"); hashMap. put ("SortId", "1"); hashMap. put ("AppKey", "568898243"); hashMap. put ("AppSecret", "38a4f8204cc784f81f9f0daaf31e02e3"); hashMap. put ("RedirectUrl", "http://www.sharesdk.cn"); hashMap. put ("ShareByAppClient", "true"); hashMap. put ("Enable", "true"); ShareSDK. setPlatformDevInfo (SinaWeibo. NAME, hashMap );

The method must be called after the Sharesdk. initSDK () method. If the ShareSDK. stopSDK () method is called, you must call the ShareSDK. setPlatformDevInfo () method again after the ShareSDK. stopSDK () method is called. For more details about different fields in the application information, refer to the description in the ShareSDK. xml file header.

Step 4: Configure AndroidManifest. xml

Different integrations require different content to be added in AndroidManifest. xml. However, you must first Add the following permission list:

<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.GET_ACCOUNTS"/><uses-permission android:name="android.permission.MANAGE_ACCOUNTS"/><uses-permission android:name="android.permission.READ_PHONE_STATE"/><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/><uses-permission android:name="android.permission.GET_TASKS"/><uses-permission android:name="android.permission.INTERNET"/>

These permissions allow your project and ShareSDK to determine whether the current application is "front-end", get the network connection permission, get the network status permission of your device, implement the https secure connection permission, and read permission for the status of mobile devices and permissions for saving necessary configurations. In general, even if ShareSDK is not integrated, most projects will register and apply for these permissions.

Second, to ensure that the authorization operation can be completed smoothly, you need to register the following Activity under the application:

<activity    android:name="com.mob.tools.MobUIShell"    android:theme="@android:style/Theme.Translucent.NoTitleBar"    android:configChanges="keyboardHidden|orientation|screenSize"    android:screenOrientation="portrait"    android:windowSoftInputMode="stateHidden|adjustResize" />

 

If your project set or circle of friends,ViewAndroidManifest. xmlIn the configuration filePackagePath,You mustPackageCreate under directoryWxapiDirectory placementWXEntryActivity. If the activity is not shared, the callback checks whether the activity exists. If no activity exists, an error is returned.

<activity         android:name=".wxapi.WXEntryActivity"    android:theme="@android:style/Theme.Translucent.NoTitleBar"         android:configChanges="keyboardHidden|orientation|screenSize"         android:exported="true"         android:screenOrientation="portrait" />

 

If your project is easy to send messages to the two platforms, check the package path in the AndroidManifest. xml configuration file. Create the yxapi directory in the package directory and place the callback Activity:

<activity         android:name=".yxapi.YXEntryActivity"         android:theme="@android:style/Theme.Translucent.NoTitleBar"    android:configChanges="keyboardHidden|orientation|screenSize"     android:exported="true"         android:screenOrientation="portrait" />

 

The operation callback of the client, so ShareSDK cannot give you operation callback. To avoid errors, use relative paths,Directly copy the above Code to yourAndroidManifest. xmlMedium.

Step 5: Add code

Open the portal Activity of your project and insert the following code in its onCreate: If you use ShareSDK. xml to configure the call

ShareSDK.initSDK(this);

If you use the code to configure the Application Registration Information or the application background to configure the Application Registration Information, call

ShareSDK.initSDK(this,”androidv1101″);

Androidv1101: The AppKey returned by your application when the ShareSDK registers the application information. The Code initializes the ShareSDK, and then the ShareSDK operations are based on this.If not allShareSDKThis line of code is called before the operation, a null pointer exception will be thrown.

  :

 

 

Author: Jerry Education
Source: http://www.cnblogs.com/jerehedu/
Copyright Disclaimer: The copyright of this article is shared by Yantai jereh Education Technology Co., Ltd. and the blog Park. You are welcome to repost it. However, you must keep this statement without the consent of the author and provide the original article connection clearly on the article page, otherwise, you are entitled to pursue legal liability.
Technical Consultation:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.