legendary child
legendary child

Reputation: 39

Get activity context in click listener callback

 public class MainActivity extends AppCompatActivity {
final int SEND_SMS_PERMISSION_REQUEST_CODE =1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button= (Button) findViewById(R.id.btnSend1);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if(checkPermission(Manifest.permission.SEND_SMS)){
                    SmsManager smqr = SmsManager.getDefault();
                    smqr.sendTextMessage("000000000", null, "Message", null, null);
                }
                else
                {
                    ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.SEND_SMS},SEND_SMS_PERMISSION_REQUEST_CODE);
                }
            }

            private boolean checkPermission(String permission) {
         int check = ContextCompat.checkSelfPermission(this,permission);
         return (check == PackageManager.PERMISSION_GRANTED);

            }
        });


    }
}

i have wrote this code to make a simple app to send msgs to phone numbers , in the android studio it puts red lines under the "this" i am not sure why thanks in advance

Upvotes: 1

Views: 600

Answers (2)

Bogdan Android
Bogdan Android

Reputation: 1395

You need a context, this inside the Interface refers to the interface itself and not to the activitycontext.

Use bellow code:

public class MainActivity extends AppCompatActivity {
private Context context;
final int SEND_SMS_PERMISSION_REQUEST_CODE =1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        setContentView(R.layout.activity_main);
        Button button= (Button) findViewById(R.id.btnSend1);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if(checkPermission(Manifest.permission.SEND_SMS)){
                    SmsManager smqr = SmsManager.getDefault();
                    smqr.sendTextMessage("000000000", null, "Message", null, null);
                }
                else
                {
                    ActivityCompat.requestPermissions(context,new String[]{Manifest.permission.SEND_SMS},SEND_SMS_PERMISSION_REQUEST_CODE);
                }
            }

            private boolean checkPermission(String permission) {
         int check = ContextCompat.checkSelfPermission(this,permission);
         return (check == PackageManager.PERMISSION_GRANTED);

            }
        });


    }
}

Upvotes: 2

Nicola Gallazzi
Nicola Gallazzi

Reputation: 8713

As you can see from documentation, checkSelfPermission requires a context as first parameter, in your code you are passing an instance of View.OnClickListener, which obviously is not a context.

this should be your activity, not the click listener.

Refactor your code this way:

private boolean checkPermission(String permission) {
  int check = ContextCompat.checkSelfPermission(MainActivity.this, permission);
  return (check == PackageManager.PERMISSION_GRANTED);
}

Upvotes: 3

Related Questions