PipeExampleActivity.java
package com.jing.example.nknu_.p42;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;
import java.io.IOException;
import java.io.PipedReader;
import java.io.PipedWriter;
public class PipeExampleActivity extends AppCompatActivity {
private static final String TAG = "PipeExampleActivity";
private EditText editText;
PipedReader r;
PipedWriter w;
private Thread workerThread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pipe_example);
r = new PipedReader();//消費者
w = new PipedWriter();//生產者
try {
w.connect(r);
} catch (IOException e) {
e.printStackTrace();
}
setContentView(R.layout.activity_pipe_example);
editText = (EditText)findViewById(R.id.editText);
editText.addTextChangedListener(new TextWatcher(){
@Override
public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
try{
Log.d(TAG, "before=" + String.valueOf(before) + " count = " +String.valueOf(count));
//只處理字元增加
if(count>before){
//最後輸出的字元寫到管道中
w.write(charSequence.subSequence(charSequence.length() - 1, charSequence.length()).toString());//產出單一字元
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
//進行無限迴圈讀取
workerThread = new Thread(new TextHandlerTask(r)); // r = 消費者
workerThread.start();
}
@Override
protected void onDestroy(){
super.onDestroy();
workerThread.interrupt();
try{
r.close();
w.close();
}catch(IOException e){
}
}
private static class TextHandlerTask implements Runnable{
private final PipedReader reader;
public TextHandlerTask(PipedReader reader){
this.reader = reader;//載入消費者
}
//消費者進行讀取
@Override
public void run() {
while(!Thread.currentThread().isInterrupted()){
try{
int i;
while((i = reader.read())!=-1){
char c = (char) i;
//在此增加文字處理邏輯
Log.d(TAG, "char = " + c);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
content_pipe_example
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.jing.example.nknu_.p42.PipeExampleActivity"
tools:showIn="@layout/activity_pipe_example">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
</RelativeLayout>