ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Android - 四大组件之内容提供者,安卓开发权威指南

2021-09-05 13:00:40  阅读:228  来源: 互联网

标签:提供者 Notification 安卓 uri UriMatcher new Android


  • activity

  • service

  • broadcast receiver

  • content provider 内容提供者

2.内容提供者的作用

应用程序创建的数据库默认都是私有的,别的应用程序不可以访问里面的数据。如果有需求把自己应用程序私有的数据库暴露给别的用户,就需要使用内容提供者

3.创建内容提供者

  1. 创建一个类继承ContentProvider

    
    public class BankDBBackdoor extends ContentProvider {
    
    
    
    }
    
    
    
    
  2. 在清单文件的application节点中进行配置

    
    <application
    
        android:allowBackup="true"
    
        android:icon="@drawable/ic_launcher"
    
        android:label="@string/app_name"
    
        android:theme="@style/AppTheme" >
    
        ...
    
        ...
    
        <provider
    
            android:name="com.mythmayor.db.BankDBBackdoor"
    
            //必须配置该主机名,访问者使用该主机名才能访问
    
            android:authorities="com.mythmayor.db" >
    
        </provider>
    
    </application>
    
    
    
    
  3. 重写内容提供者中的insert等方法

4.访问内容提供者


// 得到内容提供者的解析器

ContentResolver resolver = getContentResolver();

// 访问内容提供者主要通过uri来访问

Uri uri = Uri.parse("content://com.mythmayor.db");

ContentValues values = new ContentValues();

// 通过内容解析器让内容提供者添加一条数据

resolver.insert(uri, values);



5.UriMatcher的使用步骤

  1. 创建一个UriMatcher,并初始化

    
    //初始化为不匹配
    
    static UriMatcher mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    
    
    
    
  2. 创建一些匹配规则

    
    //如果uri满足 content://com.mythmayor.db/account,则返回SUCCESS这个常量值
    
    static {
    
        mUriMatcher.addURI("com.mythmayor.db", "account", SUCCESS);
    
    }
    
    
    
    //系统短信应用的匹配规则
    
    private static final UriMatcher sURLMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    
    static {
    
        sURLMatcher.addURI("sms", null, SMS_ALL); //所有短信
    
        sURLMatcher.addURI("sms", "inbox", SMS_INBOX); //收件箱
    
        sURLMatcher.addURI("sms", "sent", SMS_SENT); //发件箱
    
        sURLMatcher.addURI("sms", "draft", SMS_DRAFT); //草稿箱
    
    }
    
    
    
    
  3. 在insert等方法中,先使用match(Uri uri)方法匹配一个uri,然后根据返回的值进行不同的操作

    
    int code = mUriMatcher.match(uri);
    
    if (code == SUCCESS) {
    
        ...
    
        ...
    
    }else{
    
        ...
    
        ...
    
    }
    
    
    
    

6.内容提供者编写的流程

  1. 写一个类继承ContentProvider,实现增删改查的方法

  2. 在清单文件中配置内容提供者,指定 android:authorities=”com.mythmayor.db”

  3. 在内容提供者代码的内部 声明uriMatcher

  4. 通过uriMatcher 检查uri的路径是否正确

  5. 在另外一个应用程序里面 通过contentResolver 增删改查

7.内容提供者编写的流程

  1. 创建一个类继承ContentProvider

    
    public class BankDBBackdoor extends ContentProvider {
    
    
    
    }
    
    
    
    
  2. 在清单文件的application节点中进行配置

    
    <application
    
        android:allowBackup="true"
    
        android:icon="@drawable/ic_launcher"
    
        android:label="@string/app_name"
    
        android:theme="@style/AppTheme" >
    
        ...
    
        ...
    
        <provider
    
            android:name="com.mythmayor.db.BankDBBackdoor"
    
            //必须配置该主机名,访问者使用该主机名才能访问
    
            android:authorities="com.mythmayor.db" >
    
        </provider>
    
    </application>
    
    
    
    
  3. 在内容提供者代码的内部声明UriMatcher,创建匹配规则

    
    public static final int SUCCESS = 1;
    
    /**
    
     * 创建一个保安,检查uri的规则,如果uri匹配失败 返回-1
    
     */
    
    static UriMatcher mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    
    static {
    
        mUriMatcher.addURI("com.mythmayor.db", "account", SUCCESS);
    
    }
    
    
    
    
  4. 实现增删改查的方法,通过uriMatcher的返回值确定要做什么操作

  5. 在另外一个应用程序里面,通过contentResolver进行增删改查

8.学习内容提供者的目的

  1. 了解内容提供者原理

  2. 能看懂系统源码

  3. 获取系统应用内容提供者所提供的数据,例如联系人、短信应用

