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.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
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;


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

    button = (Button) findViewById(R.id.checkConnectionButton);
    textView = (TextView) findViewById(R.id.chechConnectionText);
    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.
                return;
            }
            textView.setText("IMSI number: " + td.getSubscriberId());
        }
    });
}

}

enter code here

Reply
  • 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

Helpful?
Reply