博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android——ContentProvider
阅读量:5122 次
发布时间:2019-06-13

本文共 4654 字,大约阅读时间需要 15 分钟。

xml

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

 

转载于:https://www.cnblogs.com/Chenshuai7/p/5454075.html

你可能感兴趣的文章
浅谈 unix, linux, ios, android 区别和联系
查看>>
51nod 1428 活动安排问题 (贪心+优先队列)
查看>>
中国烧鹅系列:利用烧鹅自动执行SD卡上的自定义程序(含视频)
查看>>
Solaris11修改主机名
查看>>
latex for wordpress(一)
查看>>
如何在maven工程中加载oracle驱动
查看>>
Flask 系列之 SQLAlchemy
查看>>
aboutMe
查看>>
【Debug】IAR在线调试时报错,Warning: Stack pointer is setup to incorrect alignmentStack,芯片使用STM32F103ZET6...
查看>>
一句话说清分布式锁,进程锁,线程锁
查看>>
python常用函数
查看>>
FastDFS使用
查看>>
服务器解析请求的基本原理
查看>>
[HDU3683 Gomoku]
查看>>
【工具相关】iOS-Reveal的使用
查看>>
数据库3
查看>>
存储分类
查看>>
下一代操作系统与软件
查看>>
【iOS越狱开发】如何将应用打包成.ipa文件
查看>>
[NOIP2013提高组] CODEVS 3287 火车运输(MST+LCA)
查看>>