`
QCheng5453
  • 浏览: 15850 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

Android笔记——Day5 *SQLite基本用法 *Broadcast广播机制 *WiFi基础

 
阅读更多

//本文中代码基本上是Mars老师的代码...--#

 

 

1、SQLite基本用法

参考:http://my.oschina.net/cathleencheng/blog/17682

··SQLite是一种轻量级的数据库,时常用于嵌入式开发。

··对于数据库的基本操作有:

1)创建数据库。包括创建表,记录,字段等。

2)打开,关闭数据库。

3)向数据库中插入数据。

4)删除数据库中某些数据。

5)在数据库中查找相应的数据。

··对数据库操作的一般流程:

1)写一个类继承SQLiteOpenHelper类,该类是对数据库创建和打开的帮助类,在该类中必须有构造函数,可以 在该类中重写onCreate()等方法,在数据库创建或更新等操作时被系统回调执行相应代码。

 

public class DatabaseHelper extends SQLiteOpenHelper {
	
	private static final int VERSION = 1;
	//在SQLiteOepnHelper的子类当中,必须有该构造函数
	public DatabaseHelper(Context context, String name, CursorFactory factory,
			int version) {
		//必须通过super调用父类当中的构造函数
		super(context, name, factory, version);
		// TODO Auto-generated constructor stub
	}

	//该函数是在第一次创建数据库的时候执行,实际上是在第一次得到SQLiteDatabse对象的时候,才会调用这个方法
	@Override
	public void onCreate(SQLiteDatabase db) {
		//execSQL函数用于执行SQL语句
		db.execSQL("create table user(id int,name varchar(20))");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
             //... ...在数据库更新时执行的代码
	}

}

 

       2)创建上述类对象,再用其中继承下来的getReadableDatabase()和getWritableDatabase()函数返回 一个SQLiteDatabase数据库类型。

 

DatabaseHelper dbHelper = new DatabaseHelper(SQLiteActivity.this,"test_mars_db");
//只有调用了DatabaseHelper对象的getReadableDatabase()方法,或者是getWritableDatabase()方法之后,才会创建,或打开一个数据库
SQLiteDatabase db = dbHelper.getReadableDatabase();

 

  3)插入数据。

 

ContentValues values = new ContentValues();//该对象可放入键和值。
//想该对象当中插入键值对,其中键是列名,值是希望插入到这一列的值,值必须和数据库当中的数据类型一致
values.put("id", 1);
values.put("name","zhangsan");
DatabaseHelper dbHelper = new             DatabaseHelper(SQLiteActivity.this,"test_mars_db",2);
SQLiteDatabase db = dbHelper.getWritableDatabase();
//调用insert方法,就可以将数据插入到数据库当中
db.insert("user", null, values);//第一个参数是表名

 

4)查找数据。

 

DatabaseHelper dbHelper = new DatabaseHelper(SQLiteActivity.this,"test_mars_db");
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = db.query("user", new String[]{"id","name"}, "id=?", new String[]{"1"}, null, null, null);//第一个数表名,
while(cursor.moveToNext()){//Cursor对象游走在匹配数据上,当移动到末尾时返回false
		String name =     cursor.getString(cursor.getColumnIndex("name"));
		System.out.println("query--->" + name);
			}

/*
public Cursor query (String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy) 
Since: API Level 1 
Query the given table, returning a Cursor over the result set.

Parameters
table  The table name to compile the query against. 
columns  A list of which columns to return. Passing null will return all columns, which is discouraged to prevent reading data from storage that isn't going to be used. 
selection  A filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself). Passing null will return all rows for the given table. 
selectionArgs  You may include ?s in selection, which will be replaced by the values from selectionArgs, in order that they appear in the selection. The values will be bound as Strings. 
groupBy  A filter declaring how to group rows, formatted as an SQL GROUP BY clause (excluding the GROUP BY itself). Passing null will cause the rows to not be grouped. 
having  A filter declare which row groups to include in the cursor, if row grouping is being used, formatted as an SQL HAVING clause (excluding the HAVING itself). Passing null will cause all row groups to be included, and is required when row grouping is not being used. 
orderBy  How to order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself). Passing null will use the default sort order, which may be unordered 
*/

 

  5)其他操作不是很清楚,等编写具体程序的时候再仔细研究一番...--#

 

 

2、Broadcast广播机制。

··Broadcast机制可以想所有程序发送一个广播,某些程序可以接收这些广播进行相应的处理。广播可以由我们自己定义发出,也可以是系统固有的广播。

  ··如何发出广播。广播的发出和调用另一个Activity很像,都是借助Intent类实现的。

 

Intent intent = new Intent();//创建一个Intent对象
intent.setAction(Intent.ACTION_EDIT);//为该Intent对象设置动作,实际上用于接收时的过滤
TestActivity.this.sendBroadcast(intent);//广播该Intent对象

 

  ··如何接收广播。接收广播分为两种。

1)在AndroidMainifest中注册,类似于Activity的注册。用该方法实现接收广播即使该程序关闭,同样也会 收到广播消息。

 

//Java文件,继承了BroadcastReceiver类实现广播的接收,每次程序收到正确的广播,就会创建该类的一个对象。
public class TestReceiver extends BroadcastReceiver{
        //覆写其中的onReceive()函数。该函数在收到广播时被回调。
	@Override
	public void onReceive(Context context, Intent intent) {
		System.out.println("onReceive");
	}
}

 

  <!--AndroidManifest中注册该接收器,并声明需要的action。-->

<receiver android:name=".TestReceiver">
		<intent-filter>
				<action android:name="android.intent.action.PICK" />
		</intent-filter>
</receiver>
 

2)在程序中创建一个类继承BroadcastReceiver类,并在程序代码中动态注册该类对象和取消该类对象,用 法灵活方便。

 

public class SMSReceiver extends BroadcastReceiver{
	@Override
	public void onReceive(Context context, Intent intent) {
              //覆写onReceive()方法,表明收到广播时需要做什么
	}

}

 

smsReceiver = new SMSReceiver();
//生成一个IntentFilter对象,该对象为接收器绑定过滤器。
IntentFilter filter = new IntentFilter();
//为IntentFilter添加一个Action
filter.addAction(SMS_ACTION);
//将BroadcastReceiver对象注册到系统当中
TestBC2Activity.this.registerReceiver(smsReceiver, filter);
//TestBC2Activity.this.unregisterReceiver(smsReceiver);//取消注册
 

 

 

3、WiFi基础

对于WiFi网卡的操作,Android中用WifiManager类对其封装,只需要调用类对象相应函数以及查看帮助文档明白其中各常量意义即可。

 

wifiManager = (WifiManager)WifiActivity.this.getSystemService(Context.WIFI_SERVICE);//创建WifiManager对象
wifiManager.setWifiEnabled(true);//打开Wifi
System.out.println("wifi state --->" + wifiManager.getWifiState());//查看Wifi状态。
 

 

 

 

恩,个人觉得Mars老师的教程真心不错,顶一个...--#

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics