联想平板产品序列号代表什么(联想平板 序列号)

android项目做好了,以后,所有pad设备,在springboot服务后端,需要实现监控,那么,这个时候就需要保活机制,这个保活机制,可以让服务端,实时监控,所有的pad设备的,工作状态.


实现思路:每个设备都有个唯一的 s/n 序列号,我们可以在android,端做个服务,或者,一个


Timer,每隔几秒,就发送保活数据,这个数据中包含sn序列号,给服务器端,让服务器端,知道该设备


是状态是可用状态,如果服务端长时间,收不到该设备的保活信息,就证明该设备可能已经关机,或者


我们的软件没有运行,或者设备故障.


1.先看如何来获取设备序列号,这个获取方式,每种设备有所不同,比如华为的平板的S/N,通过


这种方式就无法解析出来,我们用的是联想小新Pad:是可以的:


try { final String serialnoStr = "[ro.odm.lenovo.gsn]"; Process p = Runtime.getRuntime().exec("getprop"); p.waitFor(); BufferedReader stdInput = new BufferedReader(new InputStreamReader( p.getInputStream())); String temp = ""; while ((temp = stdInput.readLine()) != null) { if (temp.contains(serialnoStr)) { temp.replaceAll(" ", ""); int index = temp.indexOf(serialnoStr); temp = temp.substring(index + 20); temp = temp.substring(1, temp.length() - 1); Log.d("getSerialnoNumbers", temp); temp = temp.replaceAll("\\[",""); //1.首先获取了联想小新pad的,序列号. //2.然后把序列号保存到,配置文件中:unique_sn saveConfigValue("unique_sn", String.valueOf(temp)); //3.然后,我们去配置文件获取序列号,看有没有,如果有,弹出显示获取到的 //序列号 String sn = GetConfigValue.getConfigProperties("unique_sn"); if(StringUtils.isNotEmpty(sn)){ Toast.makeText(this,"当前设备序列号为:"+sn,Toast.LENGTH_LONG).show(); //ToastUtils.toast(CommonSettingActivity.this, "当前设备序列号为:"+sn); }else{ //4.如果没有序列号,就弹出找到序列号,然后把序列号保存到配置文件中 //5.然后再弹出显示找到的序列号 //SnowFlakeIdWorker idWorker = new SnowFlakeIdWorker(1, 0); //long snum = idWorker.nextId(); saveConfigValue("unique_sn", String.valueOf(temp)); Toast.makeText(this,"当前设备序列号为:"+String.valueOf(temp),Toast.LENGTH_LONG).show(); //ToastUtils.toast(CommonSettingActivity.this, "当前设备序列号为:"+String.valueOf(snum)); }// Message msg = new Message();// msg.obj = temp;// mHandler.sendMessage(msg); break; }else{ Log.d("getSerialnoNumbers1:", temp); } }



2.如果通过上面的方式没有找到序列号,说明就不是联想小新的pad,那么这时候,我们可以,自己利用


雪花算法,获取一个设备编码: 这样保证所有的设备都有自己的设备编码,因为这里,我们项目要求


统一使用,联想小新的pad,所以不存在,找不到序列号的情况,这里做个保守逻辑.


if(StringUtils.isEmpty(temp)){ //1.去配置文件获取序列号, String sn = GetConfigValue.getConfigProperties("unique_sn"); if(StringUtils.isNotEmpty(sn)){ //2.如果获取到序列号就显示 Toast.makeText(this,"当前设备序列号为:"+sn,Toast.LENGTH_LONG).show(); //ToastUtils.toast(CommonSettingActivity.this, "当前设备序列号为:"+sn); }else{//3.如果没有获取到序列号就生成一个,然后保存,然后再显示. SnowFlakeIdWorker idWorker = new SnowFlakeIdWorker(1, 0); long snum = idWorker.nextId(); saveConfigValue("unique_sn", String.valueOf(snum)); Toast.makeText(this,"当前设备序列号为:"+String.valueOf(snum),Toast.LENGTH_LONG).show(); //ToastUtils.toast(CommonSettingActivity.this, "当前设备序列号为:"+String.valueOf(snum)); } }



3.上面用到的,保存到配置文件中的工具方法这里再贴一遍,之前有贴过,关于配置文件使用的文章


从配置文件中获取值:


package com.baidu.idl.main.facesdk.utils;import java.io.File;import java.io.FileInputStream;import java.io.InputStream;import java.util.Properties;public class GetConfigValue { public static boolean isFileExists = false; public static Properties props = new Properties(); public static String getConfigProperties(String key) { String result = ""; try { String configPath = "/data/data/com.baidu.idl.face.demo/audiofile/config.properties"; File file = new File(configPath); if(file.exists()){ isFileExists =true; }else{ isFileExists =false; result = "noFile"; return result; } //初始化配置文件 String configValue = ""; InputStream inputStream = new FileInputStream(configPath); props.load(inputStream); configValue=props.getProperty(key); return configValue; } catch (Exception e1) { e1.printStackTrace(); return "exception"; } finally { } }}



4.然后保存文件:


//修改properties文件 private String saveConfigValue(String key, String value) { try { copyDbFile(CommonSettingActivity.this, "config.properties"); String configPath = "/data/data/" + CommonSettingActivity.this.getPackageName() + "/audiofile/config.properties"; InputStream inputStream = new FileInputStream(configPath); //初始化配置文件 Properties props = new Properties(); props.load(inputStream); props.setProperty(key, value); File file = new File(configPath); OutputStream fos = new FileOutputStream(file); props.store(fos, "Update '" + key + "' value"); fos.flush(); return value; } catch (Exception e1) { ToastUtils.toast(CommonSettingActivity.this, "配置信息保存失败"); return "-1"; } finally { } }



5.上面的代码可以根据自己需要稍加修改,下面是上面代码中需要的方法


private void copyDbFile(Context context, String fileName) { InputStream in = null; FileOutputStream out = null; String path = "/data/data/" + context.getPackageName() + "/audiofile/"; File file = new File(path + fileName); //创建文件夹 File filePath = new File(path); if (!filePath.exists()) filePath.mkdirs(); if (file.exists()) return; try { in = context.getAssets().open(fileName); // 从assets目录下复制 out = new FileOutputStream(file); int length = -1; byte[] buf = new byte[1024]; while ((length = in.read(buf)) != -1) { out.write(buf, 0, length); } out.flush(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (in != null) in.close(); if (out != null) out.close(); } catch (IOException e1) { e1.printStackTrace(); } } }



6.然后上面部分,获取到序列号以后,保活的时候,再把序列号发给springboot服务器端,去验证就可以了,这里可以用okhttpclient去发送请求,也可以用安卓原生HttpURLConnection 也可以.


可以开启一个服务,每隔几秒钟发送保活信息,也可以用timer,发送保活信息.