android获取权限

1.在Manifest文件中添加权限:

<uses-permission android:name="android.permission.READ_SMS"/>

2.动态申请权限

package com.example.contentdemo;

import android.Manifest;
import android.content.ContentResolver;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

public class MainActivity extends AppCompatActivity {

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

    private void getMsgs(){

        Uri uri = Uri.parse("content://sms/");
        ContentResolver resolver = getContentResolver();
        //获取的是哪些列的信息
        if(ContextCompat.checkSelfPermission(getBaseContext(), "android.permission.READ_SMS") != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{"android.permission.READ_SMS"}, 1);   //第一种方式
        }else {
            Cursor cursor = resolver.query(uri, new String[]{"address","date","type","body"}, null, null, null);
            while(cursor.moveToNext())
            {
                String address = cursor.getString(0);
                String date = cursor.getString(1);
                String type = cursor.getString(2);
                String body = cursor.getString(3);
                System.out.println("地址:" + address);
                System.out.println("时间:" + date);
                System.out.println("类型:" + type);
                System.out.println("内容:" + body);
                System.out.println("======================");
            }
            cursor.close();
        }
    }
    private void getContacts(){
        //①查询raw_contacts表获得联系人的id
        ContentResolver resolver = getContentResolver();
        Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(MainActivity.this,
                    new String[]{Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    1);//第二种方式
        }else {
            //查询联系人数据
            Cursor cursor = resolver.query(uri, null, null, null, null);
            while(cursor.moveToNext())
            {
                //获取联系人姓名,手机号码
                String cName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                String cNum = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                System.out.println("姓名:" + cName);
                System.out.println("号码:" + cNum);
                System.out.println("======================");
            }
            cursor.close();
        }

    }
}

参考网站:
https://www.e-learn.cn/content/wangluowenzhang/172750
https://blog.csdn.net/wodexiongdizdp/article/details/74928573
https://www.runoob.com/w3cnote/android-tutorial-contentprovider.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值