-3

I had the same error as this question. So I created his doCheckPermission() in my activity class. Now, there is an issue. The activity class says :

java.lang.SecurityException: getCellLocation: Neither user 10074 nor current process has android.permission.ACCESS_COARSE_LOCATION.

doPermisionCheck()

private void doPermissionCheck() {
        if (ContextCompat.checkSelfPermission(getApplicationContext(),
                Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED ) {
            ActivityCompat.requestPermissions(TelephonyStatusActivity.this,
                    new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                    MY_PERMISSION_ACCESS_COARSE_LOCATION); //This line is the error
        }
}

My manifest is :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.user.myapp>

    <uses-feature
        android:name="android.hardware.telephony"
        android:required="true" />

    <!-- Permission needed to read TelephoneManager data-->
        <uses-permission android:name="android.permission.READ_PHONE_STATE" />
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <!-- END -->

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".TelephonyStatusActivity"></activity>
    </application>

</manifest>            

What did I do wrong ?

UPDATE

Below is my activity java code

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.CellLocation;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.telephony.cdma.CdmaCellLocation;
import android.telephony.gsm.GsmCellLocation;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class TelephonyStatusActivity extends AppCompatActivity {

    Button btnPhoneStatus, btnEffacer;
    TextView txtPhoneStatus;




    //int MY_PERMISSION_ACCESS_COARSE_LOCATION = Integer.parseInt(Manifest.permission.ACCESS_COARSE_LOCATION);
    int MY_PERMISSION_ACCESS_COARSE_LOCATION = 1;

    @Override
    public void onStart() {
       super.onStart();

        final TelephonyManager telMgr = (TelephonyManager)
                getSystemService(Context.TELEPHONY_SERVICE);

        PhoneStateListener phoneStateListener = new PhoneStateListener(){

            public void onCallStateChanged(int state,
                                             String incomingNumber){
                txtPhoneStatus.setText(getTelephonyOverview(telMgr));
            }
        };

        telMgr.listen(phoneStateListener,
                PhoneStateListener.LISTEN_CALL_STATE);
        txtPhoneStatus.setText(getTelephonyOverview(telMgr));
    }

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

        final TelephonyManager telMgr = (TelephonyManager)
                getSystemService(Context.TELEPHONY_SERVICE);

        btnPhoneStatus = (Button) findViewById(R.id.btnPhoneStatus);
        btnEffacer = (Button) findViewById(R.id.btnEffacer);
        txtPhoneStatus = (TextView) findViewById(R.id.txtPhoneStatus);

        btnPhoneStatus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txtPhoneStatus.setText(getTelephonyOverview(telMgr));
            }
        });

        btnEffacer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txtPhoneStatus.setText("");
            }
        });
    }

    public String getTelephonyOverview(TelephonyManager telMgr){

        String strCallState = "NA";
        int IntCallState = telMgr.getCallState();

        switch(IntCallState){
            case TelephonyManager.CALL_STATE_IDLE :
                strCallState = "Aucune Activité";
                break;

            case TelephonyManager.CALL_STATE_OFFHOOK :
                strCallState = "Décroché";
                break;

            case TelephonyManager.CALL_STATE_RINGING :
                strCallState = "Sonne";
                break;
        }

        CellLocation cellLocation = (CellLocation) telMgr.getCellLocation();
        String strCellLocation = null;
        //Location Area Code (getLac) and Cell Id (getCid)
        if (cellLocation instanceof GsmCellLocation)
        {
            strCellLocation = ((GsmCellLocation)cellLocation).getLac()
                    + " " + ((GsmCellLocation)cellLocation).getCid();
        }
        else if (cellLocation instanceof CdmaCellLocation)
        {
            strCellLocation = ((CdmaCellLocation)cellLocation).
                    getBaseStationLatitude() + " " +
                    ((CdmaCellLocation)cellLocation).getBaseStationLongitude();
        }

        String deviceId = telMgr.getDeviceId();
        String deviceSoftwareVersion =
                telMgr.getDeviceSoftwareVersion();
        String line1Number = telMgr.getLine1Number();
        String networkCountryIso = telMgr.getNetworkCountryIso();
        String networkOperator = telMgr.getNetworkOperator();
        String networkOperatorName = telMgr.getNetworkOperatorName();

        String strPhoneType = "NA";
        int intPhoneType = telMgr.getPhoneType();

        switch (intPhoneType){

            case TelephonyManager.PHONE_TYPE_NONE :
                strPhoneType = "Aucun";
                break;

            case TelephonyManager.PHONE_TYPE_GSM :
                strPhoneType = "GSM";
                break;

            case TelephonyManager.PHONE_TYPE_CDMA :
                strPhoneType = "CDMA";
                break;

        }

        String simCountryIso = telMgr.getSimCountryIso();
        String simOperator = telMgr.getSimOperator();
        String simOperatorName = telMgr.getSimOperatorName();
        String simSerialNumber = telMgr.getSimSerialNumber();
        String simSubscriberId = telMgr.getSubscriberId();

        String strSimState = "NA";
        int intSimState = telMgr.getSimState();

        switch (intSimState){

            case TelephonyManager.SIM_STATE_ABSENT :
                strSimState = "SIM Absente";
                break;

            case TelephonyManager.SIM_STATE_READY :
                strSimState = "SIM prête";
                break;

            case TelephonyManager.SIM_STATE_UNKNOWN :
                strSimState = "SIM non définie";
                break;

            case TelephonyManager.SIM_STATE_NETWORK_LOCKED :
                strSimState = "SIM verouillée";
                break;

            case  TelephonyManager.SIM_STATE_PIN_REQUIRED :
                strSimState = "SIM PIN non défini";
                break;

            case  TelephonyManager.SIM_STATE_PUK_REQUIRED :
                strSimState = "SIM PUK non défini";
                break;
        }

        StringBuilder sb = new StringBuilder();
        sb.append("telMgr - ");
        sb.append(" \nEtat du télephone = " + strCallState);
        sb.append(" \nLocalisation cellulaire = " + strCellLocation);
        sb.append(" \nType de tel = " + strPhoneType);
        sb.append(" \nId équipement = " + deviceId);
        sb.append(" \nVersion OS = " + deviceSoftwareVersion);
        sb.append(" \nNumero Tél 1 = " + line1Number);
        sb.append(" \nISO Pays de l'opérateur = " + networkCountryIso);
        sb.append(" \nOpérateur = " + networkOperator);
        sb.append(" \nNom opérateur = " + networkOperatorName);
        sb.append(" \nSIM Pays ISO = " + simCountryIso);
        sb.append(" \nSIM Opérateur = " + simOperator);
        sb.append(" \nSIM Nom Opérateur = " + simOperatorName);
        sb.append(" \nSIM ICCID = " + simSerialNumber);
        sb.append(" \nSIM IMSI = " + simSubscriberId);
        sb.append(" \nEtat SIM = " + strSimState);

        return sb.toString();
    }

    private void doPermissionCheck() {
        if (ContextCompat.checkSelfPermission(getApplicationContext(),
                Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED ) {
            ActivityCompat.requestPermissions(TelephonyStatusActivity.this,
                    new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                    MY_PERMISSION_ACCESS_COARSE_LOCATION);
        }
    }
}

