Permission check goes wrong

  • Replies:1
  • OpenNot stickiedUnanswered
  • Forum posts: 1

Apr 1, 2018, 1:56:12 PM via Website

Hey everyone :)

I just started writing the beginning of a program to get an IMSI number,
Upon setting the event of when pressing the button and using TelephonyServices
I get an error telling me I should give a warning before this action, and so I did choose that option, but now I got an error next to the word: "this"..
Does anybody know why?

enter code here

package com.example.yakir.myapplication;

import android.Manifest;
import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

private Button button;
TextView textView;
TelephonyManager td;

protected void onCreate(Bundle savedInstanceState) {

    button = (Button) findViewById(;
    textView = (TextView) findViewById(;
    td = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
            textView.setText("IMSI number: " + td.getSubscriberId());


enter code here

  • Forum posts: 1

Apr 11, 2018, 6:31:53 AM via Website

Change this to getApplicationContext or try to MainActivitiy.this

— modified on Apr 11, 2018, 6:32:19 AM

Recommended by NextPit