7

Recently I started studying programming for the android system in the kotlin programming language. When writing a simple application for sending SMS from the application itself, I ran into the problem that SmsManager.getDefault() is now DEPRECATION and, accordingly, it is not possible to send SMS as indicated in the video lessons. Question - how is it now possible to send SMS from the app itself? I read the official documentation, but could not understand the solution method.

package com.example.mysendsms

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.telephony.SmsManager
import com.example.mysendsms.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
    lateinit var binding: ActivityMainBinding
    val sms = SmsManager.getDefault()

    override fun onCreate(savedInstanceState: Bundle?) {
        binding = ActivityMainBinding.inflate(layoutInflater)
        super.onCreate(savedInstanceState)
        setContentView(binding.root)

        binding.send.setOnClickListener {
            val textMsg = binding.message.toString().trim()
            val numberMsg = binding.number.toString().trim()
            sendSMS(textMsg,numberMsg)
        }
    }

    private fun sendSMS(text: String, number: String) {
            sms.sendTextMessage(number,null,text,null,null)
        }
    }
}
gidds
  • 16,558
  • 2
  • 19
  • 26
  • That method is only deprecated as of API level 31. Also, "deprecated" does not mean "non-functional". What is the problem, exactly? That is, what specifically isn't working? – Mike M. Dec 08 '21 at 14:46
  • @MikeM. I think he ask for the new way to do it, the no-deprecated way to prevent error – Elikill58 Dec 14 '21 at 09:43
  • @Elikill58 Deprecation is not an error. The OP doesn't even mention an "error". That's why they need to clarify. Are they just asking how to fix the deprecation warning? Or have they actually tried running this thing? 'cause to me, it sounds like they just stopped to post this question when they saw the deprecation warning, assuming it to be an error. – Mike M. Dec 14 '21 at 13:42
  • Any answer on this? – Dafne Defant Jan 26 '22 at 13:28

3 Answers3

7

SmsManager getDefault() method is only deprecated starting from API 31, so you should still use getDefault() APIs levels below the 31.

val smsManager: SmsManager = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            context.getSystemService(SmsManager::class.java)
        } else {
            SmsManager.getDefault()
        }
user158
  • 12,852
  • 7
  • 62
  • 94
4

By official documentation you can get it like this

val smsManager = context.getSystemService(SmsManager::class.java)
Ninjaval
  • 113
  • 1
  • 5
0

acording this

...kotlin

val smsManager:SmsManager=
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M{
applicationContext.getSystemService(SmsManager::class.java)
} 
else {
SmsManager.getDefault()
}
...
  • 1
    Answer needs supporting information Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](https://stackoverflow.com/help/how-to-answer). – moken Jul 21 '23 at 11:43