:
. java files have Mainactivity.java, Fileservice.java, Fileservicetest.java, and. xml files have activity_main.xml.
This time focus on the use of Androidtestcase class, in the development of very practical. Used to test a feature.
Use the Androidtestcase class, like the following requirements:
1. In the Androidmanifest.xml file, add the following,<manifest></manifest>:
<instrumentation android:name= "Android.test.InstrumentationTestRunner" android:targetpackage= " Com.example.l3_files "> </instrumentation>
2. In the Androidmanifest.xml file, add the following to <application> </application>:
<uses-library android:name= "Android.test.runner"/>
3. Build test class, Inherit androidtestcase, write test method.
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 = "File saved successfully"; 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;} /** * Save file * @param filename File name * @param filecontent file contents * @throws ioexception */public void Save (String Filename,strin G filecontent) throws ioexception{//the 1th parameter is the name of the file, and the 2nd parameter is the operating mode 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 to output data in memory//If the amount of data is large, The data read for the 2nd time is likely to overwrite the 1th reading of the data {Baos.write (Buffer,0,len);} Byte[] Data=baos.tobytearray ();//Get the in-memory data stored in binary baos.close (); Fis.close (); return new String (data);// Converted to the corresponding string according to the binary data}}
Fileservicetest.java (Identification Unit Class)
Package Com.example.l3_files.model;import Java.io.ioexception;import Android.test.androidtestcase;public class Fileservicetest extends Androidtestcase { //test Unit public void Testsave () throws IOException {Fileservice fileservice= New Fileservice (GetContext ()); Fileservice.save ("File1.txt", "Save Test"); 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= "fi Ll_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>
To test the use of a class:
Right-click the test method that you have written, Run as->android JUnit test.
The test succeeds as follows, and if errors is not 0, the test fails.