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