简体   繁体   中英

Androis SMS permissions

I would like to check for sms send permission each time when the user press a button. If the permission is granted the application should send the sms but if the permission is denied i would like to display again the permission dialog box in order for the user to grant the permission. My problem is the following: If the user was denied the permission my app does not display again the dialog box. I use the following code. Any help?

package com.example.lockdownsms.ui.home

import android.Manifest
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.telephony.SmsManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.core.app.ActivityCompat
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import com.example.lockdownsms.R


class HomeFragment : Fragment() {

    private lateinit var homeViewModel: HomeViewModel

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        homeViewModel =
                ViewModelProvider(this).get(HomeViewModel::class.java)
        val root = inflater.inflate(R.layout.fragment_home, container, false)
       /* val textView: TextView = root.findViewById(R.id.text_home)
        homeViewModel.text.observe(viewLifecycleOwner, Observer {
            textView.text = it
        })*/

        val sms1 = root.findViewById<Button>(R.id.button1)
        sms1.setOnClickListener {

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (getContext()?.let { it1 -> ActivityCompat.checkSelfPermission(it1, Manifest.permission.SEND_SMS)} != PackageManager.PERMISSION_GRANTED) {
                    requestPermissions(Array<String>(1) { android.Manifest.permission.SEND_SMS }, 100);
                } else {
                    val smsManager: SmsManager = SmsManager.getDefault()
                    smsManager.sendTextMessage("13033", null, "msg", null, null)
                }

            }else {
                val smsManager: SmsManager = SmsManager.getDefault()
                smsManager.sendTextMessage("13033", null, "msg", null, null)
            }
        }
        return root
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        //super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == 100) {
            if (permissions[0].equals(Manifest.permission.SEND_SMS) && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                val smsManager: SmsManager = SmsManager.getDefault()
                smsManager.sendTextMessage("13033", null, "msg", null, null)
            }

        }
    }


}

This is all you need to do in Activity.

Attention if you want to use this snippet in Fragment just do the following this.

  • Change checkSelfPermission() to ActivityCompact.checkSelfPermission()

and Also change ActivityCompat.requestPermissions() to requestPermissions()

Handling of Permission Result (Allow or Deny) is the same as Activity.


public class MainActivity extends AppCompatActivity implements View.OnClickListener {

 public static final int MY_PERMISSIONS_SEND_SMS = 1;

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnCheckSMSPermission.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.btnCheckSMSPermission:
                checkSMSPermission();
                break;
                   }
}


 void checkSMSPermission() {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS}, MY_PERMISSIONS_SEND_SMS);
        }

    }



  @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSIONS_SEND_SMS: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // permission was granted, yay! Do the
                    
                    Toast.makeText(getApplicationContext(), "SEND_SMS granted", Toast.LENGTH_SHORT).show();
                } else {
                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
                    Toast.makeText(getApplicationContext(), "SEND_SMS denied", Toast.LENGTH_SHORT).show();
                }
                return;

            }

      }


    }





The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM