InitActivity.java 27 KB

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