InitActivity.java 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. package com.wdkl.callingbed.ui;
  2. import android.annotation.SuppressLint;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.content.SharedPreferences;
  6. import android.graphics.drawable.AnimationDrawable;
  7. import android.media.MediaPlayer;
  8. import android.media.MediaRecorder;
  9. import android.net.ConnectivityManager;
  10. import android.net.NetworkInfo;
  11. import android.os.Environment;
  12. import android.os.Handler;
  13. import android.os.Message;
  14. import android.text.TextUtils;
  15. import android.util.Log;
  16. import android.view.View;
  17. import android.widget.ImageView;
  18. import com.google.gson.Gson;
  19. import com.wdkl.callingbed.MyApplication;
  20. import com.wdkl.callingbed.R;
  21. import com.wdkl.callingbed.base.BaseActivity;
  22. import com.wdkl.callingbed.common.Constants;
  23. import com.wdkl.callingbed.entity.InitDataEntity;
  24. import com.wdkl.callingbed.entity.MessageEvent;
  25. import com.wdkl.callingbed.service.SoundPoolManager;
  26. import com.wdkl.callingbed.util.AutoRebootUtil;
  27. import com.wdkl.callingbed.util.CutSerialPortUtil;
  28. import com.wdkl.callingbed.util.DateUtil;
  29. import com.wdkl.callingbed.util.DownloadUtil;
  30. import com.wdkl.callingbed.util.LogUtil;
  31. import com.wdkl.callingbed.util.NetUtil;
  32. import com.wdkl.callingbed.util.ScreenManagerUtil;
  33. import com.wdkl.callingbed.util.SharedPreferencesUtil;
  34. import com.wdkl.callingbed.util.StringUtils;
  35. import com.wdkl.callingbed.util.ToastUtil;
  36. import com.wdkl.callingbed.util.UdpSendUtil;
  37. import com.wdkl.callingbed.util.ethernetwifiwithsipconnectstatus.WifiNet;
  38. import com.wdkl.callingbed.util.sendcommand.CallingBedSendCommand;
  39. import com.zhy.http.okhttp.OkHttpUtils;
  40. import com.zhy.http.okhttp.callback.StringCallback;
  41. import org.greenrobot.eventbus.EventBus;
  42. import org.greenrobot.eventbus.Subscribe;
  43. import org.greenrobot.eventbus.ThreadMode;
  44. import org.json.JSONObject;
  45. import java.io.File;
  46. import java.io.IOException;
  47. import butterknife.Bind;
  48. import butterknife.OnClick;
  49. import okhttp3.Call;
  50. import serialporttest.utils.SerialPortUtil;
  51. import static com.wdkl.callingbed.util.AutoRebootUtil.ethreboot;
  52. import static com.wdkl.callingbed.util.sendcommand.CallingBedSendCommand.closeHeart;
  53. import static com.wdkl.callingbed.util.sendcommand.CallingBedSendCommand.setNurseBrightness;
  54. /**
  55. * 类名称:InitActivity <br>
  56. * 类描述:APP初始化页面<br>
  57. * 创建人:Waderson <br>
  58. * 创建时间:2018-02-01 <br>
  59. *
  60. * @version V1.0
  61. */
  62. public class InitActivity extends BaseActivity implements SerialPortUtil.ISerialPortBedOnclickEvent, SerialPortUtil.ISerialPortBedOnclickString {
  63. @Bind(R.id.activity_init_layout_rl)
  64. View initView;
  65. @Bind(R.id.activity_init_layout_iv_loading)
  66. ImageView ivLoading;
  67. AnimationDrawable animationDrawable;
  68. ConnectivityManager cm;
  69. /**
  70. * mac地址
  71. */
  72. String macAddress = "";
  73. SerialPortUtil serialPortUtil;
  74. private static int countsReboot;//重启计数
  75. private boolean wifiEnabled;
  76. @Override
  77. public int getLayoutId() {
  78. return R.layout.activity_init_layout;
  79. }
  80. @Override
  81. protected void initView() {
  82. ivLoading.setVisibility(View.VISIBLE);
  83. animationDrawable = (AnimationDrawable) ivLoading.getBackground();
  84. animationDrawable.start();
  85. SoundPoolManager.getInstance().init();
  86. }
  87. @Override
  88. protected void initUtil() {
  89. serialPortUtil = MyApplication.serialPortUtil;
  90. serialPortUtil.setOnDataReceiveListener(this);
  91. serialPortUtil.setOnDataReceiveStringListener(this);
  92. startEnableWifi();
  93. }
  94. @Override
  95. protected void initData() {
  96. //刚进来的时候将所有的护理灯全部灭掉
  97. dismissNurseBrightness();
  98. //20190415 waderson
  99. ScreenManagerUtil.setScreenBrightness(context, 210);
  100. dateRefresh();
  101. }
  102. @Override
  103. public View getLoadingTargetView() {
  104. return initView;
  105. }
  106. @OnClick(R.id.activity_init_layout_rl)
  107. public void init(View view) {
  108. //isNewWork();
  109. }
  110. private void startEnableWifi() {
  111. wifiEnabled = WifiNet.getInstance().enableWifi();
  112. }
  113. /**
  114. * 获取网络请求地址头
  115. */
  116. private void getLocalWayAddress() {
  117. try {
  118. //Constants.URL = "http://" + NetUtil.getLocalElement(NetUtil.getLocalInetAddress().toString());
  119. Constants.URL = "http://" + NetUtil.getLocalElement(3);
  120. } catch (Exception e) {
  121. LogUtil.e("getLocalWayAddress", "Exception==" + e.toString());
  122. CallingBedSendCommand.setSipStatus(serialPortUtil, "0");
  123. showNetErrorView("MAC地址: " + macAddress + "\nError:请检查网络连接是否正常"
  124. + "\n" + AutoRebootUtil.getTextTip());
  125. e.printStackTrace();
  126. }
  127. }
  128. /**
  129. * 获取MAC地址
  130. */
  131. private void getMacAddress() {
  132. if (NetUtil.isethernetConnected(cm)) {
  133. macAddress = NetUtil.getMacAddress();
  134. if (null != macAddress) {
  135. SharedPreferences sharedPreferences = getSharedPreferences(Constants.MSG_SP, Context.MODE_PRIVATE); //私有数据
  136. SharedPreferences.Editor editor = sharedPreferences.edit();
  137. editor.putString("macAddress", macAddress);
  138. editor.commit();//提交修改
  139. }
  140. } else {
  141. SharedPreferences sharedPreferences = getSharedPreferences(Constants.MSG_SP, Context.MODE_PRIVATE); //私有数据
  142. macAddress = sharedPreferences.getString("macAddress", null);
  143. }
  144. LogUtil.d("getMacAddress", "macAddress==" + macAddress);
  145. Constants.MAC_ADDRESS = macAddress;
  146. }
  147. /**
  148. * 判断网络连接
  149. * 无网络显示错误页面
  150. */
  151. public void isNewWork() {
  152. //尝试连接wifi
  153. if (wifiEnabled) {
  154. boolean connect = WifiNet.getInstance().connectWifi("NCS", "23456789");
  155. }
  156. if (cm != null) {
  157. //ethreboot();//wuyq add
  158. String ipStr = NetUtil.getLocalIP();
  159. NetworkInfo networkInfo = cm.getActiveNetworkInfo();
  160. LogUtil.d("initwifi", "network info: " + networkInfo);
  161. if (networkInfo != null && networkInfo.isConnected()) { //wuyq modify
  162. LogUtil.d("Initwifi", "network type: " + networkInfo.getType() + ", network typeName: " + networkInfo.getTypeName());
  163. if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI || networkInfo.getType() == ConnectivityManager.TYPE_ETHERNET) {
  164. CallingBedSendCommand.setSipStatus(serialPortUtil, "2");
  165. getInitData(networkInfo.getTypeName());
  166. countsReboot = 0; //wuyq add
  167. } else {
  168. updateHandler.sendEmptyMessageDelayed(404, DATEREFRESH_DELAYMILLIS);
  169. showNetErrorView("MAC地址: " + macAddress + " IP地址: " + ipStr + "\nError:本地网络或wifi连接错误");
  170. ToastUtil.showToast("本地网络或wifi连接错误");
  171. CallingBedSendCommand.setSipStatus(serialPortUtil, "0");
  172. }
  173. } else {
  174. //updateHandler.sendEmptyMessageDelayed(404, DATEREFRESH_DELAYMILLIS);//wuyq remove
  175. updateHandler.sendEmptyMessageDelayed(4444, DATEREFRESH_DELAYMILLIS);//wuyq add
  176. CallingBedSendCommand.setSipStatus(serialPortUtil, "0");
  177. showNetErrorView("MAC地址: " + macAddress + " IP地址: " + ipStr + "\nError:无网络连接;网络是否连接正常?");//网络信息初始化错误
  178. ToastUtil.showToast("无网络连接;网络是否连接正常?");//网络信息初始化错误
  179. //autoReboot();//wuyq remove
  180. resetEthernet();
  181. }
  182. } else {
  183. updateHandler.sendEmptyMessageDelayed(404, DATEREFRESH_DELAYMILLIS);
  184. showNetErrorView("MAC地址: " + macAddress + "\nError:ConnectivityManager初始化失败");
  185. CallingBedSendCommand.setSipStatus(serialPortUtil, "0");
  186. ToastUtil.showToast("ConnectivityManager初始化失败");
  187. }
  188. }
  189. /**
  190. * =================================================(网络不通)15次以上自动重启系统=====================================================
  191. */
  192. private void resetEthernet() {
  193. if (countsReboot == 15) {
  194. updateHandler.sendEmptyMessage(222);
  195. countsReboot = 0;
  196. }
  197. countsReboot++;
  198. }
  199. /**
  200. * 检查APP更新版本
  201. *
  202. */
  203. private void appCheckUpdate() {
  204. if (!StringUtils.notEmpty(Constants.URL)) return;
  205. if (!StringUtils.notEmpty(Constants.PartID)) return;
  206. try {
  207. OkHttpUtils.post().url(Constants.URL + Constants.URL_END + Constants.APP_CHECK_UPDATE)
  208. .addParams("PartID", Constants.PartID)
  209. .build()
  210. .execute(new StringCallback() {
  211. @Override
  212. public void onError(Call call, Exception e, int id) {
  213. Constants.UPDATE_APP_FLAG = false;
  214. }
  215. @Override
  216. public void onResponse(String response, int id) {
  217. String data = response.substring(0, response.length() - 4);
  218. LogUtil.d("appCheckUpdate", "appCheckUpdate==" + data);
  219. try {
  220. JSONObject object = new JSONObject(data);
  221. if (object.getString("Code").equals("OK!")) {
  222. String APPVersion = StringUtils.deleteCharAt(object.getString("APPVersion"), 0);
  223. String APPVersion_Now = StringUtils.getAppVersionName(InitActivity.this);
  224. ToastUtil.showToast("当前版本: " + APPVersion_Now + ", 服务器版本: " + APPVersion);
  225. String downloadURL = object.getString("downloadURL");
  226. if (!APPVersion_Now.equals(APPVersion)) {//本来是“<”的;但有个别机器老是不升级成功
  227. if (StringUtils.notEmpty(downloadURL)) {
  228. closeHeart();//关闭心跳
  229. Intent intent = new Intent(InitActivity.this, APPUpdateActivity.class);
  230. intent.putExtra("downLoadURL", Constants.URL + Constants.URL_END + "/" + downloadURL);
  231. startActivity(intent);
  232. }
  233. }
  234. }
  235. } catch (Exception e) {
  236. e.printStackTrace();
  237. Constants.UPDATE_APP_FLAG = false;
  238. }
  239. }
  240. });
  241. } catch (Exception e) {
  242. Constants.UPDATE_APP_FLAG = false;
  243. e.printStackTrace();
  244. }
  245. }
  246. /**
  247. * 获取初始化信息
  248. */
  249. private void getInitData(final String type) {
  250. LogUtil.d("getInitData", "URL==" + Constants.URL + Constants.URL_END + Constants.CALLINGBED_INIT);
  251. final String ipStr = NetUtil.getLocalIP();
  252. //网络恢复正常,重置重启计数
  253. if (!TextUtils.isEmpty(ipStr)) {
  254. SharedPreferencesUtil.putIntSp(context, Constants.REBOOT_COUNT, Constants.REBOOT_COUNT, 0);
  255. }
  256. if (Constants.URL.length() > 7) {
  257. OkHttpUtils
  258. .post()
  259. .url(Constants.URL + Constants.URL_END + Constants.CALLINGBED_INIT)
  260. .addParams("deviceMAC", macAddress)
  261. .build()
  262. .execute(new StringCallback() {
  263. @Override
  264. public void onError(Call call, Exception e, int id) {
  265. ivLoading.setVisibility(View.INVISIBLE);
  266. dismissNurseBrightness();
  267. showNetErrorView("MAC地址: " + macAddress + " IP地址: " + ipStr + "\nError:" + "初始化数据请求错误[MAC地址是否注册?], " + type);
  268. //updateHandler.sendEmptyMessageDelayed(404, DATEREFRESH_DELAYMILLIS);//wuyq remove
  269. updateHandler.sendEmptyMessageDelayed(4444, DATEREFRESH_DELAYMILLIS);//wuyq add
  270. ToastUtil.showToast("初始化数据请求错误");
  271. }
  272. @Override
  273. public void onResponse(String response, int id) {
  274. String data = response.substring(0, response.length() - 4);
  275. ivLoading.setVisibility(View.INVISIBLE);
  276. LogUtil.d("getInitData", "getInitData==" + data);
  277. try {
  278. showContent();
  279. JSONObject object = new JSONObject(data);
  280. if (object.getString("Code").equals("ERROR!")) {
  281. dismissNurseBrightness();
  282. showNetErrorView("MAC地址: " + macAddress + " IP地址: " + ipStr + "\nError:" + "数据错误异常[ERROR!], " + type);
  283. updateHandler.sendEmptyMessageDelayed(404, DATEREFRESH_DELAYMILLIS);
  284. ToastUtil.showToast("数据请求失败");
  285. } else {
  286. if (object.getString("deviceStatus").equals("1")) {
  287. Gson gson = new Gson();
  288. InitDataEntity initDataEntity = gson.fromJson(data, InitDataEntity.class);
  289. saveData(initDataEntity);
  290. Intent intent = new Intent();
  291. intent.putExtra(Constants.INITENTITY, initDataEntity);
  292. intent.setClass(InitActivity.this, CallingBedActivity.class);
  293. startActivity(intent);
  294. InitActivity.this.finish();
  295. } else {
  296. dismissNurseBrightness();
  297. showNetErrorView("MAC地址: " + macAddress + " IP地址: " + ipStr + "\nError: " + "设备未启用, " + type);
  298. updateHandler.sendEmptyMessageDelayed(404, DATEREFRESH_DELAYMILLIS);
  299. ToastUtil.showToast("设备未启用");
  300. }
  301. }
  302. } catch (Exception e) {
  303. LogUtil.d("getInitData", "getInitData==Exception");
  304. showNetErrorView("MAC地址: " + macAddress + "\nError:" + "Response was Exception");
  305. e.printStackTrace();
  306. }
  307. }
  308. });
  309. }
  310. }
  311. private void saveData(InitDataEntity initDataEntity) {
  312. if (null != initDataEntity) {
  313. Constants.SIP_ID = initDataEntity.getDeviceSipId();
  314. Constants.SIP_PASS_WORD = initDataEntity.getDeviceSipPassWord();
  315. Constants.SIP_IP = initDataEntity.getDeviceSipIp();
  316. Constants.DEVICE_WIFI_HOST_NAME = initDataEntity.getDeviceWifiHostName();
  317. Constants.BED_ID = initDataEntity.getId();
  318. Constants.CALLMAIN_ID = initDataEntity.getDeviceHostingID();
  319. Constants.MYSELF_ID = initDataEntity.getId();
  320. Constants.ROOM_ID = initDataEntity.getDeviceRoomId();
  321. Constants.DEVICE_SCREEN_SLEEP = initDataEntity.getDeviceScreamSleep();
  322. Constants.DEVICE_HUMAN_ID = initDataEntity.getDeviceHumanId();
  323. Constants.PartID = initDataEntity.getPartId();
  324. LogUtil.d("saveData", "deviceHostingID==" + initDataEntity.getDeviceHostingID());
  325. if (StringUtils.notEmpty(initDataEntity.getDeviceHostingID())) {
  326. String n = StringUtils.substringByLengh(initDataEntity.getDeviceHostingID(), 0, 1);
  327. if ("#".equals(n)) {//服务器托管状态:处于托管中
  328. String nDis = StringUtils.deleteCharAt(initDataEntity.getDeviceHostingID(), 0);
  329. Constants.DEVICE_HOSTING_ID = nDis.split(",")[0];
  330. Constants.TRUST_NEW_MAIN_ID = nDis.split(",")[1];
  331. } else {//服务器托管状态:处于未托管
  332. Constants.DEVICE_HOSTING_ID = initDataEntity.getDeviceHostingID();
  333. Constants.TRUST_NEW_MAIN_ID = "";
  334. }
  335. }
  336. setSystemTime(initDataEntity);
  337. }
  338. }
  339. /**
  340. * 设置系统时间 (邓喆)
  341. *
  342. * @param initDataEntity
  343. */
  344. private void setSystemTime(InitDataEntity initDataEntity) {
  345. LogUtil.d("setSystemTime", initDataEntity.getCurTime());
  346. String[] time = initDataEntity.getCurTime().trim().split(" ");
  347. String[] day = time[0].split("/");
  348. String[] hour = time[1].split(":");
  349. //=================设置时间(可用)
  350. DateUtil.setSystemTime(this, Integer.valueOf(day[0]), Integer.valueOf(day[1]) - 1, Integer.valueOf(day[2])
  351. , Integer.valueOf(hour[0]), Integer.valueOf(hour[1]), Integer.valueOf(hour[2]));
  352. // if (!TimeUtil.isRoot()) {
  353. // String t = initDataEntity.getCurTime().trim().replaceAll("/", "").replace(":", "").replace(" ", ".");
  354. // if (t != null && t != "") {
  355. // TimeUtil.setSysDate(t);
  356. // }
  357. // }
  358. }
  359. @Override
  360. public void onReload() {
  361. super.onReload();
  362. dateRefresh();
  363. }
  364. public void dateRefresh() {
  365. cm = (ConnectivityManager) MyApplication.getAppContext().getSystemService(Context.CONNECTIVITY_SERVICE);
  366. if (null != cm) getMacAddress();
  367. getLocalWayAddress();
  368. isNewWork();
  369. }
  370. @Override
  371. protected void onDestroy() {
  372. super.onDestroy();
  373. animationDrawable.stop();
  374. updateHandler = null;
  375. serialPortUtil = null;//2018-01-10 add by Waderson
  376. }
  377. /**
  378. * 灭掉护理灯
  379. */
  380. public void dismissNurseBrightness() {
  381. new Thread(new Runnable() {
  382. @Override
  383. public void run() {
  384. try {
  385. setNurseBrightness(serialPortUtil, 1, "000000", "000000", "000000", "000000", "000000");
  386. } catch (InterruptedException e) {
  387. e.printStackTrace();
  388. }
  389. }
  390. }).start();
  391. }
  392. private boolean isRecord = false;
  393. private boolean isPlay = false;
  394. public static final String TEST_AUDIO_FILE_PATH = Environment.getExternalStorageDirectory().getPath() + "/test";
  395. private String audiofilePath;
  396. private MediaRecorder mediaRecorder;
  397. private MediaPlayer mediaPlayer;
  398. /**
  399. * 监听呼叫按钮 buffer[5] 如果按下则 放松注册信息给后台
  400. */
  401. @Override
  402. public void serialPortBedOnclick(byte[] buffer) {
  403. // mScreenExtinguishUtil.touchScreen();//===============================息屏
  404. //测试喇叭及网络
  405. if (buffer[4] == 1 || buffer[4] == 2) {
  406. //long curTime = System.currentTimeMillis();
  407. //if (curTime - clickTime < 2000) {
  408. SoundPoolManager.getInstance().playSound(3);
  409. new Thread(new Runnable() {
  410. @Override
  411. public void run() {
  412. final boolean pingStatus = NetUtil.ping("192.168.101.1", 2, null);
  413. if (pingStatus) {
  414. showTips("连接192.168.101.1成功!");
  415. } else {
  416. showTips("连接192.168.101.1失败, 请检查设备!");
  417. }
  418. }
  419. }).start();
  420. //}
  421. //clickTime = curTime;
  422. }
  423. //呼叫护士键短按/长按松开
  424. if (buffer[5] == 1 || buffer[5] == 2) {
  425. if (null != Constants.MAC_ADDRESS) {
  426. //有线mac地址,设备出场信息,无线mac地址
  427. final String str = "MGR_REG_A" + Constants.DELIMITER + Constants.MAC_ADDRESS + Constants.DELIMITER + "4" + Constants.DELIMITER + android.os.Build.DISPLAY +"_"+Constants.MCU_VERSION_NUMBER +
  428. Constants.DELIMITER + Constants.MAC_ADDRESS + Constants.DELIMITER + "FF:FF:FF:FF:FF:FF" + Constants.DELIMITER + SerialPortUtil.KEY_ID;
  429. UdpSendUtil.sendManualReboot(str);
  430. runOnUiThread(new Runnable() {
  431. @Override
  432. public void run() {
  433. ToastUtil.showToast("收到按键消息,发送注册信息:" + str);
  434. }
  435. });
  436. }
  437. }
  438. //测试麦克风录音
  439. if (buffer[6] == 2) {
  440. if (!isRecord && !isPlay) {
  441. startRecording();
  442. updateHandler.sendEmptyMessageDelayed(111, 3000);
  443. }
  444. }
  445. }
  446. @Override
  447. public void serialPortBedOnclickString(String str) {
  448. try {
  449. String newStr = CutSerialPortUtil.delHeadAndEnd(str, "$", "#");
  450. String content[] = newStr.split(",");
  451. LogUtil.d("serialPortBedOnclickString", "InitActivity newStr==" + newStr);
  452. switch (content[0]) {
  453. case "V":
  454. Constants.MCU_VERSION_NUMBER = content[1].substring(0, 14);
  455. LogUtil.d("serialPortBedOnclick", "Constants.MCU_VERSION_NUMBER==" + Constants.MCU_VERSION_NUMBER);
  456. break;
  457. }
  458. } catch (Exception e) {
  459. LogUtil.d("serialPortBedOnclickString", "Exception==");
  460. }
  461. }
  462. private void showTips(final String tips) {
  463. runOnUiThread(new Runnable() {
  464. @Override
  465. public void run() {
  466. ToastUtil.showToast(tips);
  467. }
  468. });
  469. }
  470. private void startRecording() {
  471. showTips("开始录音");
  472. new Thread(new Runnable() {
  473. @Override
  474. public void run() {
  475. isRecord = true;
  476. mediaRecorder = new MediaRecorder();
  477. try {
  478. audiofilePath = TEST_AUDIO_FILE_PATH + "/" + System.currentTimeMillis() + "_test.mp3";
  479. mediaRecorder.setOutputFile(audiofilePath);
  480. mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);// 设置MediaRecorder的音频源为麦克风
  481. mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);// 设置MediaRecorder录制的音频格式
  482. mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);// 设置MediaRecorder录制音频的编码为amr
  483. mediaRecorder.prepare();
  484. mediaRecorder.start();
  485. } catch (IOException e) {
  486. Log.i("error", "call startAmr(File mRecAudioFile) failed!" + e.getMessage());
  487. } finally {
  488. isRecord = false;
  489. }
  490. }
  491. }).start();
  492. }
  493. private void stopRecord() {
  494. showTips("结束录音");
  495. new Thread(new Runnable() {
  496. @Override
  497. public void run() {
  498. try {
  499. isPlay = true;
  500. mediaRecorder.stop();
  501. mediaRecorder.release();
  502. mediaRecorder = null;
  503. Thread.sleep(200);
  504. mediaPlayer = new MediaPlayer();
  505. mediaPlayer.reset();
  506. mediaPlayer.setDataSource(audiofilePath);
  507. mediaPlayer.setVolume(1.0f, 1.0f);
  508. mediaPlayer.prepare();
  509. mediaPlayer.start();
  510. mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
  511. @Override
  512. public void onCompletion(MediaPlayer player) {
  513. File file = new File(audiofilePath);
  514. if (file.exists()) {
  515. file.delete();
  516. }
  517. mediaPlayer.release();
  518. }
  519. });
  520. mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
  521. @Override
  522. public boolean onError(MediaPlayer player, int what, int extra) {
  523. File file = new File(audiofilePath);
  524. if (file.exists()) {
  525. file.delete();
  526. }
  527. mediaPlayer.release();
  528. return false;
  529. }
  530. });
  531. }catch (Exception e) {
  532. e.printStackTrace();
  533. } finally {
  534. isPlay = false;
  535. }
  536. }
  537. }).start();
  538. }
  539. @SuppressLint("HandlerLeak")
  540. Handler updateHandler = new Handler() {
  541. public void handleMessage(Message msg) {
  542. switch (msg.what) {
  543. case 404:
  544. dateRefresh();
  545. break;
  546. case 4444: // wuyq add
  547. ethreboot();
  548. dateRefresh();
  549. break;
  550. case 111:
  551. if (!isPlay) {
  552. stopRecord();
  553. }
  554. break;
  555. case 222:
  556. int count = SharedPreferencesUtil.getIntSp(context, Constants.REBOOT_COUNT, Constants.REBOOT_COUNT);
  557. LogUtil.d("wzlll", "NetReset reboot count: " + count);
  558. if (count >= 10) {
  559. ToastUtil.showToast("设备出现异常,请确保网络连接正常或重启设备");
  560. } else {
  561. count++;
  562. SharedPreferencesUtil.putIntSp(context, Constants.REBOOT_COUNT, Constants.REBOOT_COUNT, count);
  563. ToastUtil.showToast("网络异常,即将重启");
  564. updateHandler.sendEmptyMessageDelayed(333, 5000);
  565. }
  566. break;
  567. case 333:
  568. LogUtil.d("wzlll", "NetReset reboot...");
  569. AutoRebootUtil.reboot(MyApplication.getAppContext());
  570. CallingBedSendCommand.setNetStatus(serialPortUtil, "1");
  571. break;
  572. }
  573. }
  574. };
  575. public static final long DATEREFRESH_DELAYMILLIS = 10000;
  576. @Subscribe(threadMode = ThreadMode.MAIN)
  577. public void onMoonEvent(MessageEvent messageEvent) {
  578. switch (messageEvent.getType()) {
  579. case Constants.EVENT_MGR_APP_UPDATE://APP更新
  580. appCheckUpdate();
  581. break;
  582. }
  583. }
  584. }