获取IEMI需要开启权限获取设备id无需权限,所以建议获取设备id
首先构建项目,构建出安卓代码。
找到安卓代码里面的位置,我的文件地址是(\项目地址\build\jsb-link\frameworks\runtime-src\proj.android-studio\app\src\org\cocos2dx\javascript)
我这里是获取设备id的,无需开启权限,
获取设备id
修改AppActivity.java代码:
public static String imei="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Workaround in https://stackoverflow.com/questions/16283079/re-launch-of-activity-on-home-button-but-only-the-first-time/16447508
if (!isTaskRoot()) {
// Android launched another instance of the root activity into an existing task
// so just quietly finish and go away, dropping the user back into the activity
// at the top of the stack (ie: the last state of this task)
// Don't need to finish it again since it's finished in super.onCreate .
return;
}
// DO OTHER INITIALIZATION BELOW
SDKWrapper.getInstance().init(this);
imei = getIMEI(this);
}
/**
* 映射返回获取imei
* @param context
* @return
*/
public static String getIMEI2(String title, String message) {
return imei;
}
/**
*
* @param context
* @return
*/
public static String getIMEI(Context context) {
//获取设备的id
return Settings.Secure.getString(context.getApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID);
//获取IMEI,需要开启权限
// TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
TelephonyManager manager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
// try {
// Method method = manager.getClass().getMethod("getImei", int.class);
// String imei1 = (String) method.invoke(manager, 0);
// String imei2 = (String) method.invoke(manager, 1);
// if(TextUtils.isEmpty(imei2)){
// return imei1;
// }
// if(!TextUtils.isEmpty(imei1)){
// //因为手机卡插在不同位置,获取到的imei1和imei2值会交换,所以取它们的最小值,保证拿到的imei都是同一个
// String imei = "";
// if(imei1.compareTo(imei2) <= 0){
// imei = imei1;
// }else{
// imei = imei2;
// }
// return imei;
// }
// } catch (Exception e) {
e.printStackTrace();
// try {
// if (manager.getDeviceId() != null) {
// return manager.getDeviceId().toString();
// } else {
// return Settings.Secure.getString(context.getApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID);
// }
// } catch (Exception e2) {
// return Settings.Secure.getString(context.getApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID);
// }
// }
// return "";
}
然后在cocos creater代码里面直接调用:
//rtn 就是设备id,传参自己按照官方文档修改就行,,可以自己加标识
var rtn = jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "getIMEI2", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;", "title", "message");
如果需要获取IMEI
首先,添加安卓权限,我的项目地址:\项目地址\build\jsb-link\frameworks\runtime-src\proj.android-studio\app
找到AndroidManifest.xml 文件,添加权限
注意:为了防止没有授权app闪退,所以没授权 就获取设备id,所以一开始没有授权后面授权了,获取的id就会不一样
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
public static String getIMEI(Context context) {
// return Settings.Secure.getString(context.getApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID);
TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
// TelephonyManager manager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
try {
Method method = manager.getClass().getMethod("getImei", int.class);
String imei1 = (String) method.invoke(manager, 0);
String imei2 = (String) method.invoke(manager, 1);
if(TextUtils.isEmpty(imei2)){
return imei1;
}
if(!TextUtils.isEmpty(imei1)){
//因为手机卡插在不同位置,获取到的imei1和imei2值会交换,所以取它们的最小值,保证拿到的imei都是同一个
String imei = "";
if(imei1.compareTo(imei2) <= 0){
imei = imei1;
}else{
imei = imei2;
}
return imei;
}
} catch (Exception e) {
// e.printStackTrace();
try {
if (manager.getDeviceId() != null) {
return manager.getDeviceId().toString();
} else {
return Settings.Secure.getString(context.getApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID);
}
} catch (Exception e2) {
return Settings.Secure.getString(context.getApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID);
}
}
return "";
}
注意:混淆类名 或者读不到这个方法