//如果在AndroidManifest.xml中<provider >不存在的话,在eclipse里面运行这个AP的时候,就会报错,
01-01 21:37:47.617: E/AndroidRuntime(3245): java.lang.RuntimeException: Unable to get provider com.contentp.test3.MyContentProvider: java.lang.ClassNotFoundException: com.contentp.test3.MyContentProvider
依此类推,我认为在AndroidManifest.xml中定义的组件也必须都存在,否则就会出错。
ContentObserver学习:
定义一个activity,监听/取消监听某Uri
package com.contentp.test3;import android.app.Activity;import android.content.ContentResolver;import android.net.Uri;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.provider.Settings;import android.util.Log;//用一个AP访问 系统中存在的联系人APpublic class ContentProviderTest3Activity extends Activity { /** Called when the activity is first created. */ private ContentResolver cr; private AirContentObserver aco; private String TAG = "ContentProviderTest3Activity"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); aco = new AirContentObserver(mHander); regisContentObserver(); } private void regisContentObserver(){ cr = getContentResolver(); Uri mUri = Settings.System.getUriFor(Settings.System.AIRPLANE_MODE_ON); //注册ContentObserver,别忘了unregister. cr.registerContentObserver(mUri, true, aco); } Handler mHander = new Handler(){ @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub super.handleMessage(msg); } }; //别忘了unregister. @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); cr.unregisterContentObserver(aco); Log.v(TAG,"$$$$onDestroy--unregister"); } }
定义ContentObserver的子类,override onChange()方法
package com.contentp.test3;import android.database.ContentObserver;import android.os.Handler;import android.util.Log;public class AirContentObserver extends ContentObserver{ private String TAG = "ContentProviderTest3Activity"; public AirContentObserver(Handler handler) { super(handler); // TODO Auto-generated constructor stub }//所有ContentObserver子类都要override此onChange()方法。 @Override public void onChange(boolean selfChange) { // TODO Auto-generated method stub super.onChange(selfChange); Log.v(TAG,"$$$$$onchange"); }}
这样,当手机的飞行模式开启或关闭的时候,Uri就会发生变化,ContentObserver的子类的onChange()回调方法就会被调用,就会打出来onChange()里面的log。
//
参考别人的:
package com.sql.test1; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; //SQLiteOpenHelper是一个abstract class public class MySqliteHelper extends SQLiteOpenHelper { public static final int VERSION=1; public static final String TABLE_NAME="srx0209"; public static final String ID="_id"; public static final String TEXT="text"; public static final String DATABASE_NAME="srx.db"; public MySqliteHelper(Context context){ super(context, DATABASE_NAME, null, VERSION); } //数据库第一次生成的时候会调用这个方法,一般我们在这个方法里面生成表 @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub //创建表 /* String str_create=//"CREATE TABLE"+TABLE_NAME+"("+ID+"INTEGER PRIMARY KEY AUTOINCREMENT,"+TEXT+"text);"; "CREATE TABLE " + TABLE_NAME + "(" + ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + TEXT + " text );"; db.execSQL(str_create);*/ Log.d("MySqliteHelper", "MySqliteHelper---onCreate--"); } //插入数据后,数据库立即更新。当数据库需要升级的时候,Android系统会自动(主动)调用此方法 @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub Log.d("MySqliteHelper", "$$$$$$$call onUpgrade"); } }
package com.sql.test1; import android.app.Activity; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.widget.Button; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; public class SQLiteTest1Activity extends Activity { /** Called when the activity is first created. */ private static final String TAG = "SQLiteTest1Activity"; private Button btn_new_table; private Button btn_insert; private Button btn_delete_record; private Button btn_delete_table; private Button btn_modify; private Button btn_search; // 创建一个继承SQLiteOpenHelper的实例 private MySqliteHelper myOpenHelper; private SQLiteDatabase sqDatabase; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btn_new_table = (Button) findViewById(R.id.new_table); btn_insert = (Button) findViewById(R.id.insert); btn_delete_record = (Button) findViewById(R.id.delete_record); btn_delete_table = (Button) findViewById(R.id.delete_table); btn_modify = (Button) findViewById(R.id.modify); btn_search = (Button) findViewById(R.id.search); btn_new_table.setOnClickListener(myListener); btn_insert.setOnClickListener(myListener); btn_delete_record.setOnClickListener(myListener); btn_delete_table.setOnClickListener(myListener); btn_modify.setOnClickListener(myListener); btn_search.setOnClickListener(myListener); // 初始化SQLiteOpenHelper的子类的实例,new MySqliteHelper 类实例对象的时候并没有创建数据库! myOpenHelper = new MySqliteHelper(this); // myOpenHelper.getReadableDatabase(); } OnClickListener myListener = new OnClickListener() { public void onClick(View v) { // getReadableDatabase:android // 会分析是否已经有了数据库,若没有会默认为我们创建一个数据库并且在系统路径data-data-包名下生成XX.db 文件! sqDatabase = myOpenHelper.getReadableDatabase(); // TODO Auto-generated method stub if (v.getId() == R.id.new_table) { //若MySqliteHelper已经创建了这个表,故此处按钮可不要,如果再点,会出错,提示表已经存在。 String TABLE_NAME = "srx0209"; String ID = "_id"; String TEXT = "text"; //ID这一列type是integer,TEXT这一列type是text类型。 String str_sql2 = "CREATE TABLE " + TABLE_NAME + "(" + ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + TEXT + " text );"; sqDatabase.execSQL(str_sql2); Log.d(TAG, "####new a table"); } else if (v.getId() == R.id.insert) { ContentValues cv = new ContentValues(); //cv.put(MySqliteHelper.TEXT, "测试新的数据"); cv.put(MySqliteHelper.TEXT, "testnewdata"); sqDatabase.insert(MySqliteHelper.TABLE_NAME, null, cv); Log.d(TAG, "####insert"); } else if (v.getId() == R.id.delete_record) { sqDatabase.delete(MySqliteHelper.TABLE_NAME, MySqliteHelper.ID + "=1", null); Log.d(TAG, "####delete_record"); } else if (v.getId() == R.id.delete_table) { sqDatabase.execSQL("DROP TABLE srx0209"); Log.d(TAG, "####delete_table"); } else if (v.getId() == R.id.modify) { ContentValues cv = new ContentValues(); cv.put(MySqliteHelper.TEXT, "修改后的数据"); sqDatabase.update(MySqliteHelper.TABLE_NAME, cv, "_id " + "=" + Integer.toString(3), null); Log.d(TAG, "####modify"); } else if (v.getId() == R.id.search) { Cursor cur = sqDatabase.rawQuery("SELECT * FROM " + MySqliteHelper.TABLE_NAME, null); if (cur != null) { String temp = ""; int i = 0; while (cur.moveToNext()) { // 直到返回false说明表中到了数据末尾 temp += cur.getString(0); // 参数0 指的是列的下标,这里的0指的是id列 temp += cur.getString(1); // 这里的0相对于当前应该是咱们的text列了 i++; temp += " "; // 这里是我整理显示格式 ,呵呵~ if (i % 3 == 0) // 这里是我整理显示格式 ,呵呵~ temp += "\n";// 这里是我整理显示格式 ,呵呵~ Log.d(TAG, "temp=="+temp); } } Log.d(TAG, "####search"); } } }; }