java攻城獅之路(Android篇)--SQLite

來源:互聯網
上載者:User

標籤:android   style   blog   http   color   io   os   ar   使用   

一.Junit
    1.怎麼使用
        在AndroidManifest.xml檔案中進行配置, 在manifest借點下配置instrumentation, 在application借點下配置uses-library
        定義類繼承AndroidTestCast
        定義測試方法, Run As JunitTest
        如果需要判斷測試結果, 可以使用Assert.assertEquals()方法.

下面是利用獨立的測試工程JunitTest來測試工程Junit:

package com.shellway.junit;public class Service {      public int divide(int a,int b){          return a/b;      }}
Service.java
package com.shellway.junit;import junit.framework.Assert;import android.test.AndroidTestCase;public class TestT extends AndroidTestCase {    public void test1(){        Service service = new Service();        System.out.println(service.divide(10, 2));    }    public void test2(){        Service service = new Service();        System.out.println(service.divide(10, 0));    }    public void test3(){        Service service = new Service();        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="21" />       <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.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>
AndroidManifest.xml

JunitTest工程中的:

package com.shellway.junit.test;import junit.framework.Assert;import com.shellway.junit.Service;import android.test.AndroidTestCase;public class MyTest extends AndroidTestCase {    public void test1(){        Service service = new Service();        System.out.println(service.divide(10,2));    }    public void test2(){        Service service = new Service();        System.out.println(service.divide(10, 0));    }    public void test3(){        Service service = new Service();        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="16" />    <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


二.日誌
    1.怎麼使用
        Log.v(), d(), i(), w(), e()
        可以按層級輸出日子資訊, 可以指定Tag

package com.example.logcat;import android.test.AndroidTestCase;import android.util.Log;public class LogTest extends AndroidTestCase {    public void test1(){        System.out.println("System.out");        System.err.println("System.out");    }    public void test2(){        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"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.logcat"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="21" />        <instrumentation        android:targetPackage="com.example.logcat"        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.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>
AndroidManifest.xml


三.讀寫檔案
    1.讀寫SD卡
        擷取SD卡路徑要使用Environment.getExternalStorageDirectory()
        最好在使用SD卡之前判斷狀態, Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)
        寫入SD卡需要許可權, android.permission.WRITE_EXTERNAL_STORAGE
    2.讀寫手機
        可以使用Context.openFileOutput(String, int)方法開啟輸出資料流
        指定檔案名稱後會自動在當前應用所在檔案夾下產生files檔案夾, 在其中建立檔案
        int參數是檔案許可權, 可以是使用Context下的常量進行設定

四.SharedPreferences
    1.什麼是SharedPreferences
        是一個索引值對結構的容器, 類似於Map(Properties), 可以向其中儲存索引值對, 根據鍵尋找值, 儲存在容器中的資料會以xml檔案形式儲存.
    2.怎麼使用
        使用Context.getSharedPreferences(String, int)擷取對象, 指定檔案名稱和檔案模式
        使用SharedPreferences.edit()方法擷取編輯器Editor
        使用Editor.putString(), putInt()等方法儲存資料
        使用Editor.commit()方法提交修改(類似事務)
        擷取的時候直接使用 SharedPreferences.getString(), getInt()等方法擷取資料
        
五.XML
    1.解析
        擷取解析器: Xml.newPullParser()
        設定輸入資料流: parser.setInput(InputStream, String)
        擷取當前事件類型: parser.getEventType(), 共5種
        擷取下一個事件類型: parser.next()
        擷取標籤名: parser.getName()
        擷取屬性值: parser.getAttributeValue(int)
        擷取下一個文本: parser.nextText()
    2.產生
        擷取解析器:
        設定輸出資料流:
        開始文檔:
        結束文檔:
        開啟標籤:
        結束標籤:
        設定屬性:
        設定文本:

六.SQLite資料庫
    1.SQLite資料庫的特點
        手機內建的資料庫, 不區分資料類型(除了主鍵), 文法和MySQL相同, 每個庫是一個檔案
    2.建立庫
        定義類繼承SQLiteOpenHelper, 定義建構函式, 顯式調用父類建構函式, 傳入4個參數
        重寫onCreate()和onUpgrade()方法
        調用getWritableDatabase()或者getReadableDatabase()方法都可以建立資料庫
        資料庫檔案不存在時, 會建立資料庫檔案, 並且執行onCreate()方法
        資料庫檔案存在, 並且版本沒有改變時, 不執行任何方法
        資料庫檔案存在, 版本提升, 執行onUpgrade()方法
    3.增刪改查
        增刪改都可以使用SQLiteDatabase.execSQL()方法執行SQL陳述式完成
        查詢方法需要使用SQLiteDatabase.rawQuery()方法進行查詢, 得到一個Cursor, 再調用moveToNext()和getString()getInt()等方法擷取資料

java攻城獅之路(Android篇)--SQLite

聯繫我們

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