Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
  public void SendSMS(View view) {
        int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS);
            if (permissionCheck == PackageManager.PERMISSION_GRANTED) {

            } else {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS}, REQUEST_SEND_PHONE_SMS);
            }

        int permissionCheck2 = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE);
        if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
            
			// if both are true then do tshi
			
        } else {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, REQUEST_READ_PHONE_STATE);
        }
    }


@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_SEND_PHONE_SMS) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    if (requestCode == REQUEST_READ_PHONE_STATE) {
                            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                                // MyMessage();
                                Toast.makeText(this, "Both OK", Toast.LENGTH_SHORT).show();
                            } else {
                                Toast.makeText(this, "You don't have required permission to send a message", Toast.LENGTH_SHORT).show();
                                return;
                            }
                    } else {
                Toast.makeText(this, "You don't have required permission to send a message", Toast.LENGTH_SHORT).show();
                return;
                }
            }
        }
    }


What I have tried:

I need both permission to be true and then run the function
Posted
Updated 6-Aug-22 6:14am
Comments
Member 10974007 6-Aug-22 12:23pm    
<uses-permission android:name="android.permission.READ_PHONE_STATE">
<uses-permission android:name="android.permission.SEND_SMS">


both used but how do i get both ture and then run the main code
David Crow 8-Aug-22 22:45pm    
if (requestCode == REQUEST_SEND_PHONE_SMS)
{
    if (grantResults[0] == PackageManager.PERMISSION_GRANTED) 
    {
        if (requestCode == REQUEST_READ_PHONE_STATE) 
        {
...


This is obviously not going to work. requestCode cannot have two separate values simultaneously.
Member 10974007 9-Aug-22 0:10am    
i dont find codes any where on internet how to Ask- Save- Recall
David Crow 9-Aug-22 9:08am    
What if you simplified your code to something a little more manageable, like:

public void SendSMS()
{
    String[] sPermissions = { Manifest.permission.SEND_SMS,
                              Manifest.permission.READ_PHONE_STATE };

    if (! hasPermissions(sPermissions))
        androidx.core.app.ActivityCompat.requestPermissions(this, sPermissions, 1);
}

public boolean hasPermissions( String... sPermissions )
{
    for (String sPermission : sPermissions)
    {
        if (checkSelfPermission(sPermission) != PackageManager.PERMISSION_GRANTED)
            return false;
    }

    return true;
}

public void onRequestPermissionsResult( int requestCode, @androidx.annotation.NonNull String[] permissions, @androidx.annotation.NonNull int[] grantResults )
{
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == 1)
    {
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED)
            Toast.makeText(this, "Permissions OK to now send message", Toast.LENGTH_SHORT).show();
        else
            Toast.makeText(this, "You don't have required permission(s) to send a message", Toast.LENGTH_SHORT).show();
    }
}

1 solution

You need to set the permissions in your manifest. See Permissions on Android  |  Android Developers[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900