Android——ContentProvider
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.chenshuai.myapplication.ActivityContentProvider"
android:orientation="vertical"> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查询数据"
android:onClick="chaxun_onclick"/> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="添加数据"
android:onClick="tianjia_onclick"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="更新数据"
android:onClick="gengxin_onclick"/> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="删除数据"
android:onClick="shanchu_onclick"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="读取联系人信息"
android:onClick="duqu_onclick"/> </LinearLayout>
JAVA
package com.example.chenshuai.myapplication; import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast; public class ActivityContentProvider extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_content_provider);
/*
Intent intent = new Intent(Intent.ACTION_DIAL); Uri uri = Uri.parse("tel:110"); intent.setData(uri);*/
}
public void chaxun_onclick(View view)
{
//获得解析器
ContentResolver contentResolver = getContentResolver(); Uri uri = Uri.parse("content://com.example.cheshuai.test.activityhhh1/black_number"); /*contentResolver.update(uri,new ContentValues(),"",new String[]{}); Toast.makeText(ActivityContentProvider.this, "调用内容提供者", Toast.LENGTH_SHORT).show();*/ Cursor cursor = contentResolver.query(uri, null, null, null, null); while (cursor.moveToNext())
{
Toast.makeText(ActivityContentProvider.this, "遍历数据:_id = "+cursor.getLong(0)+
"phone_number= "+cursor.getString(1), Toast.LENGTH_SHORT).show();
} cursor.close();
}
public void tianjia_onclick(View view)
{
//获得解析器
ContentResolver contentResolver = getContentResolver(); Uri uri = Uri.parse("content://com.example.cheshuai.test.activityhhh1/black_number"); /*contentResolver.update(uri,new ContentValues(),"",new String[]{}); Toast.makeText(ActivityContentProvider.this, "调用内容提供者", Toast.LENGTH_SHORT).show();*/ ContentValues c= new ContentValues();
c.put("phone_number","123456"); uri = contentResolver.insert(uri,c); long id = ContentUris.parseId(uri); Toast.makeText(ActivityContentProvider.this, "新数据的id="+id, Toast.LENGTH_SHORT).show();
}
public void gengxin_onclick(View view)
{
//获得解析器
ContentResolver contentResolver = getContentResolver(); Uri uri = Uri.parse("content://com.example.cheshuai.test.activityhhh1/black_number"); /*contentResolver.update(uri,new ContentValues(),"",new String[]{}); Toast.makeText(ActivityContentProvider.this, "调用内容提供者", Toast.LENGTH_SHORT).show();*/ ContentValues c= new ContentValues(); c.put("phone_number","123456"); int content = contentResolver.update(uri, c, null, null); Toast.makeText(ActivityContentProvider.this, "返回修改的数据?"+content, Toast.LENGTH_SHORT).show();
}
public void shanchu_onclick(View view)
{
ContentResolver contentResolver = getContentResolver(); Uri uri = Uri.parse("content://com.example.cheshuai.test.activityhhh1/black_number"); int btn = contentResolver.delete(uri, "_id>?", new String[]{"1"}); Toast.makeText(ActivityContentProvider.this, "删除数据条数"+btn, Toast.LENGTH_SHORT).show(); }
public void duqu_onclick(View view)
{
ContentResolver contentResolver = getContentResolver(); //联系人信息的URI 授权
//管理联系人的Uri
//ContactsContract.Contacts.CONTENT_URI //管理联系人电话的Uri
//ContactsContract.CommonDataKinds.Phone.CONTENT_URI
Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null, null); while(cursor.moveToNext())
{
long id = cursor.getLong(cursor.getColumnIndex(ContactsContract.Contacts._ID)); String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); Toast.makeText(ActivityContentProvider.this, "数据id= "+id + "数据name= "+name, Toast.LENGTH_SHORT).show(); //通过id查询联系人的电话信息 Cursor cursor1 = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?", new String[]{id +""},null); while (cursor1.moveToNext())
{
String phone = cursor1.getString(cursor1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); Toast.makeText(ActivityContentProvider.this, "电话信息= "+ phone, Toast.LENGTH_SHORT).show(); }
cursor1.close();
}
cursor.close(); } }
manifest
<!-- 读取联系人权限 -->
<uses-permission android:name="android.permission.READ_CONTACTS" /> <provider
android:name=".MyContentProvider"
android:authorities="com.example.cheshuai.test.activityhhh1"
android:enabled="true"
android:exported="true" />
Android——ContentProvider的更多相关文章
- Android基础 : Android ContentProvider
Android 应用程序通过ContentProvider实现方式统一的数据共享功能. 外界的程序通过ContentResolver接口可以访问ContentProvider提供的数据,在Activi ...
- Android ContentProvider完整案例
ContentData类,提供数据常量: /** * 提供ContentProvider对外的各种常量,当外部数据需要访问的时候,就可以参考这些常量操作数据. * @author HB * */ pu ...
- Android ContentProvider基本用法
转自:https://www.jianshu.com/p/601086916c8f 一.基本概念 ContentProvider是Android系统中提供的专门用户不同应用间进行数据共享的组件,提供了 ...
- Android ContentProvider 启动分析
对于 ContentProvider 还不是很熟悉的同学,可以阅读上一篇 Android ContentProvider 基本原理和使用详解.本文主要是对 contentProvider 的源码进行分 ...
- Android ContentProvider介绍
在Android中数据的存储一共有五种形式,分别是:Shared Preferences.网络存储.文件存储,外储存储.SQLite.但是我们知道一般这些存储都只是在单独的一个应用程序之中达到一个数据 ...
- Android ContentProvider 简单学习
当应用继承ContentProvider类,并重写该类用于提供数据和存储数据的方法,就可以向其他应用共享其数据.以前我们学习过文件的操作模式,通过指定文件的操作模式为Context.MODE_WORL ...
- Android ContentProvider的实现
当Android中的应用需要访问其他应用的数据时,用ContentProvider可以很好的解决这个问题.今天介绍一下ContentProvider的用法. 首先开发ContentProvider有两 ...
- Android ContentProvider和Uri详解 (绝对全面)
ContentProvider的基本概念 : 1.ContentProvider为存储和读取数据提供了统一的接口 2.使用ContentProvider,应用程序可以实现数据共享 3.andr ...
- Android ContentProvider和getContentResolver
安卓系统中的数据库SqlLite操作和java中mysql的数据库操作很不一样,造成这样的原因是因为在安卓中数据库是属于进程的不存在数据库客户端,也不存在数据库服务器. 关于SqlLite数据库的文章 ...
- android ContentProvider学习
1.ContentProvider提供位存储或获取数据提供了统一的接口. 2.使用ContentProvider可以在不同的应用程序之间共享数据. 3.Android为常见的一些数据提供了Conten ...
随机推荐
- 大批量导入数据的SqlBulkCopy类
SqlBulkCopy 这个类用于数据库大批量的数据传递,通常用于新旧数据库之间的更新.关键的一点是,即使表结构不同,也可以通过表字段或者字段位置建立映射关系,将所需的数据导入到目标数据库. 下面代 ...
- Python dict 出现 Key error
解决方法: https://www.polarxiong.com/archives/Python-%E6%93%8D%E4%BD%9Cdict%E6%97%B6%E9%81%BF%E5%85%8D%E ...
- 找不到dubbo:annotaion错误
dubbo 2.8.4 出现找不到dubbo:annotation的错误,其实这个不会影响程序正确的运行,但是看到有红叉心里肯定非常不爽: 解决办法是,将dubbo-2.8.4.jar包,后缀改成.z ...
- 使用NPOI 转换Excel TO HTML (导出格式不如原生Excel好看)
//HSSFWorkbook workbook = ExcelToHtmlUtils.LoadXls(strPath); //ExcelToHtmlConverter excelToHtmlConve ...
- MySql(十):MySQL性能调优——MySQL Server性能优化
本章主要通过针对MySQL Server( mysqld)相关实现机制的分析,得到一些相应的优化建议.主要涉及MySQL的安装以及相关参数设置的优化,但不包括mysqld之外的比如存储引擎相关的参数优 ...
- 【Linux运维-集群技术进阶】Nginx+Keepalived+Tomcat搭建高可用/负载均衡/动静分离的Webserver集群
额.博客名字有点长.. . 前言 最终到这篇文章了,心情是有点激动的. 由于这篇文章会集中曾经博客讲到的全部Nginx功能点.包含主要的负载均衡,还有动静分离技术再加上这篇文章的重点.通过Keepal ...
- Unity获取插件所在目录的巧妙方法
编写插件时,Unity没有提供当前被放置目录的功能.比如资源商店的一些插件需要放在Assets根目录下. 但通过脚本可以反求出所在目录,对于自己写的插件,就避免了类似问题: var scriptObj ...
- Vuex 实践讲解
state 用来数据共享数据存储 mutation 用来注册改变数据状态 getters 用来对共享数据进行过滤操作 action 解决异步改变共享数据 这个四大特征就是核心,如何用怎么用 接下来还是 ...
- vuex入门教程和思考
Vuex是什么 首先对于vuex是什么,我先引用下官方的解释. Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可 ...
- 读取JPG图片的Exif属性(一) - Exif信息简介
https://blog.csdn.net/fioletfly/article/details/53605959 https://blog.csdn.net/a_big_pig/article/det ...