SMSmanager.getDefault() - DEPRECATION - what to replace?
Asked Answered
A

3

10

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)
        }
    }
}
Antebi answered 8/12, 2021 at 13:25 Comment(4)
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?Rai
@MikeM. I think he ask for the new way to do it, the no-deprecated way to prevent errorRoveover
@Roveover 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.Rai
Any answer on this?Allhallows
L
10

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()
        }
Ludivinaludlew answered 2/3, 2022 at 12:40 Comment(1)
Should be in android-x : issuetracker.google.com/issues/242889550Jemadar
M
6

By official documentation you can get it like this

val smsManager = context.getSystemService(SmsManager::class.java)
Misreckon answered 15/2, 2022 at 9:22 Comment(0)
G
0

The @user158 answer is perfect, but it has a typo. As mentioned getDefault() is deprecated starting from API 31, so the if condition should be like this:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S)

Note: context.getSystemService(SmsManager::class.java) returns null for API levels below 31.

Gimcrackery answered 4/12, 2023 at 9:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.