This question has been asked multiple times before and not a single answer from any of them have solved the problem for me. I have tried every method I could find to unregister my listener by pressing a button, going into onPause, and onDestroy; but the listener persists even when the app is closed. What's more is that I am Toasting the size of an array that my listener is populating, even when the app is closed the Toast remains and continues to increment. I have tried using null and LISTEN_NONE and everything else I could find on the web.
// Name of this file is Second.class
public class Second extends Activity {
SignalStrengthListener signalStrengthListener;
TextView lteRsrp;
TextView lteRsrq;
TextView cellPciTextView;
TextView timerValue;
Button startButton, stopButton;
TelephonyManager tm;
List<CellInfo> cellInfoList;
String lte1, lte2;
int cellPci = 0;
long startTime = 0L;
long timeInMilliseconds = 0L;
long timeSwapBuff = 0L;
long updatedTime = 0L;
ArrayList<String> rsrpArray;
ArrayList<String> rsrqArray;
ArrayList<String> pciArray;
Handler customHandler = new Handler();
boolean flag = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);
flag = false;
rsrpArray = new ArrayList<>();
rsrqArray = new ArrayList<>();
pciArray = new ArrayList<>();
lteRsrp = (TextView) findViewById(R.id.lteRsrp);
lteRsrq = (TextView) findViewById(R.id.lteRsrq);
cellPciTextView = (TextView) findViewById(R.id.cellPciTextView);
timerValue = (TextView) findViewById(R.id.timerValue);
startButton = (Button) findViewById(R.id.startButton);
stopButton = (Button) findViewById(R.id.stopButton);
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startTime = SystemClock.uptimeMillis();
customHandler.postDelayed(updateTimerThread, 0);
//start the signal strength listener
signalStrengthListener = new SignalStrengthListener();
((TelephonyManager) getSystemService(TELEPHONY_SERVICE)).listen(signalStrengthListener, SignalStrengthListener.LISTEN_SIGNAL_STRENGTHS);
tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
try {
cellInfoList = tm.getAllCellInfo();
} catch (Exception e) {
Log.d("SignalStrength", "+++++++++++++++++++++++++++++++++++++++++ null array spot 1: " + e);
}
try {
for (CellInfo cellInfo : cellInfoList) {
if (cellInfo instanceof CellInfoLte) {
// cast to CellInfoLte and call all the CellInfoLte methods you need
// Gets the LTE PCI: (returns Physical Cell Id 0..503, Integer.MAX_VALUE if unknown)
cellPci = ((CellInfoLte) cellInfo).getCellIdentity().getPci();
}
}
} catch (Exception e) {
Log.d("SignalStrength", "++++++++++++++++++++++ null array spot 2: " + e);
}
}
});
stopButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
timeSwapBuff += timeInMilliseconds;
customHandler.removeCallbacks(updateTimerThread);
flag = true;
try{
if(signalStrengthListener != null) {
tm.listen(signalStrengthListener, SignalStrengthListener.LISTEN_NONE);
Log.d("TAG", "+++++++++++++++++++++++++++++++++++ Success!!!!!!");
}
}catch(Exception e){
e.printStackTrace();
Log.d("TAG", "+++++++++++++++++++++++++++++++++++ Fail!!!!!! with error = " + e);
}
}
});
}
private Runnable updateTimerThread = new Runnable() {
public void run() {
timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
updatedTime = timeSwapBuff + timeInMilliseconds;
int secs = (int) (updatedTime / 1000);
int mins = secs / 60;
secs = secs % 60;
int milliseconds = (int) (updatedTime % 1000);
timerValue.setText("" + mins + ":"
+ String.format("%02d", secs) + ":"
+ String.format("%03d", milliseconds));
customHandler.postDelayed(this, 0);
}
};
private class SignalStrengthListener extends PhoneStateListener {
@Override
public void onSignalStrengthsChanged(android.telephony.SignalStrength signalStrength) {
//++++++++++++++++++++++++++++++++++
((TelephonyManager) getSystemService(TELEPHONY_SERVICE)).listen(signalStrengthListener, SignalStrengthListener.LISTEN_SIGNAL_STRENGTHS);
tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String ltestr = signalStrength.toString();
String[] parts = ltestr.split(" ");
lte1 = parts[9];
lte2 = parts[10];
try {
cellInfoList = tm.getAllCellInfo();
for (CellInfo cellInfo : cellInfoList) {
if (cellInfo instanceof CellInfoLte) {
// cast to CellInfoLte and call all the CellInfoLte methods you need
// Gets the LTE PCI: (returns Physical Cell Id 0..503, Integer.MAX_VALUE if unknown)
cellPci = ((CellInfoLte) cellInfo).getCellIdentity().getPci();
}
}
} catch (Exception e) {
Log.d("SignalStrength", "+++++++++++++++++++++++++++++++ null array spot 3: " + e);
}
if (!flag) {
rsrpArray.add(lte1);
rsrqArray.add(lte2);
pciArray.add(Integer.toString(cellPci));
int size = rsrpArray.size();
Toast.makeText(Second.this, "Array size = " + size, Toast.LENGTH_LONG).show();
}
lteRsrp.setText(String.valueOf(lte1));
lteRsrq.setText(String.valueOf(lte2));
cellPciTextView.setText(String.valueOf(cellPci));
super.onSignalStrengthsChanged(signalStrength);
//++++++++++++++++++++++++++++++++++++
}
}
@Override
public void onPause() {
Log.d("onPause SigStr", "+++++++++++++++++++++++++++++++++++ onPause");
try{
if(signalStrengthListener != null){
tm.listen(signalStrengthListener, SignalStrengthListener.LISTEN_NONE);
Log.d("TAG", "+++++++++++++++++++++++++++++++++++ Success!!!!!!");
}
}catch(Exception e){
e.printStackTrace();
Log.d("TAG", "+++++++++++++++++++++++++++++++++++ Fail!!!!!! with error = " + e);
}
flag = true;
super.onPause();
}
@Override
public void onDestroy() {
Log.d("onPause SigStr", "+++++++++++++++++++++++++++++++++++ onDestroy");
try{
if(signalStrengthListener != null) {
tm.listen(signalStrengthListener, SignalStrengthListener.LISTEN_NONE);
Log.d("TAG", "+++++++++++++++++++++++++++++++++++ Success!!!!!!");
}
}catch(Exception e){
e.printStackTrace();
Log.d("TAG", "+++++++++++++++++++++++++++++++++++ Fail!!!!!! with error = " + e);
}
flag = true;
super.onDestroy();
}
}
My code is starting to get ugly and redundant as I keep rearranging things in the hope that it might unregister my listener. Following is my XML layout for Second.class. The name of the XML layout file is second_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#42abc0">
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text=""
android:textSize="16sp"
android:textColor="#000000"
android:id="@+id/lteRsrp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginStart="29dp"
android:layout_marginTop="31dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="= LTE RSRP"
android:textSize="16sp"
android:textColor="#000000"
android:id="@+id/textView2"
android:layout_alignTop="@+id/lteRsrp"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text=""
android:textColor="#000000"
android:textSize="16sp"
android:id="@+id/lteRsrq"
android:layout_below="@+id/lteRsrp"
android:layout_alignStart="@+id/lteRsrp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="= LTE RSRQ"
android:textSize="16sp"
android:textColor="#000000"
android:id="@+id/textView3"
android:layout_below="@+id/textView2"
android:layout_alignStart="@+id/textView2" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text=""
android:textSize="16sp"
android:textColor="#000000"
android:id="@+id/cellPciTextView"
android:layout_below="@+id/lteRsrq"
android:layout_alignStart="@+id/lteRsrq" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="= LTE PCI"
android:textSize="16sp"
android:textColor="#000000"
android:id="@+id/textView4"
android:layout_below="@+id/textView3"
android:layout_alignStart="@+id/textView3" />
<Button
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Start"
android:textColor="#000000"
android:textSize="22sp"
android:id="@+id/startButton"
android:layout_alignParentBottom="true"
android:layout_toEndOf="@+id/textView3"
android:layout_marginBottom="48dp"
android:background="#ffffff"
android:textAlignment="center"
android:textStyle="bold"
android:padding="4dp" />
<Button
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Stop"
android:textSize="22sp"
android:textColor="#000000"
android:id="@+id/stopButton"
android:layout_alignBottom="@+id/startButton"
android:layout_alignStart="@+id/cellPciTextView"
android:background="#ffffff"
android:textStyle="bold"
android:padding="4dp"
android:textAlignment="center" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/timerVal"
android:textSize="40sp"
android:textColor="#000000"
android:id="@+id/timerValue"
android:layout_above="@+id/startButton"
android:layout_centerHorizontal="true"
android:layout_marginBottom="55dp" />
</RelativeLayout>
And following is my AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.parksjg.its.pscrindoortesttool" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name=".First"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Second"
android:screenOrientation="portrait">
</activity>
</application>
</manifest>
Again, this question has been asked before BUT none of the posted answers worked!!! Here are a few, and note that none of the answers were up-voted or it was never marked as having worked: Android : why PhoneCallListener still alive after activity finish?
Android : how to stop listening a PhoneCallListener?
Hopefully I am overlooking something simple that someone can point out. Let me know if you need more code or would like me to post the whole Android Studio project on GitHub.
Thank you!!!
I think I figured out what was happening. I put some more Log.d() statements in the code to determine how many times the SignalStrengthListener was called. I ran the app for 3 seconds and found the SignalStrengthListener was called approximately 300 times! A lot more than I thought. Now the problem is that Toast can't keep up with how many times it was called; so when I stop or close the app the Toasts continue to appear for several minutes after. My fault was not giving it enough time for the Toasts to catch up before uninstalling the app, at the time I thought that was the only way to stop the listener.
To test this I ran the app again for 3 seconds and got 273 calls to SignalStrengthListener, and every time it is called it makes a Toast. I stopped and closed the app and the Toasts continued to appear for several minutes. However, they finally stopped at the 273 Toast.
Long story short, Toast is not suitable to be called many times a second. This is my fault for not understanding how my listener was operating. In the end, the booleans that I set and the LISTEN_NONE tag to my listener successfully unregistered the listener.