目錄 html
1、構建Android開發環境... 4 java
1.用eclipse作Android的開發... 4 android
2.用手機作虛擬設備... 4 sql
2、生命週期... 4 shell
2.參考的調用順序... 5 api
(1)日誌點... 5 多線程
(2)應用程序提示... 6 oracle
(2)繼承ContentProvider並重載其中的方法... 29
(3)AndroidManifest.xml中註冊ContentProvider 29
正文
(1) 下載JDK的可執行文件並執行
(2) 下載adt的zip壓縮包
(3) 下載較新的SDK並解壓SDK
(4) 下載eclipse並運行,選擇help->install new software->add->archive->下載完畢的adt壓縮文件
(5) 去掉「contact all update sites during install to find required software」選項,提升安裝速度
(6) 重啓eclipse後選擇SDK解壓後的位置
(7) Windows系統利用上vt技術的話,AVD的反應速度要快不少:啓動BOIS啓動vt,在sdk\extras\intel\Hardware_Accelerated_Execution_Manager下安裝intel加速器,在此基礎上創建一個intel驅動的AVD並啓動
(8) 建立新項目
下面用的是SUMSUNG手機,這種辦法只能看到當前應用的信息
(1)About phone->Software information
(2)連續點擊Build number3次
(3)在Developer options中打開USB debugging,並選擇適當設置
(4)用USB鏈接手機,就能夠經過IDE選擇這個手機作模擬設備,運行須要調試的應用
程序在Android系統中從啓動到終止的全部過程,程序不能控制自身的生命週期而受系統的調度和控制。
onSaveInstanceStae()用於保存activity界面的臨時信息、信息通常放在Bundle中,onRestoreInstanceState()用於程序被銷燬時恢復臨時信息、好比程序遇到鎖屏或者被系統中斷,可是它們都不是生命週期的時間回調函數。
onCreate() |
用於activity的初始化、建立鏈接、綁定數據等,是第一個被調用的函數 |
onStart() |
activity顯示在屏幕上時被調用 |
onRestart() |
activity從新啓動時調用、以後調用onResume()繼續和用戶交互 |
onResume() |
獲取屏幕焦點、接收用戶輸入時被調用 |
onPause() |
程序被其餘窗口覆蓋,系統調用函數「暫停」程序、以後調用onSaveInstanceStae()保存臨時信息 |
onStop() |
用戶不可見時,進入中止對activity更新 |
onDestroy() |
程序調用finish()函數或者被系統終結的時候調用 |
當用戶離開當前頁面又從新回到這個頁面時,會調用onStart()、onStop()
好比Log.i(String TAG,String cnt);,還能夠選.w、.e等不一樣的日誌信息等級,查看日誌的方法是在Logcat中創建過濾器,輸入Filter Name、Tag的內容(即字符串TAG),選擇日誌的級別肯定便可。
好比Toast.makeText(MainActivity.this, "服務未綁定", Toast.LENGTH_SHORT).show();
做爲前兩種辦法的補充,若是前面的辦法還不能分析出錯誤緣由,再考慮DevTools提供的信息。
模型、視圖、控制器,控制器加工用戶輸入的數據、交給處理核心「模型」,最終由「模型」來更新視圖。
用eclipse的layout設計界面製做靜態的UI,用java控制動態的內容、先聲明一個控件變量而後用findViewById引用xml中的資源進行初始化。具體的函數能夠查閱android的API文檔。
控件的屬性在設計界面中有,也能夠查閱android的手冊。
TextView
設計界面中雙擊控件,能夠進入控件的xml文件
<TextView android:id=」@+id/TextView0」
android:layout_width=」wrap_content」
android:layout_height=」wrap_content」
android:text=」TextView0」/>
EditText經常使用函數
getText().toString() |
獲取文本內容 |
setText() |
設置文本內容 |
selectAll() |
選中所有 |
findFocus() |
獲取輸入的焦點 |
Button、ImageButton
CheckBox用於多選、RadioGroup和RadioButton用於單選。
經常使用函數
isChecked() |
控件是否被選中 |
Spinner是一個相似下拉列表的東西,由於可能有不一樣的選項、存在動態的內容,須要java寫適配器將要顯示的內容和底層的數據統一塊兒來。一個可行的例子是:
//spinner初始化
spinner = (Spinner) findViewById(R.id.spinner1);
//要顯示的數據
List<String> li=new ArrayList<String>();
li.add("cnt0");
li.add("cnt1");
li.add("cnt2");
li.add("cnt3");
//適配器初始化
ArrayAdapter<String> ad=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,li);
//適配器樣式
ad.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//給spinner設置適配器ad
spinner.setAdapter(ad);
ListView中若是內容不少,就會出現滾動條;若是沒有內容,將不顯示控件。
監聽器屬於控件的方法,將使用內部類、若是出現錯誤多是沒有加上View.,在android_api.chm中也有具體的說明。好比OnClickListener、
① 控件獨佔一個監聽器
好比列表控件中給每一個子項設置監聽器,listView.setOnItemClickListener(new AdapterView.OnIOnItemClickListener(){…});
② 多個控件共享一個監聽器
方法是定義一個監聽器,而後用v.getId()區分各個控件寫上須要的代碼,最後控件綁定這個監聽器。若是一種類型的控件屢次出現,儘可能用這種方法設置監聽器,消除一些沒必要要的冗餘,。
佈局之間能夠相互嵌套,按經常使用的程度分爲:
LinearLayout能夠選擇水平也能夠選擇垂直,還能夠像html中table的行列組合使用、這樣就靈活多了。
RelativeLayout使用相對的位置以適應不一樣的屏幕尺寸,靈活並且比較穩定。
和表格佈局相似,可是網格佈局中的元素能夠佔用多個網格,而表格佈局中的元素不能跨行跨列。
TableLayout默認控件獨佔一行,TableRow默認控件儘可能放在一行。
在layout設計界面中已經找不到,可是在手動建立佈局的xml文件的時候有這樣一種佈局,部件的位置像座標同樣嚴格的受到數值的控制、不一樣的設備上效果不必定好。
一種相似座標的自由佈局FrameLayout
能夠在onCreate函數中用getActionBar().hide();隱藏操做欄的菜單,也能夠經過修改app的主題styles作到一樣的效果
在menu目錄中用layout設計菜單、onCreateOptionsMenu函數中用getMenuInflater().inflate方法映射成Menu對象,以後在onOptionsItemSelected函數中寫入動態交互的內容便可;也能夠用純代碼的方式實現,好比:
一級菜單 |
menu.add(0, menu0, 0, "打印").setIcon(R.drawable.ic_launcher); |
多級菜單 |
menu.addSubMenu(0, menu0, 0, "新建").setIcon(R.drawable.ic_launcher); |
說明:menu0是int對象要提早用Menu.FIRST初始化 |
具備快捷菜單的元素,若是按住它超過兩秒,就會啓動它的快捷菜單;和菜單資源相似,不一樣的是須要重載onCreateContextMenu函數,最後對須要的元素註冊快捷菜單,方法是registerForContextMenu。
把頁面中可能重複使用的部分作成Fragment,在一個activity中調用、提升了代碼的可重用性。
① layout設計界面中添加須要的Fragment、建立繼承自Fragment的類
② 設計Fragment對應的xml界面
③ 重載Fragment類中的函數onCreateView、改寫return inflater.inflate(R.layout.bfragment, container, false);便可。
監聽器中的函數的返回值爲true將阻止事件傳遞給界面元素,界面元素就不會更新;程序中和事件有關的變量,它的信息能夠類的聲明中找到。
比較典型的函數 |
值的說明 |
函數的說明 |
getAction() |
ACTION_DOWN、ACTION_UP、ACTION_MOVE等 |
獲取動做 |
getX()、getY() |
獲取相對座標,相對前一個位置 |
|
getRawX()、getRawY() |
獲取絕對座標 |
|
getPressure() |
觸點的壓力 |
|
getSize() |
觸點的尺寸 |
|
getHistorical…() |
獲取歷史數據中的第幾個數據 |
不論這些組件是否在一個應用程序中均可以用Intent進行通訊,顯式通訊須要指明組件具體的類,隱式通訊發出動做便可、信息的接收方由系統決定。
(1)顯式通訊,好比用Intent啓動一個activity(這兩個activity都已經在Manifest中註冊):
Intent intent=new Intent(IntentDemoActivity.this,NewActivity.class);
startActivity(intent);
(2)隱式通訊,由動做觸發:
Internt intent=new Intent(Intent.ACTION_VIEW,Uri.parse(「http://cn.bing.com/」));
startActivity(intent);
動做 |
說明 |
ACTION_VIEW |
提供瀏覽的activity,Uri「http://...」表示網頁地址、「tel:..」是電話號碼 |
(3)獲取子activity的返回值
父activity中設置子activity的標籤,以後發送消息,好比:
SubActivity1就是一個用於區分不一樣子activity的標籤
Intent intent=new Intent(MainActivity.this, Activity1.class);
startActivityForResult(intent,SubActivity1);
子activity封裝Uri信息,並設置結果,好比:
finish()無關緊要
Uri data=Uri.parse(editText.getText().toString());
Intent result=new Intent(null, data);
setResult(RESULT_OK, result);
finish();
父activity接收消息,須要重載函數onActivityResult:
函數的參數依次是標籤、設置的結果標記、Uri數據;在函數中寫須要的代碼
這是隱式啓動的匹配機制:當應用安裝到Android系統的時候,會註冊本身組件和Intent過濾器,這樣系統就能夠經過Intent過濾器映射每個Intent請求到相應的組件上;能夠在Manifest配置文件中、特定的組件下定義<intent-filter>節點,獲得組件的Intent過濾器
(1)Intent過濾器的標籤
標籤 |
屬性 |
說明 |
|
<action> |
android:name |
動做,VIEW視圖、包名 |
|
<category> |
android:category |
分類,LAUNCHER啓動時最早被顯示、DEFAULT默認 |
|
<data> |
android:host |
數據 |
指定主機名 |
android:mimetype |
指定activity能處理的文檔類型 |
||
android:path |
Uri路徑 |
||
android:port |
端口號 |
||
android:scheme |
指定的協議 |
(2)使用Intent過濾器
若是一個activity的過濾器是這樣定義的:
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="schemodemo" android:host="edu.hrbeu" />
</intent-filter>
那麼能夠用Intent這樣調用activity:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("schemodemo://edu.hrbeu/"));
startActivity(intent);
須要一個區別於其餘全局消息的標誌,能夠是程序包的名稱,以後發送鍵值對形式的信號:
Intent intent=new Intent("com.example.demo0");
intent.putExtra("key", editText.getText().toString());
sendBroadcast(intent);
①Manifest文件下添加receiver,並聲明它的過濾器(能夠接收的消息類型),好比:
<receiver android:name=".Activity1">
<intent-filter>
<action android:name="com.example.demo0"/>
</intent-filter>
</receiver>
②定義一個activity,好比是Activity一、它繼承自BroadcastReceiver,以後重載onReceive函數用來處理接收到的數據:
String value=intent.getStringExtra("key");
Toast.makeText(context, value, Toast.LENGTH_SHORT).show();
指的是沒有界面且長時間在後臺運行的應用功能,很典型的是Service服務組件,好比MP3播放器,界面關閉後音樂還在播放,這就須要用到Service,此外Service還能夠用於進程間的通訊。
boolean是java中的布爾值 |
java類中的變量聲明立刻就能夠初始化,這點和CC++不一樣 |
null至關於CC++中的NULL |
① Manifest中註冊Service:
<service android:name=".RandomService"></service>
② 定義一個activity繼承自Service,能夠在onStart函數中、也能夠其餘合理的地方寫須要的功能
③ 顯式調用、中止Service:
final Intent intent=new Intent(MainActivity.this, RandomService.class);
startService(intent);
stopService(intent);
④ Manifest中註冊Service,不一樣的是須要添加過濾器(聲明能夠接收的數據類型,甚至能夠精確到Service類的名字):
<service android:name=".RandomService">
<intent-filter>
<action android:name="com.example.demo0"/>
</intent-filter>
</service>
⑤ 定義一個activity繼承自Service,能夠在onStart函數中、也能夠其餘合理的地方寫須要的功能
⑥ 隱式調用、中止Service,隱式調用的時候發出動做action便可:
final Intent intent=new Intent("com.example.demo0");
startService(intent);
stopService(intent);
Android系統中若是一個線程處理的時間過長,會下降用戶界面的響應速度、甚至使界面失去響應,將耗時的處理過程分解到子線程上是一個可行的解決辦法。
(1)在主線程activity中定義Handle變量,用來接收服務線程Runable中時刻變化的數據:
static Handle handle;
public static void updateGUI(服務線程傳入的新數據){
handle.post(refreshLabel);
}
static Runable refreshLabel=new Runable(){
@Override
public void run() {
// TODO Auto-generated method stub
用新數據更新UI界面
}
}
(2)Service中運用多線程:
⑦ 定義用於事務處理的線程:
Thread serviceThread;
Runnable backgroundWork = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
while (!Thread.interrupted()) {
//過程代碼,用於提供服務
double randNum = Math.random();
MainActivity.updateGUI(randNum);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
⑧ onCreate函數初始化這個線程:
serviceThread = new Thread(null, backgroundWork, "serviceThread");
⑨ onStart函數檢查線程是否正常工做:
if (!serviceThread.isAlive()) {
serviceThread.start();
}
⑩ onDestory函數終止線程:
serviceThread.interrupt();
最簡單的Service不接收參數也不返回參數,只接受調用;多線程的Service不接收參數本身產生變化,並經過調用主線程中的某些函數達到更新UI的目的;綁定服務的Service能夠接收參數也能夠返回參數,有Service的做用(避免界面失去響應、提供服務)、又能夠像函數同樣方便使用,這是頗有意義的。
(1)新建一個繼承自service的類,好比MathService並在類中寫入須要提供服務的函數
(2)新建一個IBinder對象,重載onBind()和onUnbind()函數:
IBinder mBinder=new LocalBinder();
class LocalBinder extends Binder{
MathService getService() {
return MathService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return mBinder;
}
@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
return false;
}
(3)主線程的activity中聲明一個服務類的變量,重載一個ServiceConnection變量:
MathService mathService = null;
ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
//服務意外斷開的時候,系統調用的函數
mathService = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
mathService = ((MathService.LocalBinder) service).getService();
}
};
(4)綁定服務
Intent serviceIntent = new Intent(MainActivity.this, MathService.class);
bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE);
(5)取消綁定
//這個函數未必有用
unbindService(mConnection);
mathService = null;
(6)提供服務
int res = mathService.add(a, b);
2. 遠程服務
這是除Intent外的另外一種進程間的通訊方式,能夠將服務和調用者以不一樣應用的方式在一個系統中獨立起來。
(1) 創建一個Service,以後用AIDL定義遠程服務的接口,即寫一個.aidl文件,好比:
AIDL即安卓接口定義語言,語法上相似java,可是AIDL容許參數有方向
package com.example.demo0;
interface IMathService{
int add(int a,int b);
}
(2) Service中須要一個Binder對象:
//也是這個Service提供的服務
IMathService.Stub mBinder=new IMathService.Stub() {
@Override
public int add(int a, int b) throws RemoteException {
// TODO Auto-generated method stub
return a+b;
}
};
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return mBinder;
}
@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
return false;
}
(3) 新建一個調用者的工程,將Service中的AIDL連同包複製過來
(4) 須要一個ServiceConnection:
// 可以提供服務的對象
IMathService mathService;
ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
mathService = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
mathService = IMathService.Stub.asInterface(service);
}
};
(5) 綁定服務
Intent serviceIntent = new Intent();
serviceIntent.setAction("top.guomc.demo0.MathService");
bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE);
(6) 取消綁定
unbindService(mConnection);
mathService = null;
(7) 提供服務
int res = 0;
try {
res = mathService.add(a, b);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
在遠程服務中,java基本數據類型的打包過程是自動完成的,可是自定義的數據(類)須要重載Parcelable接口來完成打包的過程
(1)自定義數據的.aidl文件,AllResult.aidl:
package top.guomc.demo0;
parcelable AllResult;
(2)提供服務函數的.aidl文件,IMathService.aidl:
package top.guomc.demo0;
import top.guomc.demo0.AllResult;
interface IMathService{
AllResult computeAll(int a,int b);
}
(3)自定義的類,這個類重載了Parcelable接口,AllResult.java
定義內部數據
//序列化
static Parcelable.Creator<AllResult> CREATOR = new Creator<AllResult>() {
@Override
public AllResult[] newArray(int size) {
// TODO Auto-generated method stub
return new AllResult[size];
}
@Override
public AllResult createFromParcel(Parcel source) {
// TODO Auto-generated method stub
return new AllResult(source);
}
};
定義構造函數
//內部數據寫到包
@Override
public void writeToParcel(Parcel dest, int flags) {
// TODO Auto-generated method stub
dest.writeInt(addRes);
dest.writeInt(subRes);
dest.writeInt(mulRes);
dest.writeDouble(divRes);
}
(4)定義Service,MathService.java:
IMathService.Stub mBinder = new IMathService.Stub() {
@Override
public AllResult computeAll(int a, int b) throws RemoteException {
// TODO Auto-generated method stub
int addRes = a + b;
int subRes = a - b;
int mulRes = a * b;
double divRes = (double) a / b;
AllResult res = new AllResult(addRes, subRes, mulRes, divRes);
return res;
}
};
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return mBinder;
}
@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
return false;
}
(5)將服務方的自定義類型連同.aidl、目錄複製到接收方
(6)ServiceConnection、綁定服務、取消服務和上面同樣
(7)提供服務
AllResult allResult = null;
try {
allResult = mService.computeAll(a, b);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
用定義過的getter函數將allResult的內部數據獲取出來
SharedPreferences用於存取應用臨時產生的數據、文件用於存取應用的非結構化數據、資源用於存取應用的內置數據、數據庫用於存取應用的結構化數據,數據庫能夠完成以上目標
簡單的鍵值對讀寫,xml文件將保存在/data/data/包名/shared_prefs/下;若是面對的是複雜類型的數據或是要保存到SD卡上,這種方式仍是有缺陷的
(1)聲明SharedPreferenes的名字和模式:
static final String PREF_NAME="person";
static final int MODE=Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE;
訪問模式:
MODE_PRIVATE |
私有 |
MODE_WORLD_READABLE |
全局讀 |
MODE_WORLD_WRITEABLE |
全局寫 |
(2)在onStart()中獲取一個SharedPreferences,讀取用戶個性化的數據來作初始化(通常程序都會這麼作):
SharedPreferences sp=getSharedPreferences(PREF_NAME, MODE);
editText1.setText(sp.getString("name", "Tom"));
(3)在onStop()中獲取一個SharedPreferences,用來保存數據到xml
SharedPreferences sp=getSharedPreferences(PREF_NAME, MODE);
SharedPreferences.Editor editor=sp.edit();
editor.putString("name", editText1.getText().toString());
文件會被保存在/data/data/包名/files/目錄下
FileOutputStream fos(String filename,Context.mode);
mode的選擇是:
MODE_PRIVATE |
私有 |
MODE_APPEND |
追加 |
MODE_WORLD_READABLE |
全局可讀 |
MODE_WORLD_WRITEABLE |
全局可寫 |
String str=」Some data啊」;
fos.write(str.getBytes());
fos.flush();
fos.close();
FileInputStream fis=openFileInput(String filename);
讀文件
byte[] readBytes=new byte[fis.available()];
while(fis.read(readBytes)!=-1);
String str=new String(readBytes);
在SD卡T-Flash上存儲的數據是沒有訪問權限的控制的,可是能夠保存一些大文件而不佔用內部存儲器的空間,模擬器啓動時會自動加載SD卡出現新的路徑/mnt/sdcard/,文件會被存儲在/storage/sdcard/下
在AndroidManifest.xml中設置加載卸載文件系統、向外部存儲器讀寫數據的權限
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
String tmpFileName = "SdcardFile-" + System.currentTimeMillis() + ".txt";
File dir = new File("/sdcard/");
if (dir.exists() && dir.canWrite()) {
fileName = dir.getAbsolutePath() + "/" + tmpFileName;
File newFile = new File(fileName);
try {
newFile.createNewFile();
if (newFile.exists() && newFile.canWrite()) {
FileOutputStream fos = new FileOutputStream(newFile);
// str是要寫入的String類型數據
fos.write(randNum.getBytes());
fos.flush();
fos.close();
stateView.setText(tmpFileName + "已寫入SD卡");
randNumView.setText("");
}
} catch (Exception e) {
Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_LONG).show();
}
}
File dir = new File("/sdcard/");
if (dir.exists() && dir.canWrite()) {
File newFile = new File(fileName);
try {
newFile.createNewFile();
if (newFile.exists() && newFile.canRead()) {
FileInputStream fis = new FileInputStream(newFile);
byte[] readBytes = new byte[fis.available()];
while (fis.read(readBytes) != -1);
String str = new String(readBytes);
randNumView.setText("讀取的對應內容是:\n" + str);
}
} catch (Exception e) {
e.printStackTrace();
}
}
視頻、音樂、圖片等原始文件放置在項目的/res/raw/下、在應用打包編譯的時候不容許修改;xml做數據的文件放置在/res/xml/下,在打包編譯的時候會被轉換爲二進制文件、以提升訪問的效率;在項目中能夠直接新建獲得
Resources res=MainActivity.this.getResources();
try{
//raw_file是對應的原始文件名、不含後綴
InputStream ips=res.openRawResource(R.raw.raw_file);
byte[] readBytes=new byte[ips.available()];
while(ips.read(readBytes)!=-1);
ips.close();
//str是讀出的String類型變量
String str=new String(readBytes,"utf-8");
}catch(Exception e){
e.printStackTrace();
}
<people>
<person name=」李」 age=」21」 height=」1.81」/>
<person name=」王」 age=」25」 height=」1.76」/>
<person name=」張」 age=」20」 height=」1.69」/>
</people>
//people是對應目錄下的xml文件名、不含後綴
XmlPullParser parser = MainActivity.this.getResources().getXml(R.xml.people);
String str = "";
try {
while (parser.next() != XmlPullParser.END_DOCUMENT) {
//用於接收鍵值對中鍵的信息
String tmp = parser.getName(), name = null, age = null, height = null;
if (tmp != null && tmp.equals("person")) {
int i, count = parser.getAttributeCount();
for (i = 0; i < count; ++i) {
//獲取鍵值對的信息
String attrName = parser.getAttributeName(i);
String attrValue = parser.getAttributeValue(i);
if (attrName != null && attrName.equals("name"))
name = attrValue;
if (attrName != null && attrName.equals("age"))
age = attrValue;
if (attrName != null && attrName.equals("height"))
height = attrValue;
if(name!=null && age!=null && height!=null)
str += "姓名:" + name + ",年齡:" + age + ",身高:" + height + "\r\n";
}
}
}
displayView.setText(str);
} catch (Exception e) {
Log.e("ResourceFileDemo", e.getMessage(), e);
}
數據保存在/data/data/包名/databases/下,默認是私有的;sqlite3在sdk/platform-tools/下,能夠經過同目錄的adb.exe鏈接模擬器的Linux系統作進一步的shell操做
能夠經過adb的shell命令建立對應目錄下的文件夾databases、再建立.db文件,可是在shell狀態下不能輸入中文等unicode字符;也能夠經過eclipse的DDMS建立對應的文件夾、再上傳.db文件
使用多個DBAdapter就能夠實現多數據集多表、單數據集單表的操做,具體代碼見../io/DBAdapter0.0.rar
①Cursor指針的經常使用方法
moveToFirst() |
移動到第一條數據上 |
moveToNext() |
移動到下一條數據上 |
moveToPrevious() |
移動到上一條數據上 |
getCount() |
獲取集合中元素的數目 |
getColumnIndexOrThrow() |
獲取指定屬性名稱的序號,屬性不存在會拋出異常 |
getColumnName() |
獲取指定序號的列屬性名稱 |
getColumnNames() |
獲取列屬性的數組 |
getColumnIndex() |
根據列名稱返回序號 |
moveToPosition() |
將指針移動到指定的數據上 |
getPosition() |
返回當前指針的位置 |
②query()的參數,從左到右
還有一種比較方便的辦法rawQuery(SQL語句,參數說明的數組),若是SQL語句完整(語句中沒有?)、數組的地方填null
參數類型 |
說明 |
String |
表的名稱 |
String[] |
屬性列的名稱 |
String |
對應select語句,查詢條件 |
String[] |
模糊查詢的替換符 |
String |
對應groupBy語句,分組方式 |
String |
對應having語句,組的過濾器 |
String |
對應orderBy語句,排序方式 |
一種應用層面共享數據的方法,ContentProvider實現後、其餘應用經過ContentResolver對象發送URI修改其中的共享數據庫;實現的代碼具體見io/ ContentProvider0.0.rar
要注意的是加上SDK的版本限制容易致使錯誤
操做數據庫 |
ContentValues封裝數據、再封裝到Uri中、ContentResolver執行對應功能的函數 |
獲取數據庫中的數據 |
Cursor接收ContentResolver的返回值,數據再從Cursor中獲取 |
繼承自Activity的類能夠管理這些標籤頁,若是要實際使用,還須要在AndroidManifest<application/>下面註冊這個類。
(1)在layout設計界面中製做tab標籤頁,並將標籤頁加入R的id資源中
(2)建立繼承自TabActivity的類做爲入口,用getTabHost()初始化tabHost
(3)將tab標籤頁轉爲java管理的對象,好比LayoutInflater.from(this).inflate(R.layout.tab1, tbHost.getTabContentView());
(4)tabHost添加上這些標籤頁,好比tbHost.addTab(tbHost.newTabSpec("tab3").setIndicator("FrameLayout").setContent(R.id.layout3));,其中「tab3」是程序中的標記,而「FrameLayout」是標籤將顯示的標題。
www.oracle.com/ |
下載JDK |
alt+shfit+n |
新建工程 |
alt+shift+r |
重命名 |
alt+shift+s |
選擇內置函數,好比繼承某些內置的方法 |
alt+shift+j |
生成註釋塊 |
ctrl+d |
刪除一行或者選中塊 |
alt+/ |
關鍵字提示 |
alt+enter |
屬性 |
alt+上下鍵 |
移動選中塊代碼 |
選中文件alt+shift+r |
重命名 |
ctrl+alt+上下鍵 |
複製選中塊,能夠在preference中設置 |
ctrl+shift+f |
快速格式化 |
ctrl+shift+b |
加斷點、去掉斷點 |
選中,ctrl+shift+/或者\ |
註釋、反註釋 |
ctrl+/ |
快速註釋、反註釋一行 |
ctrl+l |
快速到某一行 |
ctrl+f11 |
運行代碼 |
ctrl+點擊 |
查看更詳細的定義 |
alt+shift+a |
列編輯 |
adb即Android Debug Bridge安卓調試橋,用於鏈接Linux系統、管理android設備或模擬器;用命令行工具進入platform-tools,下面的命令都須要在前面加上adb,纔會有效
install .apk的完整路徑 |
安裝某個安卓文件 |
uninstall .apk的完整包名 |
卸載某個安卓文件 |
help |
幫助命令 |
version |
adb的版本信息 |
devices |
正在運行的模擬器 |
shell 命令 |
執行shell命令 |
shell |
進入shell模式 |
push 本地文件 目標路徑 |
上傳本地文件 |
pull 目標路徑 [本地路徑] |
將設備上的文件下載下來,本地路徑默認是adb的路徑 |
avd即安卓虛擬設備,進入tools目錄下,能夠用命令行建立和編譯安卓程序;
android list targets |
鏡像文件清單 |
android list avds |
avd的清單 |
設置->語言和輸入法;
語言,選擇中文(中國);
鍵盤和輸入法,選擇谷歌拼音輸入法;
默認,選擇谷歌拼音輸入法;
關閉硬件物理鍵盤