内容解析者,获取联系人和短信数据


转载请注明出处(万分感谢!): 

https://blog.csdn.net/weixin_40790006/article/details/79981352


package com.example.f405.contentresolvertest;

import android.Manifest;
import android.content.ContentValues;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Button btn_querySMS;
    private Button btn_queryContact;
    private TextView tv_info;

    private static final String TAG = "MainActivity";
    private Button btn_insertSMS;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initView() {

        btn_querySMS = (Button) findViewById(R.id.btn_querySMS);
        btn_insertSMS = (Button) findViewById(R.id.btn_insertSMS);
        btn_queryContact = (Button) findViewById(R.id.btn_queryContact);
        tv_info = (TextView) findViewById(R.id.tv_info);


        btn_querySMS.setOnClickListener(this);
        btn_queryContact.setOnClickListener(this);
        btn_insertSMS.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){

            case R.id.btn_querySMS:
               StringBuilder builder_sms =  queryWithSMS("content://sms/",new String[]{"address","date","body"});
                tv_info.setText(builder_sms.toString());
                break;

            // 插入数据,未成功??
            case R.id.btn_insertSMS:
                Uri uri = Uri.parse("content://sms/");
                ContentValues values = new ContentValues();
                values.put("address","10086");
                values.put("date",System.currentTimeMillis());
                values.put("body","哇哈哈哈");
                Uri insertUri = insert(uri,values);
                tv_info.setText(insertUri.toString());
                break;

            case R.id.btn_queryContact:
                queryWityContact();
                break;
        }
    }



    // 内容解析者的使用,查询
    private StringBuilder queryWithSMS(String str_uri,String [] projection ) {
        StringBuilder builder;
        // 从数据库查询短信信息
        Uri uri = Uri.parse(str_uri);
        Cursor cursor =  getContentResolver().query(uri,projection,null,null,null);
        // 判断是否为空
        if ( cursor != null && cursor.getCount() >0 ){
             builder = new StringBuilder();
            // 循环取出
            while (cursor.moveToNext()){

               String addr =  cursor.getString(0);
               String date =  cursor.getString(1);
               String body =  cursor.getString(2);

                String smsInfo = "addr"+addr+"date"+date+"body"+body+"\n";
                Log.d(TAG, "parserInfo: ----"+smsInfo);
                builder.append(smsInfo);
            }
            // 存储,显示
            return builder;
        }
        return new StringBuilder();
    }
    // 插入
    private Uri insert(Uri uri, ContentValues values) {
        Uri insertUri  = getContentResolver().insert(uri,values);
        return  insertUri;
    }

    // 联系人数据
    private void queryWityContact() {
        if(ContextCompat.checkSelfPermission(this,Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_CONTACTS},9);
        }else {
            queryContact();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode){

            case 9:
                if (grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                    queryContact();

                }else {
                    Toast.makeText(this, "没有权限!", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    private void queryContact() {
        List dataList = new ArrayList();
        Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
        Log.d(TAG, "queryWityContact: ----"+cursor);
        if (cursor != null) {
            while (cursor.moveToNext()){
                // 联系人姓名
                String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                // 联系人电话号码
                String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                dataList.add(name+"..."+number);
            }
        }
        tv_info.setText(dataList.toString());
    }

}
阅读更多 登录后自动展开
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页