Java Siege Lion Road (Android)--sqlite

Source: Internet
Author: User
Tags sqlite database tag name

One. Junit
1. How to use
Configure in the Androidmanifest.xml file, configure the instrumentation under the manifest lending point, configure uses-library under the application lending point
Defining class Inheritance Androidtestcast
Define test methods, Run as Junittest
If you need to determine the test results, you can use the Assert.assertequals () method.

The following is a test engineering JUnit using an independent test engineering Junittest:

 Package Com.shellway.junit;  Public class Service {      publicint divide (int A,int  b) {            Return A/b;      }}
Service.java
 PackageCom.shellway.junit;ImportJunit.framework.Assert;Importandroid.test.AndroidTestCase; Public classTesttextendsAndroidtestcase { Public voidtest1 () {Service service=NewService (); System.out.println (Service.divide (10, 2)); }     Public voidtest2 () {Service service=NewService (); System.out.println (Service.divide (10, 0)); }     Public voidtest3 () {Service service=NewService (); Assert.assertequals (2.5, Service.divide (10, 4)); }}
Testt.java
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.shellway.junit"Android:versioncode= "1"Android:versionname= "1.0" > <uses-SDK Android:minsdkversion= "16"android:targetsdkversion= "/>" <Instrumentation Android:targetpackage= "Com.shellway.junit"Android:name= "Android.test.InstrumentationTestRunner"/> <Application Android:allowbackup= "true"Android:icon= "@drawable/ic_launcher"Android:label= "@string/app_name"Android:theme= "@style/apptheme" > <uses-library android:name= "Android.test.runner"/> <Activity Android:name=". Mainactivity "Android:label= "@string/app_name" > <intent-filter> <action android:name= "Android.intent.action.MA In "/> <category android:name=" Android.intent.category.LAUNCHER "/> </intent-filter&        Gt </activity> </application></manifest>
Androidmanifest.xml

Junittest in the project:

 Packagecom.shellway.junit.test;ImportJunit.framework.Assert;ImportCom.shellway.junit.Service;Importandroid.test.AndroidTestCase; Public classMyTestextendsAndroidtestcase { Public voidtest1 () {Service service=NewService (); System.out.println (Service.divide (10,2)); }     Public voidtest2 () {Service service=NewService (); System.out.println (Service.divide (10, 0)); }     Public voidtest3 () {Service service=NewService (); Assert.assertequals (2.5, Service.divide (10, 4)); }}
Mytest.java
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "Package    =" Com.shellway.junit.test "    android:versioncode=" 1 "    android: Versionname= "1.0" >    <uses-sdk android:minsdkversion= "+"/>    <instrumentation        Android:name= "Android.test.InstrumentationTestRunner"        android:targetpackage= " Com.shellway.junit "/>    <application        android:icon=" @drawable/ic_launcher "         Android:label= "@string/app_name" >        <uses-library android:name= "Android.test.runner"/>    </application></manifest>
Androidmanifest.xml


Two. Log
1. How to use
LOG.V (), D (), I (), W (), E ()
You can output day information by level, you can specify tag

 PackageCom.example.logcat;Importandroid.test.AndroidTestCase;ImportAndroid.util.Log; Public classLogtestextendsAndroidtestcase { Public voidtest1 () {System.out.println ("System.out"); System.err.println ("System.out"); }     Public voidtest2 () {LOG.V ("Logtest", "verbose"); LOG.D ("Logtest", "Debug"); LOG.I ("Logtest", "info"); LOG.W ("Logtest", "warning"); LOG.E ("Logtest", "Error"); }}
Logtest.java
<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.example.logcat"Android:versioncode= "1"Android:versionname= "1.0" >    <USES-SDKandroid:minsdkversion= "8"android:targetsdkversion= "+" />        <InstrumentationAndroid:targetpackage= "Com.example.logcat"Android:name= "Android.test.InstrumentationTestRunner" />        <ApplicationAndroid:allowbackup= "true"Android:icon= "@drawable/ic_launcher"Android:label= "@string/app_name"Android:theme= "@style/apptheme" >     <uses-libraryAndroid:name= "Android.test.runner" />        <ActivityAndroid:name=". Mainactivity "Android:label= "@string/app_name" >            <Intent-filter>                <ActionAndroid:name= "Android.intent.action.MAIN" />                <categoryAndroid:name= "Android.intent.category.LAUNCHER" />            </Intent-filter>        </Activity>    </Application></Manifest>
Androidmanifest.xml


Three. Read and write files
1. Read and write SD card
Get SD card path to use Environment.getexternalstoragedirectory ()
It is best to determine the status before using the SD card, Environment.getexternalstoragestate (). Equals (environment.media_mounted)
Write to SD card requires permission, Android.permission.WRITE_EXTERNAL_STORAGE
2. Read and write mobile phone
You can use the Context.openfileoutput (String, int) method to open the output stream
When you specify a file name, the files folder is automatically generated under the folder where you are currently applying, creating
The int parameter is a file permission that can be set using the constants under context

Four. sharedpreferences
1. What is Sharedpreferences
is a container for key-value pairs, similar to map (Properties), where you can store key-value pairs, look up values by key, and store the data stored in the container as an XML file.
2. How to use
Use Context.getsharedpreferences (String, int) to get the object, specifying the file name and the document mode
Use the Sharedpreferences.edit () method to get editor editors
Use Editor.putstring (), Putint () and other methods to store data
Commit changes using the Editor.commit () method (similar to a transaction)
Get the data directly using Sharedpreferences.getstring (), GetInt () and other methods

Five. XML
1. Parsing
Get parser: Xml.newpullparser ()
Set input stream: Parser.setinput (InputStream, String)
Gets the current event type: Parser.geteventtype (), total 5
Get the next event Type: Parser.next ()
Get tag name: Parser.getname ()
Get property value: Parser.getattributevalue (int)
Get Next Text: Parser.nexttext ()
2. Build
Get parser:
To set the output stream:
To start a document:
To end a document:
To open a tag:
End tag:
To set properties:
Set Text:

Six. SQLite database
Features of the 1.SQLite database
The phone comes with a database that does not differentiate between data types (except the primary key), syntax and MySQL are the same, each library is a file
2. Create a library
Define class inheritance Sqliteopenhelper, define constructors, explicitly call a parent class constructor, pass in 4 parameters
Overriding the OnCreate () and Onupgrade () methods
You can create a database by calling either the Getwritabledatabase () or the Getreadabledatabase () method
Database file is created when the database file does not exist, and the OnCreate () method is executed
No method is executed when the database file exists and the version does not change
Database file exists, version promoted, execute Onupgrade () method
3. Adding and deleting changes
Additions and deletions can be done using the Sqlitedatabase.execsql () method to execute the SQL statement
The query method needs to use the Sqlitedatabase.rawquery () method to query, get a cursor, and then call MoveToNext () and GetString () GetInt () and other methods to get the data

Java Siege Lion Road (Android)--sqlite

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.