简介

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

一、插件短链:

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.tencent.tauth.AuthActivity"
        android:noHistory="true"
        android:launchMode="singleTask">
        <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="tencent1105439618" />
        </intent-filter>
    </activity>

    <activity
        android:name="com.tencent.connect.common.AssistActivity"
        android:screenOrientation="behind"
        android:theme="@android:style/Theme.Translucent.NoTitleBar"
        android:configChanges="orientation|keyboardHidden">
    </activity>

    <activity android:name="com.sina.weibo.sdk.component.WeiboSdkBrowser"
        android:configChanges="keyboardHidden|orientation"
        android:windowSoftInputMode="adjustResize" android:exported="false" >
    </activity>
    <activity
        android:name=".WBShareActivity"
        android:configChanges="keyboardHidden|orientation"
        android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="com.sina.weibo.sdk.action.ACTION_SDK_REQ_ACTIVITY" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

    <service
        android:name="com.sina.weibo.sdk.net.DownloadService"
        android:exported="false">
    </service>

3 其他配置:

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

四、插件调用

调用分享用到的字符串较多,PlugConstants已经在 六 附录中给出,参数收集帮助类 七 中给出

分享接口

uri=apkplug://umshare/rpc/share

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

PlugConstants.INIT:List>:平台初始化参数列表,其中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,可不传

通信接口方法:

public void share(HashMap params, Action2 callback)

回调中返回参数Boolean表示是否成功,String为具体信息

五、调用代码:

分享:

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();
BundleRPCAgent agent = new BundleRPCAgent(FrameworkFactory.getInstance().getFrame().getSystemBundleContext());
    try {
        IUMShare iumShare = agent.syncCall("apkplug://umshare/rpc/share", IUMShare.class);
        iumShare.share(hashMap, new Action2<Boolean, String>() {
            @Override
            public void call(Boolean aBoolean, String s) {
                Log.e("callback",aBoolean+" "+s);
            }
        });
    } catch (Throwable throwable) {
        throwable.printStackTrace();
    }

快速测试

可以通过如下接口一步从短链下载并测试rpc接口,短链为官方提供示例插件demo。

PlugManager.getInstance().getRPCInstanceByShortLink(true,“http://yyfr.net/q1t”, "apkplug://umshare/rpc/share", IUMShare.class, new OnGetRPCInstanceListener<IUMShare>() {
        @Override
        public void onDownloadProgress(String s, String s1, long l, long l1) {

            float percent = (float)l/(float) l1;
            Log.e("progress","percent:"+percent)
                      }

        @Override
        public void onInstallSuccess(org.osgi.framework.Bundle bundle) {
            Log.e("install s",bundle.getName());
        }

        @Override
        public void onGetRPCSuccess(IUMShare t) {
           //hashMap同上
            t.share(hashMap, new Action2<Boolean, String>() {
            @Override
            public void call(Boolean aBoolean, String s) {
                Log.e("callback",aBoolean+" "+s);
            }
        });

        }

        @Override
        public void onFail(String s, String s1) {
            Log.e("fail",s+" "+s1);
        }
    });

六、附录:

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;
}
}