0

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.

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.

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

EDIT: I am providing logcat error message.

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) 
vmilojevic
  • 109
  • 9
  • 1
    Possible duplicate of [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – Selvin Mar 28 '19 at 16:07
  • If you add your logcat with the errors you're getting (if you don't know what that is, check the link on the first comment above), you're more likely to get answers. – Nikos Hidalgo Mar 28 '19 at 16:31
  • It wasn't an answer but comment.. It's been 3 years since android 6... He is using kotlin so... He should find the example which already contains code compatible with android 6... About 6 months... From about 6 months we know that google play store will not allow to publish the apps which are reading sms and call logs if it is not their primary function. – Selvin Mar 28 '19 at 17:01
  • And about code after his edit... Well that's how end using context before you can use it... Even if you will call inside onCreate than in Application derived class you won't be able to ask user about permissions... – Selvin Mar 28 '19 at 17:07
  • ... Which brings me to the conclusion that you should start with some android's basics like life cycle of android's application's components – Selvin Mar 28 '19 at 17:09
  • @Selvin You were making assumptions that the user is experienced or has been programming for years. They may as well be a beginner and have no clue what you're talking about. Answer or comment or say what you will, it was still condescending and assumptive, and that's what I highlighted. – Nikos Hidalgo Mar 29 '19 at 08:56

1 Answers1

4

You use READ_SMS permission - it has a protection level dangerous, so you need a runtime request.

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

STAYER
  • 451
  • 5
  • 4