Question

I'm new to Android programming and while I have successfully logged the output of a listener for a state change in the Telephony API, I'm having trouble accessing the value and writing it to a database. I can write to the log from within the inner class but can't save to DB as I can't access the helper from within the inner class. I'm also having trouble calling getSignalStrengths() method. these are all learning exercises and any advice would be much appreciated.

public class MyPhoneStateListener extends PhoneStateListener {

String sSstr = "initial value";

public String getSignalStrengths(){
 return sSstr;
}

public void onSignalStrengthsChanged(SignalStrength signalStrength) {
 super.onSignalStrengthsChanged(signalStrength);
 sSstr = signalStrength.toString();
 Log.v("Test", sSstr);
 }
}
Was it helpful?

Solution

You can access your database inside the MyPhoneStateListener in a couple of way

  1. Create a MyPhoneStateListener constructor with a Context parameter and use this context to instantiate your database class.
  2. Just instantiate your database class using

    MyDatabase db = new MyDatabase(MainActivity.this)
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top