1 package com.desmand.screencapture; 2 3 import android.app.Activity; 4 import android.content.Intent; 5 import android.os.Bundle; 6 import android.os.Environment; 7 import android.os.Handler; 8 import android.os.Message; 9 import android.util.Log; 10 import android.view.KeyEvent; 11 import android.view.View; 12 import android.widget.Button; 13 import android.widget.TextView; 14 import android.widget.Toast; 15 16 import java.io.BufferedInputStream; 17 import java.io.BufferedReader; 18 import java.io.File; 19 import java.io.FileInputStream; 20 import java.io.IOException; 21 import java.io.InputStreamReader; 22 import java.util.regex.Matcher; 23 import java.util.regex.Pattern; 24 25 public class MainActivity extends Activity implements View.OnClickListener, Runnable { 26 27 private static String TAG = "ScreenCapture"; 28 Thread showTextThread; 29 Button start_service_btn; 30 private TextView show_text_tv; 31 private String readerString; 32 private String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Panasonic/"; 33 private String fileName = file_path + "test.txt"; 34 private char testChar; 35 36 private boolean isRun; 37 38 39 @Override 40 protected void onCreate(Bundle savedInstanceState) { 41 super.onCreate(null); 42 43 startService(new Intent(this, ScreenCaptureService.class)); 44 this.finish(); 45 46 // super.onCreate(savedInstanceState); 47 setContentView(R.layout.activity_main); 48 49 start_service_btn = (Button) findViewById(R.id.start_service_btn); 50 start_service_btn.setOnClickListener(this); 51 show_text_tv = (TextView) findViewById(R.id.show_text_tv); 52 53 showTextThread = new Thread(this); 54 55 File filePath = new File(file_path); 56 if (!filePath.exists()) { 57 Log.i(TAG, "----------ERROR----------"); 58 } 59 } 60 61 Handler myHandler = new Handler() { 62 public void handleMessage(Message msg) { 63 show_text_tv.setText(String.valueOf(testChar)); 64 super.handleMessage(msg); 65 } 66 }; 67 68 @Override 69 public void onClick(View v) { 70 switch (v.getId()) { 71 case R.id.start_service_btn: 72 if (showTextThread.isAlive()) { 73 isRun = false; 74 showTextThread.interrupt(); 75 } 76 showTextThread = new Thread(this); 77 showTextThread.start(); 78 isRun = true; 79 80 // startService(new Intent(this, ScreenCaptureService.class)); 81 // this.finish(); 82 break; 83 default: 84 break; 85 } 86 } 87 88 @Override 89 public void run() { 90 // List<String> lines = new ArrayList<>(); 91 // BufferedReader br = null; 92 // try { 93 // File file = new File(fileName); 94 // InputStream inputStream = new FileInputStream(file); 95 // Reader reader = new InputStreamReader(inputStream, "GBK"); 96 // br = new BufferedReader(reader); 97 // } catch (UnsupportedEncodingException | FileNotFoundException e) { 98 // e.printStackTrace(); 99 // } 100 // 101 // try { 102 // while ((readerString = br.readLine()) != null) { 103 // lines.add(readerString); 104 // myHandler.sendEmptyMessage(0); 105 // Log.i(TAG, readerString); 106 // Thread.sleep(200); 107 // } 108 // br.close(); 109 // } catch (IOException | InterruptedException e) { 110 // e.printStackTrace(); 111 // } 112 113 114 File file = new File(fileName); 115 BufferedReader reader; 116 String text = ""; 117 try { 118 FileInputStream fis = new FileInputStream(file); 119 BufferedInputStream in = new BufferedInputStream(fis); 120 in.mark(4); 121 byte[] first3bytes = new byte[3]; 122 in.read(first3bytes);//找到文档的前三个字节并自动判断文档类型。 123 in.reset(); 124 if (first3bytes[0] == (byte) 0xEF && first3bytes[1] == (byte) 0xBB && first3bytes[2] == (byte) 0xBF) {// utf-8 125 reader = new BufferedReader(new InputStreamReader(in, "utf-8")); 126 } else if (first3bytes[0] == (byte) 0xFF && first3bytes[1] == (byte) 0xFE) { 127 reader = new BufferedReader(new InputStreamReader(in, "unicode")); 128 } else if (first3bytes[0] == (byte) 0xFE && first3bytes[1] == (byte) 0xFF) { 129 reader = new BufferedReader(new InputStreamReader(in, "utf-16be")); 130 } else if (first3bytes[0] == (byte) 0xFF && first3bytes[1] == (byte) 0xFF) { 131 reader = new BufferedReader(new InputStreamReader(in, "utf-16le")); 132 } else { 133 reader = new BufferedReader(new InputStreamReader(in, "GBK")); 134 } 135 136 readerString = reader.readLine(); 137 while (readerString != null) { 138 if (isRun) { 139 for (int i = 0; i < readerString.length(); i++) {// 遍历字符串每一个字符 140 141 // 使用正则表达式判断字符是否属于汉字编码 142 Pattern p = Pattern.compile("[u4e00-u9fa5]"); 143 Matcher m = p.matcher(readerString); 144 if (m.find()) {// 如果是汉字 145 testChar = readerString.charAt(i); 146 Log.i(TAG, "testChar: " + testChar); 147 myHandler.sendEmptyMessage(0); 148 } 149 150 Thread.sleep(300); 151 152 // String[] splitStr = readerString.split("[\s,.;。,;]"); 153 // for (String str : splitStr) { 154 // if (!str.trim().equals("")) { 155 // testChar = str.trim(); 156 // Log.i(TAG, "testChar: " + testChar); 157 // myHandler.sendEmptyMessage(0); 158 // Thread.sleep(400); 159 // } 160 // } 161 162 // StringBuffer english = new StringBuffer(); 163 // String regex = "[^u4e00-u9fa5。,?”“《》:!——-、]"; 164 // Pattern pattern = Pattern.compile(regex); 165 // Matcher matcher = pattern.matcher(readerString); 166 // while (matcher.find()) { 167 // testChar = null; 168 // english.append(matcher.group(1)); 169 // testChar = english.toString(); 170 // Log.i(TAG, "testChar: " + testChar); 171 // myHandler.sendEmptyMessage(0); 172 // } 173 174 } 175 } 176 readerString = reader.readLine(); 177 } 178 reader.close(); 179 } catch (IOException | InterruptedException e) { 180 e.printStackTrace(); 181 } 182 } 183 184 185 @Override 186 public boolean onKeyDown(int keyCode, KeyEvent event) { 187 if (keyCode == KeyEvent.KEYCODE_BACK) { 188 isRun = false; 189 this.finish(); 190 } 191 return super.onKeyDown(keyCode, event); 192 } 193 194 195 @Override 196 protected void onDestroy() { 197 super.onDestroy(); 198 isRun = false; 199 Toast.makeText(getApplicationContext(), "MainActivity onDestroy", Toast.LENGTH_SHORT).show(); 200 } 201 202 203 }