利用sqlite想要实现从本机获取短信的信息,我们就必须找到本机存储短信数据的位置
一、使用真机
打开studio -- Device-File-Explorer--data-data--com.android.providers.telephony下
这也就是provider 内容提供者
此时报了一个错误,无法看到本机数据
真机的话需要进行root操作
暂时使用google自带模拟器可以找到provider的下的sms数据库
贴一下代码:
package admin.example.com.myapplication;
import android.content.ContentResolver;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
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;
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_CODE_ASK_PERMISSIONS =123;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkPermisssion();
}
private void checkPermisssion() {
if(ContextCompat.checkSelfPermission(getBaseContext(), "android.permission.READ_SMS") != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{"android.permission.READ_SMS"}, REQUEST_CODE_ASK_PERMISSIONS);
}
}
public void onGetSmsInfoClick(View view) {
//系统的短信,联系人都是通过内容提供者组件暴露给第三方
ContentResolver cr = getApplicationContext().getContentResolver();
Cursor cursor =cr.query(Uri.parse("content://sms/"),new String[]{"address","date","body","type"},null,null,null);
while (cursor.moveToNext())
{
String address = cursor.getString(0);
String date = cursor.getString(cursor.getColumnIndex("date"));
String body = cursor.getString(2);
String type = cursor.getString(3);
Log.i("test","address:"+address+"date:"+date+"body;"+body+"type:"+type);
}
cursor.close();
}
}
需要的权限:
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<!-- 读取联系人 -->
<uses-permission android:name="android.permission.READ_CONTACTS"/>
最后得到结果内容:
5-10 01:10:36.677 12586-12586/? I/test: address:5554date:1525857482729body;have you eat something to eat?type:1
05-10 01:10:36.677 12586-12586/? I/test: address:5554date:1525857467298body;i miss youtype:1
05-10 01:10:36.678 12586-12586/? I/test: address:5554date:1525857437307body;????type:1
05-10 01:10:36.678 12586-12586/? I/test: address:5554date:1525857426452body;?????type:1
05-10 01:10:36.678 12586-12586/? I/test: address:5554date:1525857418522body;??????type:1
05-10 01:10:36.678 12586-12586/? I/test: address:15555215554date:1525854572351body;23123123rdtype:1
05-10 01:10:36.678 12586-12586/? I/test: address:5554date:1525854572053body;23123123rdtype:2
05-10 01:10:36.678 12586-12586/? I/test: address:5554date:1525854563113body;789465iiuojtype:1
05-10 01:10:36.678 12586-12586/? I/test: address:15555215554date:1525854553956body;Risk luff D'stype:1
05-10 01:10:36.678 12586-12586/? I/test: address:15555215554date:1525854553660body;Risk luff D'stype:2
05-10 01:10:36.678 12586-12586/? I/test: address:15555215554date:1525854544170body;1234567890type:1
05-10 01:10:36.678 12586-12586/? I/test: address:5554date:1525854543863body;1234567890type:2
05-10 01:10:36.678 12586-12586/? I/test: address:5554date:1525854529608body;erwer23432type:1
05-10 01:10:36.678 12586-12586/? I/test: address:5554date:1525854528245body;erwer23432type:1
05-10 01:10:36.678 12586-12586/? I/test: address:5554date:1525854508941body;erwer23432type:1
05-10 01:10:36.678 12586-12586/? I/test: address:15555215554date:1525854498163body;234rwerwertype:1
05-10 01:10:36.678 12586-12586/? I/test: address:5554date:1525854497848body;234rwerwertype:2
05-10 01:10:36.678 12586-12586/? I/test: address:5554date:1525854476937body;werwe1223123123rtype:1
05-10 01:10:36.678 12586-12586/? I/test: address:5554date:1525854468773body;werwerwerwerwertype:1
05-10 01:10:36.678 12586-12586/? I/test: address:5554date:1525854440648body;??5554?????type:1
05-10 01:10:36.678 12586-12586/? I/test: address:5554date:1525854437449body;??5554?????type: