FileObserver监听文件示例

mainActivity如下:

package c.c;
import java.io.File;
import android.os.Bundle;
import android.os.Environment;
import android.os.FileObserver;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;
/**
 * 利用FileObserver监听SD卡的文件变化
 * 如文件的建立和删除
 * 注意FileObserver是另外的一个线程
 * 所以在其中改变View是不可行的
 *
 */
public class MainActivity extends Activity {
	private Button mStartButton;
	private Button mAddButton;
	private Button mDeleteButton;
	private TextView mTextView;
	private String mSDCardPath;
	private String mFilePath;
	private File mFile;
	private SDCardFileObserver mFileObserver;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		init();
	}
	private void init(){
		mStartButton=(Button) findViewById(R.id.start_Button);
		mStartButton.setOnClickListener(new OnClickListenerImpl());
		mAddButton=(Button) findViewById(R.id.add_Button);
		mAddButton.setOnClickListener(new OnClickListenerImpl());
		mDeleteButton=(Button) findViewById(R.id.delete_Button);
		mDeleteButton.setOnClickListener(new OnClickListenerImpl());
		mTextView=(TextView) findViewById(R.id.textView);
		mSDCardPath=Environment.getExternalStorageDirectory().toString();
	}

	private class OnClickListenerImpl implements OnClickListener {
		@Override
		public void onClick(View v) {
			if (v.getId()==R.id.start_Button) {
				mFileObserver=new SDCardFileObserver(mSDCardPath);
				mFileObserver.startWatching();
				mTextView.setText("开始监听");
			}
			if (v.getId() == R.id.add_Button) {
				try {
					mFilePath=mSDCardPath+File.separator+"test.txt";
					mFile=new File(mFilePath);
					if (!mFile.exists()) {
						mFile.createNewFile();
					}
					mTextView.setText("创建"+mFilePath);
				} catch (Exception e) {
				}
			}
			if (v.getId() == R.id.delete_Button) {
                if (mFile!=null) {
					mFile.delete();
					mTextView.setText("删除"+mFilePath);
				}
			}
		}
	}
	
	private class SDCardFileObserver extends FileObserver{
		public SDCardFileObserver(String path) {
			super(path);
		}

		@Override
		public void onEvent(int event, String path) {
			switch (event) {
			case FileObserver.CREATE:
				System.out.println("------->创建文件,"+path);
				//the next code is not right
				//mTextView.setText("创建,"+path);
				break;
			case FileObserver.DELETE:
				System.out.println("------->删除文件,"+path);
				//the next code is not right
				//mTextView.setText("删除,"+path);
				break;
			default:
				break;
			}
		}
		
	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
		mFileObserver.stopWatching();
	}
}


main.xml如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="match_parent" >

            <Button
                android:id="@+id/start_Button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:text="开始监听" />

            <Button
                android:id="@+id/add_Button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="增加文件" />

            <Button
                android:id="@+id/delete_Button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:text="删除文件" />
        </RelativeLayout>
    </LinearLayout>

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/linearLayout"
        android:layout_centerHorizontal="true"
        android:paddingTop="20dip"
        android:text="@string/hello_world"
        android:textSize="20sp" />

</RelativeLayout>


 

你可能感兴趣的:(FileObserver监听文件示例)