cocos creator获取安卓原生平台上的信息(ts)
说明
首先在cocos上构建项目,原生平台上的代码写在项目\build\jsb-link\frameworks\runtime-src\proj.android-studio\app\src\org\cocos2dx\javascript\AppActivity.java中。jsb.reflection.callStaticMethod是cocos调用原生平台的方法,第一个参数可以理解为文件地址,可以根据需求改变,第二个参数是方法名,也可改变,第三个参数是方法的参数类型,()Ljava/lang/String;代表的是这个方法不需要参数,返回string类型的参数。有需要的话可以参考其他文章去改变。
代码
获取当前手机系统版本(安卓版本)
jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "getSystemVersion", "()Ljava/lang/String;");
原生平台代码
public static String getSystemVersion() {
return android.os.Build.VERSION.RELEASE;
}
获取手机型号
jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "getSystemModel", "()Ljava/lang/String;");
原生平台代码
public static String getSystemModel() {
return android.os.Build.MODEL;
}
获取手机厂商
jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "getDeviceBrand", "()Ljava/lang/String;");
原生平台代码
public static String getDeviceBrand() {
return android.os.Build.BRAND;
}
获取手机语言
jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "getLanguage", "()Ljava/lang/String;");
原生平台代码
public static String getLanguage(){
return Locale.getDefault().toString();
}
获取手机AndroidID
jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "getAndroidId", "()Ljava/lang/String;");
原生平台代码
这里的getContentResolver方法是非静态的,不能直接写在静态方法里,我的处理方法是在AppActivity.java的onCreate方法中先获取到androidId,再去读取。
private static String androidId= "";
androidId = Settings.System.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
public static String getAndroidId(){
return androidId;
}
获取项目包名
jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "getAppId", "()Ljava/lang/String;");
原生平台代码
这里的getApplicationInfo方法是非静态的,不能直接写在静态方法里,我的处理方法是在AppActivity.java的onCreate方法中先获取到appId,再去读取。
private static String appId = "";
appId = context.getApplicationInfo().processName;
public static String getAppId(){
return appId;
}
获取手机ip地址
jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "getIP", "()Ljava/lang/String;");
原生平台代码
private static String ipAddress = "";
ipAddress = this.getIpAddress(this);
public static String getIP(){
return ipAddress;
}
public String getIpAddress(Context context) {
String ip = "";
ConnectivityManager conMann = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mobileNetworkInfo = conMann.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
NetworkInfo wifiNetworkInfo = conMann.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mobileNetworkInfo.isConnected()) {
ip = this.getLocalIpAddress();
}else if(wifiNetworkInfo.isConnected())
{
WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();
ip = intToIp(ipAddress);
}
return ip;
}
public String getLocalIpAddress() {
try {
Pattern ptipv4 = Pattern.compile("^(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}$");
String ipv4;
ArrayList<NetworkInterface> nilist = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface ni: nilist)
{
ArrayList<InetAddress> ialist = Collections.list(ni.getInetAddresses());
for (InetAddress address: ialist){
if (!address.isLoopbackAddress() && ptipv4.matcher(ipv4=address.getHostAddress()).matches())
{
return ipv4;
}
}
}
} catch (SocketException ex) {
Log.e("localip", ex.toString());
}
return null;
}
public static String intToIp(int ipInt) {
StringBuilder sb = new StringBuilder();
sb.append(ipInt & 0xFF).append(".");
sb.append((ipInt >> 8) & 0xFF).append(".");
sb.append((ipInt >> 16) & 0xFF).append(".");
sb.append((ipInt >> 24) & 0xFF);
return sb.toString();
}
|