说明:android一键分享微信和QQ等可以使用极光分享和友盟分享,由于极光主打远程推送所以选择了极光分享
一、集成准备
1、下载官网的Android版分享SDK 地址:https://www.jiguang.cn/share解压查看源码
2、在各个开放平台注册账号,创建项目App申请自己的AppID和AppSecret(demo中的微信key无法复制使用必须自己申请才能使用)
3、以微信为例:
微信开放平台地址: https://open.weixin.qq.com/
1)注册
2)创建移动应用
3)填写基本信息
说明:应用官网不知道怎么填的可以留言
4)填写平台信息
勾选android应用
说明:应用签名是去androidstudio生成的MD5去掉分号:
a)使用开发工具创建正式的签名证书
b)使用命令keytool -list -v -keystore 数字证书名称.jks(签名的秘钥)
c)复制出来MD5删除分号即可
5)等待3~7个工作日即可审核通过
二、极光SDK集成
1、在App的build.gradle中配置
android {
......
defaultConfig {
applicationId "com.xxx.xxx" //极光控制台创建应用时填写的应用包名.
......
ndk {
//选择要添加的对应cpu类型的.so库。
abiFilters 'armeabi', 'armeabi-v7a', 'armeabi-v8a'
// 还可以添加 'x86', 'x86_64', 'mips', 'mips64'
}
manifestPlaceholders = [
JSHARE_PKGNAME : applicationId,
JPUSH_APPKEY : "你的appkey", //极光控制台创建应用得到的AppKey.
JPUSH_CHANNEL : "developer-default", //暂时填写默认值即可.
TENCENT_APPID: "QQ开发者应用的appID",//腾讯开放平台注册应用得到的appId
FACEBOOK_APPID: "facebook开发者应用的appID",//facebook注册应用得到的appId
]
......
}
......
}
dependencies {
......
compile 'cn.jiguang.sdk:jshare:1.6.0' // 此处以JShare 1.6.0 版本为例,具体版本请参考压缩包libs的jar包版本。
compile 'cn.jiguang.sdk:jshare-qqmodel:1.6.0' // 此处以jshare-qqmodel 1.6.0 版本为例,具体版本请参考压缩包libs的jar包版本。
compile 'cn.jiguang.sdk:jshare-wechatmodel:1.6.0' // 此处以jshare-wechatmodel 1.6.0 版本为例,具体版本请参考压缩包libs的jar包版本。
compile 'cn.jiguang.sdk:jshare-sinamodel:1.6.0' // 此处以jshare-sinamodel 1.6.0 版本为例,具体版本请参考压缩包libs的jar包版本。
compile 'cn.jiguang.sdk:jshare-facebookmodel:1.6.0' // 此处以jshare-facebookmodel 1.6.0 版本为例,具体版本请参考压缩包libs的jar包版本。
compile 'cn.jiguang.sdk:jshare-twittermodel:1.6.0' // 此处以jshare-twittermodel 1.6.0 版本为例,具体版本请参考压缩包libs的jar包版本。
compile 'cn.jiguang.sdk:jshare-jchatpromodel:1.6.0' // 此处以jshare-twittermodel 1.6.0 版本为例,具体版本请参考压缩包libs的jar包版本。
compile 'cn.jiguang.sdk:jcore:1.2.3' // 此处以JCore 1.2.3版本为例,具体版本请参考压缩包libs的jar包版本。
......
}
注 : 如果在添加以上 abiFilter 配置之后android Studio出现以下提示:
NDK integration is deprecated in the current plugin. Consider trying the new experimental plugin.
则在 Project 根目录的gradle.properties文件中添加:
android.useDeprecatedNdk=true
2、配置AndroidManifest.xml
根据 SDK 压缩包里的 AndroidManifest.xml 样例文件,来配置应用程序项目的 AndroidManifest.xml 。
- 复制备注为 "Required" 的部分
- 将标注为“您应用的包名”的部分,替换为当前应用程序的包
- 将标注为“您应用的Appkey”的部分,替换为在Portal上注册该应用的的Key,例如: 9fed5bcb7b9b87413678c407
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="130"
android:versionName="1.3.0"
package="您应用的包名">
<!-- Required -->
<uses-permission android:name="android.permission.RECEIVE_USER_PRESENT" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!-- Optional. Required for location feature -->
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_TASKS" />
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SelectPlatActivity"/>
<activity android:name=".ShareTypeActivity"/>
<!-- Required SDK核心功能-->
<activity
android:name="cn.jiguang.share.android.ui.JiguangShellActivity"
android:exported="true"
android:launchMode="singleTask"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:windowSoftInputMode="stateHidden|adjustResize">
<!-- Optional QQ分享回调-->
<!-- scheme为“tencent”前缀再加上QQ开发者应用的appID;例如appID为123456,则scheme=“tencent123456” -->
<intent-filter>
<data android:scheme="tencent+appID" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<!-- Optional 新浪微博分享回调 -->
<intent-filter>
<action android:name="com.sina.weibo.sdk.action.ACTION_SDK_REQ_ACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<!-- Optional 新浪微博私信回调-->
<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="jsharesdk" android:host="sinaweibo"/>
</intent-filter>
</activity>
<!-- Optional 微信分享回调,wxapi必须在包名路径下,否则回调不成功-->
<activity
android:name=".wxapi.WXEntryActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:exported="true" />
<!-- Required. For publish channel feature -->
<!-- JPUSH_CHANNEL 是为了方便开发者统计APK分发渠道。-->
<!-- 例如: -->
<!-- 发到 Google Play 的APK可以设置为 google-play; -->
<!-- 发到其他市场的 APK 可以设置为 xxx-market。 -->
<!-- 目前这个渠道统计功能的报表还未开放。-->
<meta-data
android:name="JPUSH_CHANNEL"
android:value="developer-default" />
<!-- Required. AppKey copied from Portal -->
<meta-data
android:name="JPUSH_APPKEY"
android:value="您应用的Appkey" />
</application>
</manifest>
3、配置第三方平台信息
1.5.0版本开始支持两种方式配置第三方平台信息:1、配置JGShareSDK.xml;2、在代码中设置。开发者应选择其中一种方式,1.5.0之前版本只支持配置JGShareSDK.xml的方式。(建议选择第一种)
无论是使用自动集成还是手动集成方式,都需要配置JGShareSDK.xml。 主要步骤为:
- 复制或者新建JGShareSDK.xml到工程目录的asset目录下。
- 把JGShareSDK.xml中相关的AppKey、AppSecret替换成自己在第三方平台创建的应用得到的信息。
- 根据需要配置各个平台,不需要的平台可以删除。
<?xml version="1.0" encoding="utf-8"?>
<DevInfor>
<!-- 如果不需要支持某平台,可缺省该平台的配置-->
<SinaWeibo
AppKey="新浪微博的AppKey"
AppSecret="新浪微博ppSecret"
RedirectUrl="微博开放平台填写的授权回调页"/>
<QQ
AppId="QQ 的 AppId"
AppKey="QQ 的 AppKey"/>
<Wechat
AppId="微信的 AppId"
AppSecret=" 微信的 AppSectet"/>
<Facebook
AppId="facebook 的 appId"
AppName="facebook 后台填写的名称"
/>
<Twitter
ConsumerKey="twitter 的 ConsumerKey"
ConsumerSecret="twitter 的 ConsumerSecret"
/>
<!-- 趣聊平台appkey的申请, 请查看入门指南的第三方平台申请文档 -->
<JChatPro
auth="申请后的 appkey"
/>
</DevInfor>
4、配置微信平台回调
- 在你的包名相应目录下新建一个wxapi目录,并在该wxapi目录下新增一个WXEntryActivity类,该类继承自WeChatHandleActivity(例如应用程序的包名为cn.jiguang.share.demo,则新添加的类如下图所示)
/**
* 微信客户端回调activity示例
*/
public class WXEntryActivity extends WeChatHandleActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
}
}
- 并在manifest文件里面加上exported属性,设置为true,例如:
<activity
android:name=".wxapi.WXEntryActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:exported="true" />
5、配置项目签名
androidstudio图形界面签名配置
6、配置混淆
-dontwarn cn.jiguang.**
-keep class cn.jiguang.** { *; }
-dontwarn cn.jpush.**
-keep class cn.jpush.** { *; }
-keep public class com.sina.** {
*;
}
7、SDK的API各个参数说明
链接地址:https://docs.jiguang.cn/jshare/client/Android/android_api/
8、在Application中进行初始化
/**
* 极光分享
*/
JShareInterface.setDebugMode(true);
PlatformConfig platformConfig = new PlatformConfig()
.setWechat("申请的AppID", "申请的APPSecret")
.setQQ("1106011004", "YIbPvONmBQBZUGaN");
// .setSinaWeibo("374535501", "baccd12c166f1df96736b51ffbf600a2", "https://www.jiguang.cn");
// .setFacebook("1847959632183996", "JShareDemo")
// .setTwitter("eRJyErWUhRZVqBzADAbUnNWx5", "Oo7DJMiBwBHGFWglFrML1ULZCUDlH990RlJlQDdfepm3lToiMC");
JShareInterface.init(this, platformConfig);
Activity中的代码
private ShareBoard mShareBoard;
//显示底部图标
private int mAction = Platform.ACTION_SHARE;
private ShareBoard mShareBoard;
private ProgressDialog progressDialog;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
String toastMsg = (String) msg.obj;
Toast.makeText(MainActivity.this, toastMsg, Toast.LENGTH_SHORT).show();
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
};
/**
* 展示底部栏
*/
private void showBroadView() {
if (mShareBoard == null) {
mShareBoard = new ShareBoard(this);
List<String> platforms = JShareInterface.getPlatformList();
platforms.remove(2);
if (platforms != null) {
for (Object temp : platforms) {
SnsPlatform snsPlatform = createSnsPlatform(String.valueOf(temp));
//把数据添加到显示板
mShareBoard.addPlatform(snsPlatform);
}
}
/**
* 显示板设置点击事件
*/
mShareBoard.setShareboardclickCallback(mShareBoardlistener);
}
//展示
mShareBoard.show();
}
/**
* 点击事件监听
*/
private ShareBoardlistener mShareBoardlistener = new ShareBoardlistener() {
@Override
public void onclick(SnsPlatform snsPlatform, String platform) {
switch (mAction) {
case Platform.ACTION_SHARE:
// progressDialog.show();
//这里以分享链接为例
ShareParams shareParams = new ShareParams();
shareParams.setShareType(Platform.SHARE_WEBPAGE);//
shareParams.setTitle(ShareTypeActivity.share_title);//
shareParams.setText(ShareTypeActivity.share_text);//
shareParams.setShareType(Platform.SHARE_WEBPAGE);//
shareParams.setUrl(ShareTypeActivity.share_url);//
shareParams.setImagePath(MyApplication.ImagePath);//
JShareInterface.share(platform, shareParams, mShareListener);
break;
default:
break;
}
}
};
/**
* 分享监听, 失败,成功,取消
*/
private PlatActionListener mShareListener = new PlatActionListener() {
@Override
public void onComplete(Platform platform, int action, HashMap<String, Object> data) {
if (handler != null) {
Message message = handler.obtainMessage();
message.obj = "分享成功";
handler.sendMessage(message);
}
}
@Override
public void onError(Platform platform, int action, int errorCode, Throwable error) {
String TAG = "MainActivity";
Logger.e(TAG, "error:" + errorCode + ",msg:" + error);
if (handler != null) {
Message message = handler.obtainMessage();
message.obj = "分享失败:" + error.getMessage() + "---" + errorCode;
handler.sendMessage(message);
}
}
@Override
public void onCancel(Platform platform, int action) {
if (handler != null) {
Message message = handler.obtainMessage();
message.obj = "分享取消";
handler.sendMessage(message);
}
}
};
/**
* 获取图标和文字
*
* @param platformName
* @return
*/
public static SnsPlatform createSnsPlatform(String platformName) {
String mShowWord = platformName;
String mIcon = "";
String mGrayIcon = "";
//微信
if (Wechat.Name.equals(platformName)) {
mIcon = "jiguang_socialize_wechat";
mGrayIcon = "jiguang_socialize_wechat";
mShowWord = "jiguang_socialize_text_weixin_key";
//微信朋友圈
} else if (WechatMoments.Name.equals(platformName)) {
mIcon = "jiguang_socialize_wxcircle";
mGrayIcon = "jiguang_socialize_wxcircle";
mShowWord = "jiguang_socialize_text_weixin_circle_key";
//微信收藏
} else if (WechatFavorite.Name.equals(platformName)) {
mIcon = "jiguang_socialize_wxfavorite";
mGrayIcon = "jiguang_socialize_wxfavorite";
mShowWord = "jiguang_socialize_text_weixin_favorite_key";
//QQ分享
} else if (QQ.Name.equals(platformName)) {
mIcon = "jiguang_socialize_qq";
mGrayIcon = "jiguang_socialize_qq";
mShowWord = "jiguang_socialize_text_qq_key";
//QQ空间分享
} else if (QZone.Name.equals(platformName)) {
mIcon = "jiguang_socialize_qzone";
mGrayIcon = "jiguang_socialize_qzone";
mShowWord = "jiguang_socialize_text_qq_zone_key";
}
return ShareBoard.createSnsPlatform(mShowWord, platformName, mIcon, mGrayIcon, 0);
}
以及ListItemView类
public class ListItemView extends RelativeLayout {
private static final String TAG="ListItemView";
public ListItemView(Context context) {
super(context);
}
public ListItemView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ListItemView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void init(String title){
TextView textView = new TextView(getContext());
textView.setText(title);
textView.setTextSize(18);
textView.setGravity(Gravity.CENTER);
RelativeLayout.LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
layoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
layoutParams.setMargins(10,10,0,10);
addView(textView,layoutParams);
TextView right = new TextView(getContext());
right.setText(">");
RelativeLayout.LayoutParams rightParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
rightParams.addRule(RelativeLayout.CENTER_VERTICAL);
rightParams.setMargins(0,0,10,0);
addView(right,rightParams);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec),100);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
9、效果