# GHOME Share 开发手册 for Android
# 1. 前言
本文用于指导游戏开发商接入G家分享SDK,文中包含客户端的接入说明(导入 sdk包内的 gunionshare_vXXX.aar / gshare_vXXX.aar. + greport_XXX.aar即可)
# 2. GShare(分享功能,按需选接)
# 2.1. 分享相关的AndroidManifest.xml 配置项:
<!-- gshare权限申请 (manifest标签内) 有录屏分享功能才需要添加 start -->
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PROJECTION"
android:minSdkVersion="34"/>
<!-- gshare权限申请(manifest标签内) end -->
<!-- GShare (application标签内)start -->
<!-- provider 方式分享 authorities 配置为: 包名.gshare.fileprovider-->
<provider
android:name="com.ghome.gshare.GShareProvider"
android:authorities="${applicationId}.gshare.fileprovider"
android:exported="false"
android:grantUriPermissions="true" >
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/share_file_provider_paths" />
</provider>
<!-- provider 方式分享 -->
<!--录屏功能所需的service-->
<service android:name="com.ghome.gshare.record.RecordService"
android:enabled="true"
android:exported="true"
android:foregroundServiceType="mediaProjection">
</service>
<!--录屏功能所需的service-->
<!-- GShare(application标签内) end -->
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 2.2. 分享相关生命周期接口
注意:
游戏主activity中接入
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//gshare
GShareInterface.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
//gshare
GShareInterface.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
@Override
protected void onDestroy() {
super.onDestroy();
//gshare
GShareInterface.free();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 2.3. 分享接口入参说明
分享类型:
GSharePlatformType sharePlatform = GSharePlatformType.WEIXIN_PLATFORM;
public enum GSharePlatformType
{
WEIXIN_PLATFORM,//微信
WEIXIN_CIRCLE_PLATFORM,//微信朋友圈
QQ_PLATFORM,//QQ
QZONE_PLATFORM,//QQ空间
SINA_WB_PLATFORM,//新浪微博
SDK_CHOOSE_PLATFORM;//弹出选择第三方平台UI
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
分享监听(系统分享调用即成功,所以这个回调是调用接口即返回):
MyShareListener shareListener;
// 分享监听
public class MyShareListener implements GShareListener {
@Override
public void onComplete(GSharePlatformType platformType) {
Toast.makeText(mContext, platformType + "share onComplete", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(GSharePlatformType platformType, String strErrMsg) {
Toast.makeText(mContext, platformType + " share onError:" + strErrMsg, Toast.LENGTH_SHORT).show();
}
@Override
public void onCancel(GSharePlatformType platformType) {
Toast.makeText(mContext, platformType + " share onCancel", Toast.LENGTH_SHORT).show();
}
@Override
public void onOpen(GSharePlatformType platformType) {
Toast.makeText(mContext, platformType + " app onOpen", Toast.LENGTH_SHORT).show();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 2.4. 分享文字 (sharePlatform:微信, QQ, QQ空间,新浪微博,平台自动选择)
GShareTextMedia shareMedia = new GShareTextMedia();
((GShareTextMedia) shareMedia).setText("分享文字测试");
GShareInterface.getInstance(mContext).doShareWithSystemPrimal(mContext, sharePlatform, shareMedia, shareListener);
1
2
3
4
2
3
4
# 2.5. 分享图片 (sharePlatform:微信, 微信朋友圈,QQ, QQ空间,新浪微博,平台自动选择)
shareMedia = new GShareSystemPrimalImageMedia();
((GShareSystemPrimalImageMedia) shareMedia).setDescription("图片分享");
((GShareSystemPrimalImageMedia) shareMedia).addMediaPath(mContext.getExternalFilesDir(null).toString() + "/" + "gshare1.jpg");
GShareInterface.getInstance(mContext).doShareWithSystemPrimal(mContext, sharePlatform, shareMedia, shareListener);
1
2
3
4
5
2
3
4
5
# 2.6. 分享视频 (sharePlatform: 微信,QQ,平台自动选择)
shareMedia = new GShareSystemPrimalVideoMedia();
((GShareSystemPrimalVideoMedia) shareMedia).setDescription("可爱的狗子");
((GShareSystemPrimalVideoMedia) shareMedia).addMediaPath(mContext.getExternalFilesDir(null).toString() + "/" + "gshare1.mp4");
GShareInterface.getInstance(mContext).doShareWithSystemPrimal(mContext, sharePlatform, shareMedia, shareListener);
1
2
3
4
5
2
3
4
5
# 2.7. 分享音乐 (sharePlatform:QQ,平台自动选择)
shareMedia = new GShareSystemPrimalMusicMedia();
((GShareSystemPrimalMusicMedia) shareMedia).setDescription("音乐分享.");
((GShareSystemPrimalMusicMedia) shareMedia).addMediaPath(mContext.getExternalFilesDir(null).toString() + "/" + "gshare1.mp3");
GShareInterface.getInstance(mContext).doShareWithSystemPrimal(mContext, sharePlatform, shareMedia, shareListener);
1
2
3
4
5
2
3
4
5
# 2.8. 分享录屏
// 初始化视频录制服务
GShareInterface.initRecordService(mContext);
MyShareListener shareListener = new MyShareListener();
GShareRecordListener myListener = new GShareRecordListener() {
@Override
public void onStartRecordSuccess() {
((Button) view).setText("结束并分享");
}
@Override
public void onStartRecordFailed(String message) {
Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();
}
@Override
public void onEndRecordSuccess(final String videoPath) {
Toast.makeText(mContext, "录屏成功" + videoPath, Toast.LENGTH_SHORT).show();
}
@Override
public void onEndRecordFailed(String message) {
Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();
}
};
if (((Button) view).getText().toString().equals("录屏开始")) {
GShareInterface.startRecord(myListener);
} else {
((Button) view).setText("录屏开始");
GShareInterface.stopRecordAndShare(mContext, shareListener);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
← 买量配置问题 分享 iOS SDK 说明 →