InitActivity.java 27 KB

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