Reputation: 51
I am trying to retrieve cell signal strength and display on my 4G based phone. My phone build is LND-AL30 8.0.0.203 (C675). Does it mean that I have android ver 8 with api level 30? If so, which Api can I use to get signal strength?
Problem: When the application is launched and when location information is DISABLED, then application runs fine but retrieves zero length of cellInfo objects (tm.getAllCellInfo). So, i am unable to get signal strength at all.
Whereas If I turn on location information on my phone, the application crashes with exception: No virtual method cellInfo.getCellSignalStrength. Can you please suggest how to overcome this problem and retrieve cell signal strength?
Below is the android sdk versions that I am using.
build.gradle
android {
compileSdk 31
defaultConfig {
applicationId "com.example.mobilenetworkinfo"
minSdk 31
targetSdk 31
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
Manifest.xml file with required permissions:
uses-permission android:name="android.permission.READ_PHONE_STATE"
uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"
uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"
uses-permission android:name="android.permission.INTERNET"
And MainActivity.java file:
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.telephony.*;
public class MainActivity extends AppCompatActivity {
private final int REQUEST_READ_PHONE_STATE=1;
private final int REQUEST_ACCESS_COARSE_LOCATION=2;
private final int REQUEST_ACCESS_FINE_LOCATION=3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("onCreate", "added hello from onCreate");
}
@SuppressLint({"NewApi", "MissingPermission"})
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public void sendMessage(View view) {
int permissionCheck = ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_PHONE_STATE);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]
{Manifest.permission.READ_PHONE_STATE}, REQUEST_READ_PHONE_STATE);
} else {
//TODO
}
int permissionCheck2 = ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION);
if (permissionCheck2 != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]
{Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_ACCESS_COARSE_LOCATION);
} else {
//TODO
}
int permissionCheck3 = ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION);
if (permissionCheck3 != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]
{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_ACCESS_FINE_LOCATION);
} else {
//TODO
}
Intent intent = new Intent(this, DisplayMessageActivity.class);
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
intent.putExtra("IMEI", tm.getImei());
intent.putExtra("SIM serial number", tm.getSimSerialNumber());
Integer i=0;
for( CellInfo cellInfo: tm.getAllCellInfo()) {
//for( i =1; i <= 6;i++){
i++;
intent.putExtra("cellSignalStrength"+i.toString(),
cellInfo.getCellSignalStrength().getDbm()+"");
}
intent.putExtra("CELLCOUNT", i+"");
Log.i("First", "added hello and count is " + i.toString());
startActivity(intent);
}
@SuppressLint({"MissingPermission", "NewApi"})
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[]
grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST_READ_PHONE_STATE:
if ((grantResults.length > 0) && (grantResults[0] ==
PackageManager.PERMISSION_GRANTED)) {
}
break;
default:
break;
}
}
}
Upvotes: 1
Views: 719
Reputation: 93559
You're compiling with SDK level 18. getSignalStrength was just added in API 30. You need to set your target SDK version to at least 30 (and run it on a device at least 30) to compile. You'd probably want to make the minSDK version 30 as well, or you'll need to check the API level before calling it.
Upvotes: 1