學習Android之第八個小程式檔案儲存(Notification、AndroidTestCase)

來源:互聯網
上載者:User

標籤:xml   android   

     


.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;@Overridepublic 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() {@Overridepublic 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,則測試失敗。










聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.