寫一些cursor查詢、更新本地數據庫的操作吧。先舉個例子:
- Cursor c = getContentResolver.query(uri , String[ ] , where , String[ ] , sort);
第一個參數:是一個URI,指向需要查詢的表;
第二個參數:需要查詢的列名,是一個數組,可以返回多個列;
第三個參數:需要查詢的行,where表示需要滿足的查詢條件,where語句裏面可以有?號;
第四個參數:是一個數組,用來替代上面where語句裏面的問號;
第五個參數:表示排序方式;
下面還是用一段代碼來加強下印象:
- Cursor c = getContentResolver.query(Message.Content_URI ,
- new String[]{SyncColumns.Server_Id} , SyncColumns.Id+"=?" , new String[]{Long.toString(MessageId)} , null);
- try {
- if(c.moveToFirst()) {
- return c.getString(0);//0表示返回的行數
- }
- else {
- return null;
- }
- }
- finally {
- c.close();
- }
- ContentValues cv = new ContentValues();
- cv.put(Body.HTML_Content, newHtmlBody);//第一個參數是列名,第二個參數是要放入的值
- String where = Body.Message_Key + "=" + mMessageId;
- getContentResolver().update(uri , cv , where , null);
- //這裏的四個參數應該很清楚了,uri是表,cv上面要更新的值,where是搜索行的語句,null是歷史記錄可以爲空
Android中include的使用
如果在程序中多次用到一部分相同的佈局,可以先將這部分佈局定義爲一個單獨的XML,然後在需要的地方通過<include>引入,如下:
main.xml
- <font size="3"><?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- <include
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/cell1"
- layout="@layout/item"
- android:layout_marginTop="10dp"
- android:layout_marginLeft="45dp" />
- <include
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/cell2" layout="@layout/item"
- android:layout_toRightOf="@+id/cell1"
- android:layout_alignTop="@+id/cell1"
- android:layout_marginLeft="20dp" />
- <include
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/cell3" layout="@layout/item"
- android:layout_toRightOf="@+id/cell2"
- android:layout_alignTop="@+id/cell1"
- android:layout_marginLeft="20dp" />
- </RelativeLayout>
- </font>
- <font size="3"><?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:visibility="invisible">
- <ImageView
- android:background="#000000"
- android:id="@+id/iv_img"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:clickable="true"
- android:focusable="false" />
- <TextView
- android:id="@+id/tv_name"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textColor="#a17006"
- android:textStyle="bold" android:textSize="22dp"
- android:layout_alignLeft="@+id/iv_img"
- android:layout_below="@+id/iv_img" />
- </RelativeLayout>
- </font>
---------------------------------------------------------------------------------
Android得到當前電量信息
通過廣播的方式監聽[Android當前電量信息。
- registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
- private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- String action = intent.getAction();
- if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
-
- int level = intent.getIntExtra("level", 0);
- int scale = intent.getIntExtra("scale", 100);
-
- Log.v(TAG,
- "Battery level: " + String.valueOf(level * 100 / scale)
- + "%");
- }
- }
- };