|
1 | 1 | package com.httpsms |
2 | 2 |
|
3 | | -import androidx.appcompat.app.AppCompatActivity |
| 3 | +import android.Manifest |
| 4 | +import android.annotation.SuppressLint |
| 5 | +import android.content.Context |
| 6 | +import android.content.pm.PackageManager |
4 | 7 | import android.os.Bundle |
| 8 | +import android.telephony.PhoneNumberUtils |
| 9 | +import android.telephony.TelephonyManager |
| 10 | +import android.util.Log |
| 11 | +import android.view.Menu |
| 12 | +import android.view.MenuItem |
| 13 | +import android.widget.TextView |
| 14 | +import android.widget.Toast |
| 15 | +import androidx.activity.result.contract.ActivityResultContracts |
| 16 | +import androidx.appcompat.app.AppCompatActivity |
| 17 | +import androidx.core.app.ActivityCompat |
| 18 | +import java.util.* |
| 19 | + |
5 | 20 |
|
6 | 21 | class MainActivity : AppCompatActivity() { |
| 22 | + private val logTag = MainActivity::class.simpleName ?: "MainActivity" |
| 23 | + |
7 | 24 | override fun onCreate(savedInstanceState: Bundle?) { |
8 | 25 | super.onCreate(savedInstanceState) |
9 | 26 | setContentView(R.layout.activity_main) |
| 27 | + |
| 28 | + val phoneNumber = getPhoneNumber(this) |
| 29 | + |
| 30 | + val titleText = findViewById<TextView>(R.id.cardPhoneNumber) |
| 31 | + titleText.text = PhoneNumberUtils.formatNumber(phoneNumber, Locale.getDefault().country) |
| 32 | + } |
| 33 | + |
| 34 | + @SuppressLint("HardwareIds") |
| 35 | + private fun getPhoneNumber(context: Context): String { |
| 36 | + // Register the permissions callback, which handles the user's response to the |
| 37 | + // system permissions dialog. Save the return value, an instance of |
| 38 | + // ActivityResultLauncher. You can use either a val, as shown in this snippet, |
| 39 | + // or a late init var in your onAttach() or onCreate() method. |
| 40 | + val requestPermissionLauncher = |
| 41 | + registerForActivityResult( |
| 42 | + ActivityResultContracts.RequestPermission() |
| 43 | + ) { isGranted: Boolean -> |
| 44 | + if (isGranted) { |
| 45 | + val toast = Toast.makeText(context, "Granted", Toast.LENGTH_SHORT) |
| 46 | + toast.show() |
| 47 | + } else { |
| 48 | + val toast = Toast.makeText(context, "NOT Granted", Toast.LENGTH_LONG) |
| 49 | + toast.show() |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + val telephonyManager = this.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager |
| 54 | + if (ActivityCompat.checkSelfPermission( |
| 55 | + this, |
| 56 | + Manifest.permission.READ_SMS |
| 57 | + ) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission( |
| 58 | + this, |
| 59 | + Manifest.permission.READ_PHONE_NUMBERS |
| 60 | + ) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission( |
| 61 | + this, |
| 62 | + Manifest.permission.READ_PHONE_STATE |
| 63 | + ) != PackageManager.PERMISSION_GRANTED |
| 64 | + ) { |
| 65 | + // You can directly ask for the permission. |
| 66 | + // The registered ActivityResultCallback gets the result of this request. |
| 67 | + requestPermissionLauncher.launch(Manifest.permission.READ_SMS) |
| 68 | + requestPermissionLauncher.launch(Manifest.permission.READ_PHONE_NUMBERS) |
| 69 | + requestPermissionLauncher.launch(Manifest.permission.READ_PHONE_STATE) |
| 70 | + } |
| 71 | + |
| 72 | + val phoneNumber = telephonyManager.line1Number ?: "NO_PHONE_NUMBER" |
| 73 | + Log.d(logTag, phoneNumber) |
| 74 | + return phoneNumber |
10 | 75 | } |
11 | 76 | } |
0 commit comments