Android 单字阅读

 package com.desmand.screencapture;

 import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast; import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern; public class MainActivity extends Activity implements View.OnClickListener, Runnable { private static String TAG = "ScreenCapture";
Thread showTextThread;
Button start_service_btn;
private TextView show_text_tv;
private String readerString;
private String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Panasonic/";
private String fileName = file_path + "test.txt";
private char testChar; private boolean isRun; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(null); startService(new Intent(this, ScreenCaptureService.class));
this.finish(); // super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); start_service_btn = (Button) findViewById(R.id.start_service_btn);
start_service_btn.setOnClickListener(this);
show_text_tv = (TextView) findViewById(R.id.show_text_tv); showTextThread = new Thread(this); File filePath = new File(file_path);
if (!filePath.exists()) {
Log.i(TAG, "----------ERROR----------");
}
} Handler myHandler = new Handler() {
public void handleMessage(Message msg) {
show_text_tv.setText(String.valueOf(testChar));
super.handleMessage(msg);
}
}; @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.start_service_btn:
if (showTextThread.isAlive()) {
isRun = false;
showTextThread.interrupt();
}
showTextThread = new Thread(this);
showTextThread.start();
isRun = true; // startService(new Intent(this, ScreenCaptureService.class));
// this.finish();
break;
default:
break;
}
} @Override
public void run() {
// List<String> lines = new ArrayList<>();
// BufferedReader br = null;
// try {
// File file = new File(fileName);
// InputStream inputStream = new FileInputStream(file);
// Reader reader = new InputStreamReader(inputStream, "GBK");
// br = new BufferedReader(reader);
// } catch (UnsupportedEncodingException | FileNotFoundException e) {
// e.printStackTrace();
// }
//
// try {
// while ((readerString = br.readLine()) != null) {
// lines.add(readerString);
// myHandler.sendEmptyMessage(0);
// Log.i(TAG, readerString);
// Thread.sleep(200);
// }
// br.close();
// } catch (IOException | InterruptedException e) {
// e.printStackTrace();
// } File file = new File(fileName);
BufferedReader reader;
String text = "";
try {
FileInputStream fis = new FileInputStream(file);
BufferedInputStream in = new BufferedInputStream(fis);
in.mark(4);
byte[] first3bytes = new byte[3];
in.read(first3bytes);//找到文档的前三个字节并自动判断文档类型。
in.reset();
if (first3bytes[0] == (byte) 0xEF && first3bytes[1] == (byte) 0xBB && first3bytes[2] == (byte) 0xBF) {// utf-8
reader = new BufferedReader(new InputStreamReader(in, "utf-8"));
} else if (first3bytes[0] == (byte) 0xFF && first3bytes[1] == (byte) 0xFE) {
reader = new BufferedReader(new InputStreamReader(in, "unicode"));
} else if (first3bytes[0] == (byte) 0xFE && first3bytes[1] == (byte) 0xFF) {
reader = new BufferedReader(new InputStreamReader(in, "utf-16be"));
} else if (first3bytes[0] == (byte) 0xFF && first3bytes[1] == (byte) 0xFF) {
reader = new BufferedReader(new InputStreamReader(in, "utf-16le"));
} else {
reader = new BufferedReader(new InputStreamReader(in, "GBK"));
} readerString = reader.readLine();
while (readerString != null) {
if (isRun) {
for (int i = 0; i < readerString.length(); i++) {// 遍历字符串每一个字符 // 使用正则表达式判断字符是否属于汉字编码
Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
Matcher m = p.matcher(readerString);
if (m.find()) {// 如果是汉字
testChar = readerString.charAt(i);
Log.i(TAG, "testChar: " + testChar);
myHandler.sendEmptyMessage(0);
} Thread.sleep(300); // String[] splitStr = readerString.split("[\\s,.;。,;]");
// for (String str : splitStr) {
// if (!str.trim().equals("")) {
// testChar = str.trim();
// Log.i(TAG, "testChar: " + testChar);
// myHandler.sendEmptyMessage(0);
// Thread.sleep(400);
// }
// } // StringBuffer english = new StringBuffer();
// String regex = "[^\u4e00-\u9fa5。,?”“《》:!——-、]";
// Pattern pattern = Pattern.compile(regex);
// Matcher matcher = pattern.matcher(readerString);
// while (matcher.find()) {
// testChar = null;
// english.append(matcher.group(1));
// testChar = english.toString();
// Log.i(TAG, "testChar: " + testChar);
// myHandler.sendEmptyMessage(0);
// } }
}
readerString = reader.readLine();
}
reader.close();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
} @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
isRun = false;
this.finish();
}
return super.onKeyDown(keyCode, event);
} @Override
protected void onDestroy() {
super.onDestroy();
isRun = false;
Toast.makeText(getApplicationContext(), "MainActivity onDestroy", Toast.LENGTH_SHORT).show();
} }
上一篇:洛谷P1120小木棍[DFS]


下一篇:LeetCode Fraction to Recurring Decimal