EduFoundUtil.java 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. package com.edufound.mobile.xxt.util;
  2. import android.app.Activity;
  3. import android.app.AlarmManager;
  4. import android.app.PendingIntent;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.content.SharedPreferences;
  8. import android.content.SharedPreferences.Editor;
  9. import android.content.pm.PackageInfo;
  10. import android.content.pm.PackageManager.NameNotFoundException;
  11. import android.graphics.Bitmap;
  12. import android.graphics.BitmapFactory;
  13. import android.net.Uri;
  14. import android.net.wifi.WifiInfo;
  15. import android.net.wifi.WifiManager;
  16. import android.os.Build;
  17. import android.support.v4.content.FileProvider;
  18. import android.text.Editable;
  19. import android.text.Selection;
  20. import android.text.TextWatcher;
  21. import android.util.DisplayMetrics;
  22. import android.util.Log;
  23. import android.view.View;
  24. import android.view.Window;
  25. import android.view.WindowManager;
  26. import android.widget.EditText;
  27. import android.widget.TextView;
  28. import java.io.BufferedReader;
  29. import java.io.File;
  30. import java.io.IOException;
  31. import java.io.InputStream;
  32. import java.io.InputStreamReader;
  33. import java.io.UnsupportedEncodingException;
  34. import java.net.HttpURLConnection;
  35. import java.net.InetAddress;
  36. import java.net.MalformedURLException;
  37. import java.net.NetworkInterface;
  38. import java.net.SocketException;
  39. import java.net.URL;
  40. import java.security.MessageDigest;
  41. import java.util.Enumeration;
  42. import java.util.regex.Matcher;
  43. import java.util.regex.Pattern;
  44. public class EduFoundUtil {
  45. /**
  46. * getVersionCode 得到名称
  47. */
  48. public static String getVersion(Context context)// 得到版本
  49. {
  50. try {
  51. PackageInfo pi = context.getPackageManager()
  52. .getPackageInfo(context.getPackageName(), 0);
  53. return pi.versionName;
  54. } catch (NameNotFoundException e) {
  55. e.printStackTrace();
  56. return null;
  57. }
  58. }
  59. /**
  60. * getVersionCode 得到版本号
  61. */
  62. public static int getVersionCode(Context context) {
  63. try {
  64. PackageInfo pi = context.getPackageManager()
  65. .getPackageInfo(context.getPackageName(), 0);
  66. return pi.versionCode;
  67. } catch (NameNotFoundException e) {
  68. e.printStackTrace();
  69. return 0;
  70. }
  71. }
  72. private static String parseByte(byte b) {
  73. String s = "00" + Integer.toHexString(b) + ":";
  74. return s.substring(s.length() - 3);
  75. }
  76. public static void openFile(Context context, File file) {
  77. // TODO Auto-generated method stub
  78. Intent intent = new Intent(Intent.ACTION_VIEW);
  79. intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  80. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
  81. Uri uri = FileProvider.getUriForFile(context, "com.edufound.mobile.fileprovider", file);
  82. intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
  83. intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
  84. intent.setDataAndType(uri, "application/vnd.android.package-archive");
  85. } else {
  86. String cmd = "chmod 777 " + file;
  87. try {
  88. Runtime.getRuntime().exec(cmd);
  89. Log.e("OpenFile", file.getName());
  90. intent.setAction(android.content.Intent.ACTION_VIEW);
  91. intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
  92. } catch (IOException e) {
  93. e.printStackTrace();
  94. }
  95. }
  96. context.startActivity(intent);
  97. }
  98. /**
  99. * long转换成String时间
  100. */
  101. public static String getTime(int mss) {
  102. int hours = (mss / (1000 * 60 * 60));
  103. int minutes = (mss - hours * (1000 * 60 * 60)) / (1000 * 60);
  104. int m = hours * 60 + minutes;
  105. int seconds = (mss - hours * (1000 * 60 * 60) - minutes * (1000 * 60)) / 1000;
  106. return String.format("%02d:%02d", m, seconds);
  107. }
  108. /**
  109. * 得到宽高缩放比
  110. */
  111. public static float initDesignSize(Context context, int screenWidth, int screenHeight) {
  112. int designWidth = 1280;
  113. int designHeight = 720;
  114. float ds = 1.0f * designWidth / designHeight;
  115. float ts = 1.0f * screenWidth / screenHeight;
  116. if (ds >= ts) {
  117. int targetWidth = screenWidth;
  118. float designScale = 1.0f * targetWidth / designWidth;
  119. return designScale;
  120. } else {
  121. int targetHeight = screenHeight;
  122. float designScale = 1.0f * targetHeight / designHeight;
  123. return designScale;
  124. }
  125. }
  126. /**
  127. * 设置dpi缩放比
  128. */
  129. public static void setDisplay(Context context, float designScale) {
  130. DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
  131. displayMetrics.density = designScale;
  132. displayMetrics.scaledDensity = displayMetrics.density;
  133. displayMetrics.xdpi = displayMetrics.density * 160;
  134. displayMetrics.ydpi = displayMetrics.xdpi;
  135. displayMetrics.densityDpi = Float.valueOf(displayMetrics.xdpi).intValue();
  136. }
  137. public static String getMacAddress() {
  138. String result = "";
  139. String Mac = "";
  140. result = callCmd("busybox ifconfig", "HWaddr");
  141. if (result == null) {
  142. return "网络出错,请检查网络";
  143. }
  144. if (result.length() > 0 && result.contains("HWaddr")) {
  145. Mac = result.substring(result.indexOf("HWaddr") + 6, result.length() - 1);
  146. if (Mac.length() > 1) {
  147. result = Mac.toLowerCase();
  148. }
  149. }
  150. return result.trim();
  151. }
  152. public static String getEthernetMac() {
  153. String mac = null;
  154. try {
  155. Process p = Runtime.getRuntime().exec("cat /sys/class/net/eth0/address");
  156. InputStream is = p.getInputStream();
  157. InputStreamReader isr = new InputStreamReader(is);
  158. BufferedReader bf = new BufferedReader(isr);
  159. String line = null;
  160. if ((line = bf.readLine()) != null) {
  161. mac = line;
  162. }
  163. bf.close();
  164. isr.close();
  165. is.close();
  166. } catch (IOException e) {
  167. e.printStackTrace();
  168. }
  169. return mac;
  170. }
  171. public static String callCmd(String cmd, String filter) {
  172. String result = "";
  173. String line = "";
  174. try {
  175. Process proc = Runtime.getRuntime().exec(cmd);
  176. InputStreamReader is = new InputStreamReader(proc.getInputStream());
  177. BufferedReader br = new BufferedReader(is);
  178. // 执行命令cmd,只取结果中含有filter的这一行
  179. while ((line = br.readLine()) != null && line.contains(filter) == false) {
  180. // result += line;
  181. Log.i("test", "line: " + line);
  182. }
  183. result = line;
  184. Log.i("test", "result: " + result);
  185. } catch (Exception e) {
  186. e.printStackTrace();
  187. }
  188. return result;
  189. }
  190. public static void runCmd(String cmd) {
  191. Runtime mRuntime = Runtime.getRuntime();
  192. try {
  193. //Process中封装了返回的结果和执行错误的结果
  194. Process mProcess = mRuntime.exec(cmd);
  195. BufferedReader mReader = new BufferedReader(new InputStreamReader(mProcess.getInputStream()));
  196. StringBuffer mRespBuff = new StringBuffer();
  197. char[] buff = new char[1024];
  198. int ch = 0;
  199. while ((ch = mReader.read(buff)) != -1) {
  200. mRespBuff.append(buff, 0, ch);
  201. }
  202. mReader.close();
  203. Logger.DebugE(mRespBuff.toString());
  204. } catch (IOException e) {
  205. // TODO Auto-generated catch block
  206. e.printStackTrace();
  207. }
  208. }
  209. /**
  210. * 根据wifi信息获取本地mac
  211. *
  212. * @param context
  213. * @return
  214. */
  215. public static String getLocalMacAddressFromWifiInfo(Context context) {
  216. WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
  217. WifiInfo winfo = wifi.getConnectionInfo();
  218. String mac = winfo.getMacAddress();
  219. return mac;
  220. }
  221. // 有线ip得mac
  222. public static String getLocalMacAddressFromIp(Context context) {
  223. String mac_s = "";
  224. String macAddress = "";
  225. try {
  226. byte[] mac;
  227. NetworkInterface ne = NetworkInterface.getByInetAddress(InetAddress
  228. .getByName(getLocalIpAddress()));
  229. mac = ne.getHardwareAddress();
  230. mac_s = bytes2hex02(mac);
  231. macAddress = mac_s.substring(0, 2) + ":" + mac_s.substring(2, 4) + ":"
  232. + mac_s.substring(4, 6) + ":" + mac_s.substring(6, 8) + ":"
  233. + mac_s.substring(8, 10) + ":" + mac_s.substring(10, 12);
  234. return macAddress;
  235. } catch (Exception e) {
  236. e.printStackTrace();
  237. }
  238. return macAddress;
  239. }
  240. /**
  241. * 二进制转十六进制
  242. *
  243. * @param
  244. * @return
  245. */
  246. public static String bytes2hex02(byte[] bytes) {
  247. StringBuilder sb = new StringBuilder();
  248. String tmp = null;
  249. for (byte b : bytes) {
  250. // 将每个字节与0xFF进行与运算,然后转化为10进制,然后借助于Integer再转化为16进制
  251. tmp = Integer.toHexString(0xFF & b);
  252. if (tmp.length() == 1)// 每个字节8为,转为16进制标志,2个16进制位
  253. {
  254. tmp = "0" + tmp;
  255. }
  256. sb.append(tmp);
  257. }
  258. return sb.toString();
  259. }
  260. // 获得ip地址
  261. public static String getLocalIpAddress() {
  262. try {
  263. for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
  264. NetworkInterface intf = (NetworkInterface) en.nextElement();
  265. for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
  266. InetAddress inetAddress = (InetAddress) enumIpAddr.nextElement();
  267. if (!inetAddress.isLoopbackAddress()) {
  268. return inetAddress.getHostAddress().toString();
  269. }
  270. }
  271. }
  272. } catch (SocketException ex) {
  273. ex.printStackTrace();
  274. }
  275. return null;
  276. }
  277. /**
  278. * 判断String是否为空
  279. *
  280. * @return boolean是否为空
  281. */
  282. public static Boolean isEmpty(String s) {
  283. if (s == null || s.length() == 0) {
  284. return true;
  285. } else {
  286. return false;
  287. }
  288. }
  289. public static String getIP(String ip) {
  290. StringBuffer s = new StringBuffer();
  291. String ss = null;
  292. Process p;
  293. try {
  294. p = Runtime.getRuntime().exec("/system/bin/ping " + ip);
  295. BufferedReader buf = new BufferedReader(new InputStreamReader(p.getInputStream()));
  296. String str = new String();
  297. // 读出所有信息并显示
  298. while ((str = buf.readLine()) != null && s.length() < 100) {
  299. str = str + "\r\n";
  300. s.append(str);
  301. }
  302. ss = EduFoundUtil.getTelnum(s.toString()).split(",")[0];
  303. } catch (Exception e) {
  304. // TODO Auto-generated catch block
  305. e.printStackTrace();
  306. }
  307. return ss;
  308. }
  309. public static void saveIsHavePhone(Context context, boolean isHavephone) {
  310. SharedPreferences preferences = context.getSharedPreferences("isHavePhone",
  311. Context.MODE_PRIVATE);
  312. Editor editor = preferences.edit();
  313. editor.putBoolean("isHadPhone", isHavephone);
  314. editor.commit();
  315. }
  316. public static boolean getIsHavePhone(Context context) {
  317. SharedPreferences preferences = context.getSharedPreferences("isHavePhone",
  318. Context.MODE_PRIVATE);
  319. boolean isHave = preferences.getBoolean("isHadPhone", false);
  320. return isHave;
  321. }
  322. public static boolean isMobile(String str) {
  323. Pattern p = null;
  324. Matcher m = null;
  325. boolean b = false;
  326. p = Pattern.compile("^[1][3,4,5,8][0-9]{9}$"); // 验证手机号
  327. m = p.matcher(str);
  328. b = m.matches();
  329. return b;
  330. }
  331. public static String getTelnum(String sParam) {
  332. if (sParam.length() <= 0)
  333. return "";
  334. // Pattern pattern = Pattern.compile("(1|[0-9])([0-9])\\d{9}$*");
  335. // Pattern pattern =
  336. // Pattern.compile("((13[0-9])|(14[7])|(15[0-9])|17[0-9]|(18[0-9]))\\d{8}$*");
  337. Pattern pattern = Pattern
  338. .compile("(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]).(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0).(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0).(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$*");
  339. Matcher matcher = pattern.matcher(sParam);
  340. StringBuffer bf = new StringBuffer();
  341. while (matcher.find()) {
  342. bf.append(matcher.group()).append(",");
  343. }
  344. int len = bf.length();
  345. if (len > 0) {
  346. bf.deleteCharAt(len - 1);
  347. }
  348. return bf.toString();
  349. }
  350. public static String getSerial() {
  351. String s;
  352. if (EduFoundUtil.isEmpty(EduFoundUtil.getMacAddress())) {
  353. s = "35" + Build.BOARD.length() + Build.BRAND.length() + Build.CPU_ABI.length()
  354. + Build.DEVICE.length() + Build.DISPLAY.length() + Build.HOST.length()
  355. + Build.ID.length() + Build.MANUFACTURER.length() + Build.MODEL.length()
  356. + Build.PRODUCT.length() + Build.TAGS.length() + Build.TYPE.length()
  357. + Build.USER.length() + EduFoundUtil.getEthernetMac();
  358. } else {
  359. s = "35" + Build.BOARD.length() + Build.BRAND.length() + Build.CPU_ABI.length()
  360. + Build.DEVICE.length() + Build.DISPLAY.length() + Build.HOST.length()
  361. + Build.ID.length() + Build.MANUFACTURER.length() + Build.MODEL.length()
  362. + Build.PRODUCT.length() + Build.TAGS.length() + Build.TYPE.length()
  363. + Build.USER.length() + EduFoundUtil.getMacAddress();
  364. }
  365. Logger.e("serial=======" + s);
  366. return md5Encode(s);
  367. }
  368. // md5加密
  369. public static String md5Encode(String inStr) {
  370. MessageDigest md5 = null;
  371. try {
  372. md5 = MessageDigest.getInstance("MD5");
  373. } catch (Exception e) {
  374. System.out.println(e.toString());
  375. e.printStackTrace();
  376. return "";
  377. }
  378. byte[] byteArray = null;
  379. try {
  380. byteArray = inStr.getBytes("UTF-8");
  381. } catch (UnsupportedEncodingException e) {
  382. // TODO Auto-generated catch block
  383. e.printStackTrace();
  384. }
  385. byte[] md5Bytes = md5.digest(byteArray);
  386. StringBuffer hexValue = new StringBuffer();
  387. for (int i = 0; i < md5Bytes.length; i++) {
  388. int val = ((int) md5Bytes[i]) & 0xff;
  389. if (val < 16) {
  390. hexValue.append("0");
  391. }
  392. hexValue.append(Integer.toHexString(val));
  393. }
  394. return hexValue.toString();
  395. }
  396. public static void savePlayMode(Context context, String value) {
  397. SharedPreferences preferences = context.getSharedPreferences("PlayMode",
  398. Context.MODE_PRIVATE);
  399. Editor editor = preferences.edit();
  400. editor.putString("playmode", value);
  401. editor.commit();
  402. }
  403. public static String getPlayMode(Context context) {
  404. SharedPreferences preferences = context.getSharedPreferences("PlayMode",
  405. Context.MODE_PRIVATE);
  406. String playMode = preferences.getString("playmode", "default");
  407. return playMode;
  408. }
  409. public static void saveStuNo(Context context, String value) {
  410. SharedPreferences preferences = context.getSharedPreferences("STUNO", Context.MODE_PRIVATE);
  411. Editor editor = preferences.edit();
  412. editor.putString("stu_no", value);
  413. editor.commit();
  414. }
  415. public static String getStuNo(Context context) {
  416. SharedPreferences preferences = context.getSharedPreferences("STUNO", Context.MODE_PRIVATE);
  417. String stu_no = preferences.getString("stu_no", "");
  418. return stu_no;
  419. }
  420. /**
  421. * 银行卡四位加空格
  422. *
  423. * @param mEditText
  424. */
  425. public static void bankCardNumAddSpace(final EditText mEditText) {
  426. mEditText.addTextChangedListener(new TextWatcher() {
  427. int beforeTextLength = 0;
  428. int onTextLength = 0;
  429. boolean isChanged = false;
  430. int location = 0;// 记录光标的位置
  431. private char[] tempChar;
  432. private StringBuffer buffer = new StringBuffer();
  433. int konggeNumberB = 0;
  434. @Override
  435. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  436. beforeTextLength = s.length();
  437. if (buffer.length() > 0) {
  438. buffer.delete(0, buffer.length());
  439. }
  440. konggeNumberB = 0;
  441. for (int i = 0; i < s.length(); i++) {
  442. if (s.charAt(i) == ' ') {
  443. konggeNumberB++;
  444. }
  445. }
  446. }
  447. @Override
  448. public void onTextChanged(CharSequence s, int start, int before, int count) {
  449. onTextLength = s.length();
  450. buffer.append(s.toString());
  451. if (onTextLength == beforeTextLength || onTextLength <= 3 || isChanged) {
  452. isChanged = false;
  453. return;
  454. }
  455. isChanged = true;
  456. }
  457. @Override
  458. public void afterTextChanged(Editable s) {
  459. if (isChanged) {
  460. location = mEditText.getSelectionEnd();
  461. int index = 0;
  462. while (index < buffer.length()) {
  463. if (buffer.charAt(index) == ' ') {
  464. buffer.deleteCharAt(index);
  465. } else {
  466. index++;
  467. }
  468. }
  469. index = 0;
  470. int konggeNumberC = 0;
  471. while (index < buffer.length()) {
  472. if ((index == 4 || index == 9 || index == 14 || index == 19)) {
  473. buffer.insert(index, ' ');
  474. konggeNumberC++;
  475. }
  476. index++;
  477. }
  478. if (konggeNumberC > konggeNumberB) {
  479. location += (konggeNumberC - konggeNumberB);
  480. }
  481. tempChar = new char[buffer.length()];
  482. buffer.getChars(0, buffer.length(), tempChar, 0);
  483. String str = buffer.toString();
  484. if (location > str.length()) {
  485. location = str.length();
  486. } else if (location < 0) {
  487. location = 0;
  488. }
  489. mEditText.setText(str);
  490. Editable etable = mEditText.getText();
  491. Selection.setSelection(etable, location);
  492. isChanged = false;
  493. }
  494. }
  495. });
  496. }
  497. public static Bitmap returnBitMap(String url) {
  498. URL myFileUrl = null;
  499. Bitmap bitmap = null;
  500. try {
  501. myFileUrl = new URL(url);
  502. } catch (MalformedURLException e) {
  503. e.printStackTrace();
  504. }
  505. try {
  506. HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
  507. conn.setDoInput(true);
  508. conn.connect();
  509. InputStream is = conn.getInputStream();
  510. bitmap = BitmapFactory.decodeStream(is);
  511. is.close();
  512. } catch (IOException e) {
  513. e.printStackTrace();
  514. }
  515. return bitmap;
  516. }
  517. public static void bankCardNumAddSpace1(final TextView mEditText) {
  518. mEditText.addTextChangedListener(new TextWatcher() {
  519. int beforeTextLength = 0;
  520. int onTextLength = 0;
  521. boolean isChanged = false;
  522. int location = 0;// 记录光标的位置
  523. private char[] tempChar;
  524. private StringBuffer buffer = new StringBuffer();
  525. int konggeNumberB = 0;
  526. @Override
  527. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  528. beforeTextLength = s.length();
  529. if (buffer.length() > 0) {
  530. buffer.delete(0, buffer.length());
  531. }
  532. konggeNumberB = 0;
  533. for (int i = 0; i < s.length(); i++) {
  534. if (s.charAt(i) == ' ') {
  535. konggeNumberB++;
  536. }
  537. }
  538. }
  539. @Override
  540. public void onTextChanged(CharSequence s, int start, int before, int count) {
  541. onTextLength = s.length();
  542. buffer.append(s.toString());
  543. if (onTextLength == beforeTextLength || onTextLength <= 3 || isChanged) {
  544. isChanged = false;
  545. return;
  546. }
  547. isChanged = true;
  548. }
  549. @Override
  550. public void afterTextChanged(Editable s) {
  551. if (isChanged) {
  552. location = mEditText.getSelectionEnd();
  553. int index = 0;
  554. while (index < buffer.length()) {
  555. if (buffer.charAt(index) == ' ') {
  556. buffer.deleteCharAt(index);
  557. } else {
  558. index++;
  559. }
  560. }
  561. index = 0;
  562. int konggeNumberC = 0;
  563. while (index < buffer.length()) {
  564. if ((index == 4 || index == 9 || index == 14 || index == 19)) {
  565. buffer.insert(index, ' ');
  566. konggeNumberC++;
  567. }
  568. index++;
  569. }
  570. if (konggeNumberC > konggeNumberB) {
  571. location += (konggeNumberC - konggeNumberB);
  572. }
  573. tempChar = new char[buffer.length()];
  574. buffer.getChars(0, buffer.length(), tempChar, 0);
  575. String str = buffer.toString();
  576. if (location > str.length()) {
  577. location = str.length();
  578. } else if (location < 0) {
  579. location = 0;
  580. }
  581. mEditText.setText(str);
  582. // Editable etable = mEditText.getText();
  583. // Selection.setSelection(etable, location);
  584. isChanged = false;
  585. }
  586. }
  587. });
  588. }
  589. public static void restartApplication(Context context) {
  590. Intent intent = context.getPackageManager()
  591. .getLaunchIntentForPackage(context.getPackageName());
  592. PendingIntent restartIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
  593. AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
  594. mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, restartIntent); // 1秒钟后重启应用
  595. System.exit(0);
  596. }
  597. public static void setHideVirtualKey(Window window) {
  598. //保持布局状态
  599. int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
  600. //布局位于状态栏下方
  601. View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
  602. //全屏
  603. View.SYSTEM_UI_FLAG_FULLSCREEN |
  604. //隐藏导航栏
  605. View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
  606. View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
  607. if (Build.VERSION.SDK_INT >= 19) {
  608. uiOptions |= 0x00001000;
  609. } else {
  610. uiOptions |= View.SYSTEM_UI_FLAG_LOW_PROFILE;
  611. }
  612. window.getDecorView().setSystemUiVisibility(uiOptions);
  613. }
  614. public static void openFullScreenModel(Activity activity) {
  615. activity.requestWindowFeature(Window.FEATURE_NO_TITLE);
  616. WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
  617. lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
  618. activity.getWindow().setAttributes(lp);
  619. View decorView = activity.getWindow().getDecorView();
  620. int systemUiVisibility = decorView.getSystemUiVisibility();
  621. int flags = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
  622. | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
  623. | View.SYSTEM_UI_FLAG_FULLSCREEN;
  624. systemUiVisibility |= flags;
  625. activity.getWindow().getDecorView().setSystemUiVisibility(systemUiVisibility);
  626. }
  627. }