9.如何去分析系统应用的内容提供者

  1. 查看数据库,分析数据库的表和字段

  2. 操作内容提供者需要uri

  3. 找到系统应用的源代码,首先去清单文件中查找主机名authorities

    
    <provider android:name="SmsProvider"
    
          android:authorities="sms"
    
          android:multiprocess="true"
    
          android:readPermission="android.permission.READ_SMS"
    
          android:writePermission="android.permission.WRITE_SMS" />
    
    
    
    
  4. 去对应的Provider的源代码中查找匹配规则,确定表名

    
    static {
    
        sURLMatcher.addURI("sms", null, SMS_ALL); //所有短信
    
        sURLMatcher.addURI("sms", "inbox", SMS_INBOX); //收件箱
    
        sURLMatcher.addURI("sms", "sent", SMS_SENT); //发件箱
    
        sURLMatcher.addURI("sms", "draft", SMS_DRAFT); //草稿箱
    
    }
    
    
    
    
  5. 根据主机名和表名确定uri,使用ContentResolver的增删改查方法操作对应的数据库

10.通知栏提醒Notification

显示在另外一个进程的界面里面的

  1. 在低版本中的写法(api小于16),创建Notification时直接new Notification()

    
    NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    
    
    
    //1.初始化Notification
    
    Notification notification = new Notification(R.drawable.ic_launcher, "有新的消息到来了", System.currentTimeMillis()); 
    
    
    
    //2.创建通知栏的点击事件
    
    Intent intent = new Intent();
    
    intent.setAction(Intent.ACTION_CALL);
    
    intent.setData(Uri.parse("tel://110"));
    
    //PendingIntent延时的意图,可以打开Activity、Service和发送广播
    
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
    
    
    
    //3.设置通知的点击事件
    
    notification.setLatestEventInfo(this, "我是标题", "我是文本", contentIntent);
    
    
    
    //4.显示通知
    
    nm.notify(0, notification);  
    
    
    
    
  2. 在高版本中的写法(api大于等于16),创建Notification时使用Notification.Builder

    
    NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    
    
    
    //1.初始化Notification
    
    Notification notification = new Notification.Builder(this)
    
        .setContentTitle("我是标题")
    
        .setContentText("我是文本")
    
        .setSmallIcon(R.drawable.ic_launcher)
    
        .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
    
        .setContentIntent(PendingIntent intent) //设置点击事件
    
        .build();
    
    
    
    //2.显示通知
    
    nm.notify(0, notification); 
    
    
    
    
  3. Notification中使用自定义View

    api小于16

    
    Notification notification = new Notification(R.drawable.ic_launcher, "有新的消息到来了", System.currentTimeMillis());
    
    //设置自定义布局
    
    RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.xxx);
    
    notification.contentView = remoteViews; 
    
    
    

最后我想说

为什么很多程序员做不了架构师?
1、良好健康的职业规划很重要,但大多数人都忽略了
2、学习的习惯很重要,持之以恒才是正解。
3、编程思维没能提升一个台阶,局限在了编码,业务,没考虑过选型、扩展
4、身边没有好的架构师引导、培养。所处的圈子对程序员的成长影响巨大。

金九银十面试季,跳槽季,整理面试题已经成了我多年的习惯!在这里我和身边一些朋友特意整理了一份快速进阶为Android高级工程师的系统且全面的学习资料。涵盖了Android初级——Android高级架构师进阶必备的一些学习技能。

附上:我们之前因为秋招收集的二十套一二线互联网公司Android面试真题(含BAT、小米、华为、美团、滴滴)和我自己整理Android复习笔记(包含Android基础知识点、Android扩展知识点、Android源码解析、设计模式汇总、Gradle知识点、常见算法题汇总。)

里面包含不同方向的自学编程路线、面试题集合/面经、及系列技术文章等,资源持续更新中…

CodeChina开源项目:《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》

程序员的成长影响巨大。

金九银十面试季,跳槽季,整理面试题已经成了我多年的习惯!在这里我和身边一些朋友特意整理了一份快速进阶为Android高级工程师的系统且全面的学习资料。涵盖了Android初级——Android高级架构师进阶必备的一些学习技能。

附上:我们之前因为秋招收集的二十套一二线互联网公司Android面试真题(含BAT、小米、华为、美团、滴滴)和我自己整理Android复习笔记(包含Android基础知识点、Android扩展知识点、Android源码解析、设计模式汇总、Gradle知识点、常见算法题汇总。)

[外链图片转存中…(img-T98QNcQb-1630817019239)]

里面包含不同方向的自学编程路线、面试题集合/面经、及系列技术文章等,资源持续更新中…

CodeChina开源项目:《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》

标签:提供者,Notification,安卓,uri,UriMatcher,new,Android
来源: https://blog.csdn.net/m0_61067964/article/details/120113375

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有