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);
}
});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值