1. 程式人生 > >Android基礎第九天

Android基礎第九天

內容提供者

應用程式建立的資料庫預設都是私有的,別的應用程式不可以訪問裡面的資料. 如果有需求把自己應用程式私有的資料庫暴露給別的使用者增刪改查,就需要使用內容提供者
重新回顧 怎麼建立一個內容提供者 在清單檔案中怎麼配置?有一個很重要的屬性! ,保安(Uri匹配器)怎麼寫的? 怎麼給這個匹配器新增過濾的規則?
1,寫一個類繼承ContentProvider
2,在清單檔案配置 ,標籤是 重要的屬性是android:authorities=”xxxxxx”
3,Uri匹配器 : static UriMatcher mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
static {
mUriMatcher.addURI(“xxxxxx”, “account”, SUCCESS);
mUriMatcher.addURI(“xxxxxx”, “person”, SUCCESS);
mUriMatcher.addURI(“xxxxxx”, “student”, SUCCESS);

            }

4,然後在重寫delete,update,query,insert方法對本地資料庫進行增刪改查的操作,例如:
@Override
public Uri insert(Uri uri, ContentValues values) {
int code = mUriMatcher.match(uri);
if (code == SUCCESS) {
System.out.println(“insert 新增資料”);
MyDBOpenHelper helper = new MyDBOpenHelper(getContext());
SQLiteDatabase db = helper.getWritableDatabase();

        long id = db.insert("account", null, values);

        //利用內容提供者的解析器,通知內容觀察者資料發生了變化
        getContext().getContentResolver().notifyChange(uri, null);
        return Uri.parse(""+id);
    }else{
        throw new IllegalArgumentException("口令 不正確,滾犢子");
    }

}

2,站在呼叫者的角度,怎麼去找到別人的內容提供者呢?通過什麼類?什麼uri?

注意!! —-> 一般內容提供者的uri寫法是固定的!! content://主機名/xxx (xxx一般來說代表著一個表名),然後呼叫增刪改查的方法去
1,拿到解析器ContentResolver resolver = getContentResolver();
2,Uri 寫的時候要注意前面寫上content://
3,resolver.insert() ,resolver.delete()…. 等

呼叫者使用別人提供的內容提供者:
// 得到內容提供者的查詢器
ContentResolver resolver = getContentResolver();
Uri uri = Uri.parse(“content://com.ithcl.db/account”);

ContentValues values = new ContentValues();
values.put("name", "zhangsan");
values.put("money", 10000);

// 通過內容解析器讓內容提供者新增一條資料
Uri insert = resolver.insert(uri, values);
System.out.println("insert uri :"+insert);

通知欄提醒

通知欄提醒主要分為4.0版本和相容2.x的低版本

4.0版本如下:
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification.Builder(this)
.setContentTitle(“我是標題”)
.setContentText(“我是文字”)
.setAutoCancel(true) //設定點選通知欄之後是否消失
.setSmallIcon(R.drawable.ic_launcher) //小圖示
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher)) //大圖示
.build(); //記得要build,才會生成一個Notification

//顯示一個通知
nm.notify(0, notification); 

相容2.x版本的程式碼:
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, “有新的訊息到來了”, System.currentTimeMillis() + 1000);
notification.flags = Notification.FLAG_AUTO_CANCEL;
//設定通知的點選事件
notification.setLatestEventInfo(this, “我是標題”, “我是文字”, contentIntent);
nm.notify(1005, notification);

我給大家補充的是
PendingIntent不僅可以開啟activity,也可以發廣播,以及開啟service,下面是傳送一個廣播的案例
//這個intent是說明要傳送一個怎麼樣的廣播,頻道,以及資料是什麼
Intent i = new Intent();
i.putExtra(“data”, “aaaaaaaaaaaaaaa”);
i.setAction(“testaction”);
//生成一個傳送廣播的PendingIntent
PendingIntent contentIntent = PendingIntent.getBroadcast(this, 0, i, 0);
//點選通知欄之後消失2.x版本的寫法
notification.flags = Notification.FLAG_AUTO_CANCEL;
//4.0之後點選消失的寫法
Notification notification = new Notification.Builder(this)
.setAutoCancel(autoCancel)

如果想延時的做一件事,可以用handler.postDelayed()這樣的方法就可以了,不需要去開一個子執行緒,裡面執行的相關方法都是在主執行緒執行的
內容觀察者

記住並理解 內容觀察者,和廣播相比較 能更好的記憶和理解 (要有一個傳送方,和一個接收方)

類似廣播,傳送者在哪裡發?通過什麼類,什麼方法傳送的?

//利用內容提供者的解析器,通知內容觀察者資料發生了變化
getContext().getContentResolver().notifyChange(uri, null);

接收者要做什麼事情,才能收到內容提供者傳送的事件?
//註冊一個內容觀察者,當另一方傳送訊息的時候,就可以收到
Uri uri = Uri.parse(“content://com.ithcl.db/account/”);

getContentResolver().registerContentObserver(uri, true, new ContentObserver(new Handler()) {
@Override
public void onChange(boolean selfChange) {
System.out.println(“我是觀察者,我發現銀行的資料庫變化了.”);
super.onChange(selfChange);
}
});