[英]How to access SMS and phone calls storage on Android?
I am trying to create an app that reads sms messages and phone calls data so it could provide some information like how much sms/calls did I make in last 24 hours or who is the person that I am texting/talking to the most. 我正在尝试创建一个读取短信和电话数据的应用程序,以便它可以提供一些信息,例如我在过去24小时内发了多少短信/电话,或者是我发短信/交谈最多的人。
So far I tried to read sms messages using code below but my app keeps crashing. 到目前为止,我尝试使用下面的代码读取短信,但我的应用始终崩溃。
package com.example.mobilestats
import android.net.Uri
import android.app.Application
class SmsData : Application() {
private var _smsBodyList = getSMS()
fun getTotalSmsCount(): Int {
return _smsBodyList.size
}
fun getSMS(): ArrayList<String> {
var sms = ArrayList<String>()
var smsURI = Uri.parse("content://sms/inbox")
var cur = applicationContext.contentResolver.query(smsURI, null, null, null, null)
while(cur != null && cur.moveToNext()) {
var body = cur.getString(cur.getColumnIndex("body"))
sms.add(body)
}
if(cur != null) {
cur.close()
}
return sms
}
}
I think the problem is when I try to make query but I don't get any error report, the application just crashes. 我认为问题是当我尝试进行查询但没有收到任何错误报告时,应用程序崩溃了。
I enabled READ_SMS permission in manifest file. 我在清单文件中启用了READ_SMS权限。
<uses-permission android:name="android.permission.READ_SMS"/>
EDIT: I am providing logcat error message. 编辑:我正在提供logcat错误消息。
03-28 17:53:21.709 30686-30686/com.example.mobilestats E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mobilestats, PID: 30686
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mobilestats/com.example.mobilestats.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2572)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2654)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1488)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5728)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:107)
at com.example.mobilestats.SmsData.getSMS(SmsData.kt:17)
at com.example.mobilestats.SmsData.<init>(SmsData.kt:7)
at com.example.mobilestats.MainActivity.onCreate(MainActivity.kt:17)
at android.app.Activity.performCreate(Activity.java:6309)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2519)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2654)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1488)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5728)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
You use READ_SMS permission - it has a protection level dangerous , so you need a runtime request. 您使用READ_SMS权限-它的保护级别为危险 ,因此需要运行时请求。
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_SMS)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.READ_SMS), 0)
} else {
// Permission already granted
}
For more info you may visit android docs 有关更多信息,您可以访问android docs
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.