FileObserver監聽檔案樣本

來源:互聯網
上載者:User

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;@Overrideprotected 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 {@Overridepublic 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);}@Overridepublic 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;}}}@Overrideprotected 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>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.