效果图:
.java文件有MainActivity.java、FileService.java、FileServiceTest.java, .xml文件有activity_main.xml。
本次注重AndroidTestCase类的使用,在开发中非常实用。用于测试某一功能。
使用AndroidTestCase类,有如下的要求:
1.在AndroidManifest.xml文件中,<manifest></manifest>中添加如下:
<instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.example.l3_files" > </instrumentation>
2.在AndroidManifest.xml文件中, <application> </application>中添加如下:
<uses-library android:name="android.test.runner" />
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
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/filename" /> <EditText android:id="@+id/filename" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/filecontent" /> <EditText android:id="@+id/filecontent" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="top" android:minLines="5" /> <Button android:id="@+id/save" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/save" /> </LinearLayout>
测试类的使用:
右键已编写的测试方法,Run as->Android JUnit Test.
如下测试成功,如果Errors不为0,则测试失败。