I still get the same error as before

Bloomberg58
  • 157
  • 1
  • 19

3 Answers3

1

You need to define MY_PERMISSION_ACCESS_COARSE_LOCATION before using it

public class MainActivity extends Activity{

   int MY_PERMISSION_ACCESS_COARSE_LOCATION = 1;



  private void doPermissionCheck() {
    // This checks whether your app is granted permission to access coarse location of the user.
    if (ContextCompat.checkSelfPermission(getApplicationContext(),
            Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED ) {
        // if permission is not already granted to your app for location access then call requestPermissions to request that permission. 
        //This opens up a dialog before user to ask for permission. 
        //The integer MY_PERMISSION_ACCESS_COARSE_LOCATION that is passed acts like a unique identifier for this request and is used to 
        //track whether user has granted the permission or denied. 
        //See the below callback function onRequestPermissionsResult which is called after user had taken some action on that permission dialog
        ActivityCompat.requestPermissions(TelephonyStatusActivity.this,
                new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                MY_PERMISSION_ACCESS_COARSE_LOCATION); 
    }
  }

  //This method is called after user has taken some action over the permission dialog
  @Override
  public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        // Here you can access the result for your requested permission
        // Now you use the integer MY_PERMISSION_ACCESS_COARSE_LOCATION which acts as a unique identifier for your permission
        case MY_PERMISSION_ACCESS_COARSE_LOCATION: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                //Permission is granted by the user on permission dialog
            } else {
                //Permission is not granted by the user on permission dialog
            }
        }
        case : SOME_OTHER_PERMISSION_UNIQUE_ID : {
             //This corresponds to some other permission request
        }
    }
 }

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

    // you need to call the method here
    doPermissionCheck();

    final TelephonyManager telMgr = (TelephonyManager)
            getSystemService(Context.TELEPHONY_SERVICE);

    btnPhoneStatus = (Button) findViewById(R.id.btnPhoneStatus);
    btnEffacer = (Button) findViewById(R.id.btnEffacer);
    txtPhoneStatus = (TextView) findViewById(R.id.txtPhoneStatus);

    btnPhoneStatus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            txtPhoneStatus.setText(getTelephonyOverview(telMgr));
        }
    });

    btnEffacer.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            txtPhoneStatus.setText("");
        }
    });
   }



  }
