Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 學習Android之第八個小程序文件保存(Notification、AndroidTestCase)

學習Android之第八個小程序文件保存(Notification、AndroidTestCase)

編輯:關於Android編程

效果圖:

\ \


.java文件有MainActivity.java、FileService.java、FileServiceTest.java, .xml文件有activity_main.xml。


<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+sb60ztei1thBbmRyb2lkVGVzdENhc2XA4LXEyrnTw6Os1Nq/qrei1tC3x7OjyrXTw6Gj08PT2rLiytTEs9K7uabE3KGjPC9wPgo8cD7KudPDQW5kcm9pZFRlc3RDYXNlwOCjrNPQyOfPwrXE0qrH86O6PC9wPgo8cD4xLtTaQW5kcm9pZE1hbmlmZXN0LnhtbM7EvP7W0KOsPG1hbmlmZXN0PjwvbWFuaWZlc3Q+1tDM7bzTyOfPwqO6PC9wPgo8cD48L3A+CjxwcmUgY2xhc3M9"brush:java;">
2.在AndroidManifest.xml文件中, 中添加如下:

 

3.建立測試類,繼承AndroidTestCase,編寫測試方法。


MainAcitity.java

package com.example.l3_files;

import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import java.io.IOException;
import com.example.l3_files.model.FileService;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

	private FileService fileService;
	private Button saveButton;
	NotificationManager notificationManager;
	Notification notification;
	PendingIntent pendingIntent;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		fileService = new FileService(this);

		saveButton = (Button) this.findViewById(R.id.save);

		notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

		pendingIntent = PendingIntent
				.getActivity(MainActivity.this, 0, null, 0);

		notification = new Notification();

		saveButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				EditText fileNameText = (EditText) findViewById(R.id.filename);
				EditText fileContentText = (EditText) findViewById(R.id.filecontent);

				String fileName = fileNameText.getText().toString();
				String fileContent = fileContentText.getText().toString();

				try {
					fileService.save(fileName, fileContent);
					Toast.makeText(MainActivity.this, R.string.success,
							Toast.LENGTH_LONG).show();
				} catch (IOException e) {
					e.printStackTrace();
					Toast.makeText(MainActivity.this, R.string.failure,
							Toast.LENGTH_LONG).show();
				}

				notification.icon = R.drawable.ic_launcher;
				notification.tickerText = "文件保存成功";
				notification.setLatestEventInfo(MainActivity.this, fileName,
						fileContent, pendingIntent);
				notificationManager.notify(0, notification);
			}

		});
	}

}

FileService.java

package com.example.l3_files.model;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.Context;

public class FileService {

	private Context context;

	public FileService(Context context) {
		super();
		this.context = context;
	}
	/**
	 * 保存文件
	 * @param filename 文件名稱
	 * @param filecontent 文件內容
	 * @throws IOException
	 */
	public void save(String filename,String filecontent) throws IOException{
		//第1個參數是文件的名稱,第2個參數是操作模式
		FileOutputStream fos=context.openFileOutput(filename, Context.MODE_WORLD_WRITEABLE+Context.MODE_WORLD_READABLE);
		fos.write(filecontent.getBytes());
		fos.close();
	}
	
	public String readFile(String filename) throws IOException{
		FileInputStream fis=context.openFileInput(filename);
		int len=0;
		byte[] buffer=new byte[1024];
		ByteArrayOutputStream baos=new ByteArrayOutputStream();//往內存中輸出數據的
		while((len=fis.read(buffer))!=-1)//如果數據量很大,第2次讀取的數據有可能會把第1次讀取的數據給覆蓋掉
		{
		   baos.write(buffer,0,len);
		}
		
		byte[] data=baos.toByteArray();//得到內存中的數據 以二進制存放的
		baos.close();
		fis.close();
		return new String(data);//根據二進制數據轉換成所對應的字符串
	}
}

FileServiceTest.java(測認單元類)

package com.example.l3_files.model;

import java.io.IOException;


import android.test.AndroidTestCase;

public class FileServiceTest extends AndroidTestCase {  //測試單元
	public void testSave() throws IOException {
		FileService fileService=new FileService(getContext());
		fileService.save("file1.txt", "保存測試");
	}
	public void testReadFile() throws IOException {
		FileService fileService=new FileService(getContext());
    	String content=fileService.readFile("file1.txt");
        System.out.println(content);
	}

}


activity_main.xml



    

    

    

    

    



測試類的使用:

右鍵已編寫的測試方法,Run as->Android JUnit Test.

\
如下測試成功,如果Errors不為0,則測試失敗。










  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved