本客服平台由提供产品服务

分享插件

Apkplug-客服支持小芳 发表于 2016年08月30日 13:46 最后修改于 2016年09月06日 14:34

描述:插件基于友盟分享实现,包括微信分享、qq分享、qq空间分享和新浪微博分享功能。有效减少apk包体大小和放法数。

*请确定apkplug相关初始化及配置已经完成,否则参考http://console.apkplug.com/documents/apkplug

一、插件短链:

http://yyfr.net/q1t

源码地址:https://github.com/apkplug/plugspace/tree/master/UMSharePlugUser

二、宿主配置:

1 权限配置:


<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_LOGS" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.SET_DEBUG_APP" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />

2 组件配置:

    <activity android:name="com.apkplug.umshareplug.MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name="com.umeng.socialize.editorpage.ShareActivity"
        android:theme="@style/Theme.UMDefault"
        android:excludeFromRecents="true" />

    <activity android:name="com.tencent.tauth.AuthActivity"
        android:launchMode="singleTask" android:noHistory="true" >
        <intent-filter> <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="tencent100424468" />
        </intent-filter>
    </activity>
    <activity android:name="com.tencent.connect.common.AssistActivity"
        android:screenOrientation="portrait"
    android:theme="@android:style/Theme.Translucent.NoTitleBar" />

    <activity android:name="com.sina.weibo.sdk.component.WeiboSdkBrowser"
        android:configChanges="keyboardHidden|orientation"
        android:windowSoftInputMode="adjustResize" android:exported="false" >
</activity>

下面这个填写宿主的包名
    <activity
        android:name="---yourpackage---.wxapi.WXEntryActivity"
        android:configChanges="keyboardHidden|orientation|screenSize"
        android:exported="true"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.Translucent.NoTitleBar" />


3 其他配置:


<meta-data android:name="UMENG_APPKEY" android:value="xxxx" > </meta-data>  友盟申请的key


三、插件调用

先调用初始化,再调用分享(用到的字符串较多,PlugConstants已经在 六 附录中给出)

初始化uri=apkplug://umshare/init 
传入参数:

返回参数:
object[0]:boolean:是否初始化成功:如果是false:object[1]为exception


分享uri=apkplug://umshare/share 

传入参数:(可参考后面的具体代码)

PlugConstants.INIT:List<HashMap<String,Object>>:平台初始化参数列表,其中list的每个hashmap配置三项:
1 PlugConstants.PLATFORM:PlugConstants.PLATFORM_WIEXIN|PlugConstants.PLATFORM_QQ|PlugConstants.PLATFORM_QZONE|PlugConstants.PLATFORM_SINA
2 PlugConstants.APPID:String:此平台的appid
3 PlugConstants.APPKEY:String:此平台的appkey

PlugConstants.TEXT:String:分享的内容
PlugConstants.SHARE_MEDIAS:String[]:new String[]{PlugConstants.WEIXIN,PlugConstants.QQ,PlugConstants.QZONE,PlugContstants.SINA},可选其中一到四个,要分享的平台
PlugConstants.TARGET_URL:String:要分享的url,可不传
PlugConstants.IMAGE_URL:String:要分享的图片的url,可不传

返回参数:

object[0]:boolean:是否初始化成功:如果是false:object[1]为exception或失败原因的字符串


五、调用代码:

分享:

ParamsHelper paramsHelper = ParamsHelper.getInstance();
paramsHelper.putInitParam(PlugConstants.PLATFORM_WIEXIN,"xxx","xxx");
paramsHelper.putInitParam(PlugConstants.PLATFORM_QQ,"xxx","xxx");
paramsHelper.putInitParam(PlugConstants.PLATFORM_SINA,"xxx","xxx");
paramsHelper.setShareTitle("fasdfas");
paramsHelper.setShareText("fasdfasfasfasfsafaf");
paramsHelper.setShareTargetUrl("https://baidu.com");
paramsHelper.setShareMediars(new String[]{PlugConstants.WEIXIN,PlugConstants.QQ,PlugConstants.QZONE,PlugConstants.SINA});

HashMap<String,Object> hashMap = paramsHelper.getParams();
dispatchAgent.call("apkplug://umshare/share", hashMap, new WorkerCallback() {
    @Override
    public void reply(URI uri, Object... objects) throws Exception {
        if((boolean)objects[0]){
            Log.e("call s","ssss:"+objects[1]);
            Toast.makeText(MainActivity.this,"fenxiangchenggong",Toast.LENGTH_LONG).show();
        }else {
            Log.e("call fail","call fail "+objects[1]);
        }
    }

    @Override
    public void timeout(URI uri) throws Exception {
    }

    @Override
    public void Exception(URI uri, Throwable throwable) {
        Log.e("call f","call fail "+throwable.getMessage());
    }
});


六、附录:

public class PlugConstants {

    public static final String INIT = "init";
    public static final String PLATFORM_WIEXIN = "weixin";
    public static final String PLATFORM_QQ = "QQ";
    public static final String PLATFORM_SINA = "sina";

    public static final String APPID = "appId";
    public static final String APPKEY = "appKey";
    public static final String PLATFORM = "platform";


    public static final String ACTIVITY = "activity";
    public static final String TEXT = "text";
    public static final String IMAGE_RES = "image-res";
    public static final String IMAGE_URL = "image-url";
    public static final String TARGET_URL = "targetUrl";
    public static final String TITLE = "title";

    public static final String WEIXIN = "weixin";
    public static final String SINA = "sina";
    public static final String QQ = "QQ";
    public static final String QZONE = "QQzone";

    public static final String SHARE_MEDIAS = "share-medias";

    public static final String PLATEPORMS = "plateforms";
}


七、收集参数的帮助类


public class ParamsHelper {
    private HashMap<String,Object> params;
    private List<HashMap<String,Object>> platforms;

    private static ParamsHelper instance;

    public static ParamsHelper getInstance(){

        if(instance == null){
            synchronized (ParamsHelper.class){
                if(instance == null){
                    instance = new ParamsHelper();
                }
            }
        }

        return instance;
    }

    private ParamsHelper(){
        params = new HashMap<>();
        platforms = new ArrayList<>();
    }

    public void putSharePara(String key,Object value){
        params.put(key,value);
    }

    public void setShareTitle(String title){
        params.put(PlugConstants.TITLE,title);
    }

    public void setShareText(String text){
        params.put(PlugConstants.TEXT,text);
    }

    public void setShareTargetUrl(String url){
        params.put(PlugConstants.TARGET_URL,url);
    }

    public void setShareImage(String url){
        params.put(PlugConstants.IMAGE_URL,url);
    }

    public void setShareMediars(String[] mediars){
        params.put(PlugConstants.SHARE_MEDIAS,mediars);
    }

    public void putInitParam(String platform,String appId,String appKey){
        HashMap<String,Object> platformMap = new HashMap<>();
        platformMap.put(PlugConstants.PLATFORM,platform);
        platformMap.put(PlugConstants.APPID,appId);
        platformMap.put(PlugConstants.APPKEY,appKey);
        platforms.add(platformMap);
    }

    public void cleanPlateforms(){
        platforms.clear();
    }

    public void cleanParams(){
        params.clear();
    }

    public HashMap<String,Object> getParams(){
        params.put(PlugConstants.INIT,platforms);
        return params;
    }
}

回复

您需要登录后才可以回复