Android工程:Google - play pass:应用要求
设备/引擎:Mac(11.6)/cocos
开发工具:Android studio(4.1.2)
开发需求:根据Google-Play Pass要求更改代码
1.在工程->app->build.gradle中添加SDK
implementation 'com.github.google:play-licensing:-SNAPSHOT'
2.工程->build.gradle中添加
maven {
url 'https://jitpack.io'
}
3.在AndroidManifest.xml下添加
<uses-permission android:name="com.android.vending.CHECK_LICENSE" />
4.添加strings值:
<!-- License checking status messages -->
<string name="check_license">Check license</string>
<string name="checking_license">Checking license...</string>
<string name="dont_allow">Don\'t allow the user access</string>
<string name="allow">Allow the user access</string>
<string name="application_error">Application error: %1$d</string>
<string name="setting_tips_title">Choose an activity</string>
<string name="setting_tips_message">Please choose at least 1 activity to play.</string>
<!-- Unlicensed dialog messages -->
<string name="unlicensed_dialog_title">Application not licensed</string>
<string name="unlicensed_dialog_body">This application is not licensed.</string>
<string name="unlicensed_dialog_retry_body">Unable to validate license. Check to see if a network connection is available.</string>
<string name="restore_access_button">Restore Access</string>
<string name="retry_button">Retry</string>
<string name="quit_button">Exit</string>
5.修改状态栏颜色(一般会要求修改为浅色)
1)在color.xml下修改
<color name="colorAccent">#3F51B5</color>
2)在styles.xml下修改
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
6.找到工程的Cocos2dxActivity.java添加如下代码
import com.google.android.vending.licensing.AESObfuscator;
import com.google.android.vending.licensing.LicenseChecker;
import com.google.android.vending.licensing.LicenseCheckerCallback;
import com.google.android.vending.licensing.Policy;
import com.google.android.vending.licensing.ServerManagedPolicy;
1)在public abstract class Cocos2dxActivity extends Activity implements Cocos2dxHelperListener
中添加
private static final String BASE64_PUBLIC_KEY = "在google后台自己对应的app中查找";
// Generate your own 20 random bytes, and put them here.
private static final byte[] SALT = new byte[]{"用电脑终端生成"};
private LicenseChecker mCHecker;
private LicenseCheckerCallback mLicenseCheckerCallback;
public ServerManagedPolicy mServerManagedPolicy;
public static String STORAGE_NAME = "Dr.Dino";
public static String LISEN_KEY = "LicenseStatus";
public boolean isLicensed = false;
SALT值可以通过终端输入:python -c "import random; print ', '.join(str(random.randint(-128, 127)) for x in xrange(20))"
来生成
2)在public static Context getContext()
中添加
String deviceId = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
mLicenseCheckerCallback = new MyLicenseCheckerCallback();
// mServerManagedPolicy = new ServerManagedPolicy(this,new AESObfuscator(SALT, getPackageName(),deviceId));
mCHecker = new LicenseChecker(this,new ServerManagedPolicy(this,new AESObfuscator(SALT, getPackageName(),deviceId)),BASE64_PUBLIC_KEY);
doCheck();
3)在onResume、onPause、onDestroy中分别添加
if (this.mGLSurfaceView != null){
this.mGLSurfaceView.onResume();
}
if (this.mGLSurfaceView != null){
this.mGLSurfaceView.onPause();
}
mCHecker.onDestroy();
4)添加如下代码
public void checkCacheAllowCallback(){
if (isLicensed){
JoyPreschool.onLicenseCallback();
}else {
displayDialog(Policy.NOT_LICENSED == Policy.RETRY);
doCheck();
}
}
public void doCheck(){
mCHecker.checkAccess(mLicenseCheckerCallback);
}
protected Dialog onCreateDialog(int id) {
final boolean bRetry = id == 1;
AlertDialog alertDialog = new AlertDialog.Builder(this)
.setTitle(R.string.unlicensed_dialog_title)
.setMessage(bRetry ? R.string.unlicensed_dialog_retry_body : R.string.unlicensed_dialog_body)
.setPositiveButton(bRetry ? R.string.retry_button : R.string.restore_access_button,
new DialogInterface.OnClickListener() {
boolean mRetry = bRetry;
public void onClick(DialogInterface dialog, int which) {
if ( mRetry ) {
doCheck();
} else {
mCHecker.followLastLicensingUrl(Cocos2dxActivity.this);
System.exit(0);
}
}
})
.setNegativeButton(R.string.quit_button, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
System.exit(0);
}
}).create();
// alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).setGravity(Gravity.CENTER);
// alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).setWidth(250);
// alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE).setWidth(250);
return alertDialog;
}
public void displayDialog(final boolean showRetry) {
mHandler.post(new Runnable() {
public void run() {
setProgressBarIndeterminateVisibility(false);
showDialog(showRetry ? 1 : 0);
}
});
}
public void setPassStatus(Context context,int _keyValue)
{
SharedPreferences _sp = context.getSharedPreferences(STORAGE_NAME,context.MODE_PRIVATE);
SharedPreferences.Editor editor = _sp.edit();
editor.putInt(LISEN_KEY,_keyValue);
editor.commit();
}
private class MyLicenseCheckerCallback implements LicenseCheckerCallback {
public void allow(int policyReason){
if (isFinishing()) {
// Don't update UI if Activity is finishing.
return;
}
// Should allow user access.
Log.d(TAG, "policyReason=" + policyReason);
isLicensed = true;
// if (mServerManagedPolicy != null){
// long ts = System.currentTimeMillis();
// if (ts <= mServerManagedPolicy.getValidityTimestamp()) {
// Log.d(TAG, "getValidityTimestamp=" + mServerManagedPolicy.getValidityTimestamp());
// // Cached LICENSED response is still valid.
// }
// }
// setPassStatus(sContext,policyReason);
}
@Override
public void dontAllow(int reason) {
if (isFinishing()) {
// Don't update UI if Activity is finishing.
return;
}
Log.d(TAG, "dontAllow reason=" + reason);
isLicensed = false;
// setPassStatus(sContext,reason);
if (reason != Policy.RETRY){
mCHecker.followLastLicensingUrl(sContext);
Cocos2dxActivity.this.finish();
}else {
displayDialog(reason == Policy.RETRY);
}
}
@Override
public void applicationError(int errorCode) {
if (isFinishing()) {
// Don't update UI if Activity is finishing.
return;
}
isLicensed = false;
Log.d(TAG, "errorCode=" + errorCode);
// if (mServerManagedPolicy != null){
// if (mServerManagedPolicy.allowAccess()){
// JoyPreschool.onLicenseCallback();
// }else {
//
// String result = String.format(getString(R.string.application_error), errorCode);
// CharSequence charResult = result;
// Toast.makeText(sContext,charResult,Toast.LENGTH_SHORT).show();
// }
// }
mCHecker.followLastLicensingUrl(sContext);
System.exit(0);
// JoyPreschool.joylandInstance.checkLocalLicense();
}
;
}
7.在工程的Activity.java下添加
import com.google.android.vending.licensing.LicenseChecker;
import com.google.android.vending.licensing.LicenseCheckerCallback;
import com.google.android.vending.licensing.Policy;
public void checkLocalLicense(){
// SharedPreferences _sp = Cocos2dxActivity.getContext().getSharedPreferences(STORAGE_NAME,Cocos2dxActivity.getContext().MODE_PRIVATE);
// if (_sp != null){
// int curReason = _sp.getInt(LISEN_KEY,Policy.RETRY);
// if (curReason == Policy.LICENSED){
// JoyPreschool.onLicenseCallback();
// }else {
//
// Cocos2dxActivity.instance.displayDialog(curReason == Policy.RETRY);
// }
// }
Cocos2dxActivity.instance.checkCacheAllowCallback();
}
public static native void onLicenseCallback();
注:要求工程内所有可以点击的东西都有反馈(变色、放大缩小……),只有音效是不行的
希望能给大家带来帮助!!!有需要讨论交流的可以私信评论~~~