InitActivity.java 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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 = false;
  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.removeCallbacksAndMessages(null);
  382. updateHandler = null;
  383. serialPortUtil = null;//2018-01-10 add by Waderson
  384. }
  385. /**
  386. * 灭掉护理灯
  387. */
  388. public void dismissNurseBrightness() {
  389. //7寸分机无护理灯
  390. /*new Thread(new Runnable() {
  391. @Override
  392. public void run() {
  393. try {
  394. setNurseBrightness(serialPortUtil, 1, "000000", "000000", "000000", "000000", "000000");
  395. } catch (InterruptedException e) {
  396. e.printStackTrace();
  397. }
  398. }
  399. }).start();*/
  400. }
  401. private boolean isRecord = false;
  402. private boolean isPlay = false;
  403. public static final String TEST_AUDIO_FILE_PATH = Environment.getExternalStorageDirectory().getPath() + "/test";
  404. private String audiofilePath;
  405. private MediaRecorder mediaRecorder;
  406. private MediaPlayer mediaPlayer;
  407. /**
  408. * 监听呼叫按钮 buffer[5] 如果按下则 放松注册信息给后台
  409. */
  410. @Override
  411. public void serialPortBedOnclick(byte[] buffer) {
  412. // mScreenExtinguishUtil.touchScreen();//===============================息屏
  413. LogUtil.d("serialPortBedOnclick", "buffer[5]==" + buffer[5] + ", buffer[8]==" + buffer[8]);
  414. //呼叫护士键短按/长按松开
  415. if (buffer[5] == 1 | buffer[5] == 2) {
  416. if (null != Constants.MAC_ADDRESS) {
  417. //有线mac地址,设备出场信息,无线mac地址
  418. final String str = "MGR_REG_A" + Constants.DELIMITER + Constants.MAC_ADDRESS + Constants.DELIMITER + "4" + Constants.DELIMITER + android.os.Build.DISPLAY +"_"+Constants.MCU_VERSION_NUMBER +
  419. Constants.DELIMITER + Constants.MAC_ADDRESS + Constants.DELIMITER + "FF:FF:FF:FF:FF:FF" + Constants.DELIMITER + SerialPortUtil.KEY_ID;
  420. UdpSendUtil.sendManualReboot(str);
  421. runOnUiThread(new Runnable() {
  422. @Override
  423. public void run() {
  424. ToastUtil.showToast("收到按键消息,发送注册信息:" + str);
  425. }
  426. });
  427. }
  428. }
  429. if (buffer[8] == 2) {
  430. SoundPoolManager.getInstance().playSound(3);
  431. new Thread(new Runnable() {
  432. @Override
  433. public void run() {
  434. boolean pingStatus = NetUtil.ping("192.168.101.1", 2, null);
  435. if (pingStatus) {
  436. showTips("连接192.168.101.1成功!");
  437. } else {
  438. showTips("连接192.168.101.1失败, 请检查设备!");
  439. }
  440. }
  441. }).start();
  442. }
  443. //测试麦克风录音
  444. if (buffer[6] == 2) {
  445. if (!isRecord && !isPlay) {
  446. startRecording();
  447. updateHandler.sendEmptyMessageDelayed(111, 3000);
  448. }
  449. }
  450. }
  451. @Override
  452. public void serialPortBedOnclickString(String str) {
  453. try {
  454. String newStr = str.substring(str.indexOf("$") + 1, str.indexOf("#"));
  455. LogUtil.d("serialPortBedOnclickString", "newStr==" + newStr);
  456. if (newStr.startsWith("V")) {
  457. Constants.MCU_VERSION_NUMBER = newStr.substring(newStr.indexOf(",") + 1, 16);
  458. LogUtil.d("serialPortBedOnclick", "Constants.MCU_VERSION_NUMBER==" + Constants.MCU_VERSION_NUMBER);
  459. }
  460. } catch (Exception e) {
  461. e.printStackTrace();
  462. }
  463. }
  464. private void showTips(final String tips) {
  465. runOnUiThread(new Runnable() {
  466. @Override
  467. public void run() {
  468. ToastUtil.showToast(tips);
  469. }
  470. });
  471. }
  472. private void startRecording() {
  473. showTips("开始录音");
  474. new Thread(new Runnable() {
  475. @Override
  476. public void run() {
  477. mediaRecorder = new MediaRecorder();
  478. try {
  479. audiofilePath = TEST_AUDIO_FILE_PATH + "/" + System.currentTimeMillis() + "_test.mp3";
  480. mediaRecorder.setOutputFile(audiofilePath);
  481. mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);// 设置MediaRecorder的音频源为麦克风
  482. mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);// 设置MediaRecorder录制的音频格式
  483. mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);// 设置MediaRecorder录制音频的编码为amr
  484. mediaRecorder.prepare();
  485. mediaRecorder.start();
  486. } catch (IOException e) {
  487. Log.i("error", "call startAmr(File mRecAudioFile) failed!" + e.getMessage());
  488. } finally {
  489. isRecord = false;
  490. }
  491. }
  492. }).start();
  493. }
  494. private void stopRecord() {
  495. showTips("结束录音");
  496. new Thread(new Runnable() {
  497. @Override
  498. public void run() {
  499. try {
  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. }