Shriyansh Gautam
  • 1,084
  • 1
  • 7
  • 13
  • where do I define it ? – Bloomberg58 Sep 10 '17 at 20:21
  • see the answer. You should define it just below the class definition and before and method. – Shriyansh Gautam Sep 10 '17 at 20:26
  • What is the int value I should provide for this ? `android.Manifest.permission.ACCESS_COARSE_LOCATION` is `String` if I parse it for `MY_PERMISSION_ACCESS_COARSE_LOCATION` will it solve my issue ? – Bloomberg58 Sep 16 '17 at 18:16
  • I got `java.lang.NumberFormatException: For input string: "android.permission.ACCESS_COARSE_LOCATION"` for trying `int MY_PERMISSION_ACCESS_COARSE_LOCATION = Integer.parseInt(Manifest.permission.ACCESS_COARSE_LOCATION);` – Bloomberg58 Sep 16 '17 at 18:27
  • @Yvette Colomb Hello. I know you know android. Would you kindly help me sove this issue ? – Bloomberg58 Sep 16 '17 at 18:30
  • just define any random integer like int MY_PERMISSION_ACCESS_COARSE_LOCATION = 1; – Shriyansh Gautam Sep 18 '17 at 08:02
  • Did not help me. I set it to `1` I still get the same error. Please se my update... – Bloomberg58 Sep 20 '17 at 21:00
  • you are not calling the method doPermissionCheck(). You need to call in it in your onCreate(). See the updated answer – Shriyansh Gautam Sep 21 '17 at 06:51
  • Awesome. It solved it.... lol I gave you a bounty for that hahaha... PLease at least kindly explain me the meaning of the `doCheckPermission()` code and why we use random int and not a specific values... – Bloomberg58 Sep 21 '17 at 19:44
  • 1
    See the updates answer. The integer is your choice. It acts as a unique identifier to your permission request. In case you are asking for more than one permission you can use unique ids to identify them and know which of the permissions have been granted and which have been denied. `onRequestPermissionsResult` callback is triggered when user has taken some action over the permission dialog. In this method you can check for each of your requested permission and check the results for each. – Shriyansh Gautam Sep 22 '17 at 09:34
  • Thank you sir. You've helped me a lot – Bloomberg58 Sep 22 '17 at 19:03
  • Thanks @Bloomberg58 and if this answer resolved your problem, you can award me the bounty if you don't mind. – Shriyansh Gautam Sep 22 '17 at 19:29
0

You have to define MY_PERMISSION_ACCESS_COARSE_LOCATION by yourself. It's a field you have to specify.

Roberto Betancourt
  • 2,375
  • 3
  • 27
  • 35
0

Define the permission by yourself.

public static final int REQUEST_COARSE_LOCATION = 2; 

    ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                   MY_PERMISSION_ACCESS_COARSE_LOCATION );
Suraksha Ajith
  • 872
  • 2
  • 12
  • 23