> >

ShareSDK Android for JavaScript

1、通用集成步骤

1.1、下载ShareSDK for JS 组件

 

打开Github下载JSSample项目。将JSApi/ShareSDK.js拷贝到Web页面可以访问的路径(现在不支持浏览器打开的web应用,只支持App内置的Web页面,这个web页可以自己在本地也就是项目里创建html文件,这个html文件也可以网上得到)。

1.2、导入ShareSDK.js脚本

 

在Web页中加入ShareSDK.js脚本。如:

<script language="javascript" type="text/javascript" src="ShareSDK.js"></script>

初始化SDK

        function init()
        {          
        	//android的平台配置信息跟ShareSDK.xml一致;
            var sinaConf = 
            {
            "Id" : "1",
            "SortId" : "1",
            "AppKey" : "568898243",
            "AppSecret" : "38a4f8204cc784f81f9f0daaf31e02e3",
            "RedirectUrl" : "http://www.sharesdk.cn",
            "ShareByAppClient" : "false",
            "Enable" : "true"
            };
        	//platformConfig平台配置信息可为null,此时用ShareSDK.xml
			var platformConfig = {}; 
			platformConfig[$sharesdk.PlatformID.SinaWeibo] = sinaConf;
            $sharesdk.initSDKAndSetPlatfromConfig("androidv1101", platformConfig);

        }

如在代码中配置第三方平台初始化,初始化的参数可以参照这里

由于Android平台推荐使用shareSDK.xml文件配置应用信息。此文件可以在jsdemo中找到,将其放入您安卓项目的assets目录下即可。
js xml配置

1.3、分享内容

先构造分享内容结构,代码如下:

            var params = {
                "text" : "测试的文字",
                "imageUrl" : "http://img0.bdstatic.com/img/image/shouye/tangwei.jpg",
                "title" : "测试的标题",
                "titleUrl" : "http://sharesdk.cn",
                "description" : "测试的描述",
                "site" : "ShareSDK",
                "siteUrl" : "http://sharesdk.cn",
                "type" : $sharesdk.ContentType.Text
            };

构造分享内容字段参数值请参考:《分享内容参数字段表》

然后将内容传入分享接口

  • 弹出分享菜单
            $sharesdk.showShareMenu(null, params, 100, 100, function (reqId, platform, state, shareInfo, error) {

                alert("state = " + state + "\n shareInfo = " + shareInfo + "\n error = " + error);

            });
  • 弹出分享编辑界面
$sharesdk.showShareView($sharesdk.platformID.SinaWeibo, params, function (platform, state, shareInfo, error) {alert("state = " + state + "\nshareInfo = " + shareInfo + "\nerror = " + error);});
  • 直接分享
$sharesdk.shareContent($sharesdk.platformID.SinaWeibo, params, function (platform, state, shareInfo, error) {

alert("state = " + state + "\nshareInfo = " + shareInfo + "\nerror = " + error);

});
  • 一键分享
$sharesdk.oneKeyShareContent([$sharesdk.platformID.SinaWeibo, $sharesdk.platformID.TencentWeibo], params, function (platform, state, shareInfo, error) {

alert("state = " + state + "\nshareInfo = " + shareInfo + "\nerror = " + error);

});
关于具体的分享参数名称,可以查看下载SDK里的ShareSDK.JS文件里的配置;

1.4、用户授权

调用ShareSDK中的authorize方法可进行用户授权。代码如下:

$sharesdk.authorize($sharesdk.platformID.SinaWeibo, function (platform, state, error){

alert("state = " + state + "\nerror = " + error);

});

获取用户资料

调用ShareSDK中的getUserInfo来获取授权用户的信息。代码如下:

$sharesdk.getUserInfo($sharesdk.platformID.SinaWeibo, function (platform, state, user, error) {

alert("state = " + state + "\nuser = " + user + "\nerror = " + error);

});

 

2、Android部分集成步骤

导入的步骤如下:

1、参考”安卓快速集成文档“章节的说明,导入ShareSDK for Android的jar库和图片、文字资源。

2、复制github上的jsdemo项目中”cn.sharesdk.js”包中的源代码复制到你的项目中。如下图1就是了,如果需要onekeyshare的快捷分享则也需要将下图最下面2的部分复制到项目中;具体其他的集成步骤可以参考快速集成

111

 

QQ截图20150304142013

3、在您WebView加载页面地址之前。调用ShareSDKUtils.prepare(webview, webviewClient);

请注意,ShareSDKUtils.prepare方法要在WebView.setWebViewClient(WebViewClinet);之后调用,而且此后不能再调用setWebViewClient方法,否则ShareSDK的初始化操作可能失败。

下面的代码来自github上的demo:

public class MainActivity extends Activity {
       protected void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
               WebView wvBody = new WebView(this);
               WebViewClient wvClient = new WebViewClient();
               wvBody.setWebViewClient(wvClient);
               wvBody.setWebChromeClient(new WebChromeClient() {
                   public boolean onJsAlert(WebView view, String url, String message,
                            JsResult result) {
                        return super.onJsAlert(view, url, message, result);
	            }
	});