0

I created 2 apps, a client and a server app. When someone using the client app writes something on the RDB this triggers some events on the phones running the server app then when the server is done performing the operation requested by the client it deletes that from the RDB.

Each app does what it has to (client writes and server deletes entries), the problem arrives after some phone innactivity, in order to complete the write and delete operation I need to turn off and on the wifi/mobile data connection.

Below there is a code snipet that shows how the server app tries to communicate with the firebase RDB to delete the entry that was written by the client, this happens everytime a phone call ends on the server app. BTW the onReceive method does get called and I don´t think this problem is related with this code implementation because I´ve tried in many different ways to fix this problem with no success.

public class CallStateReceiver extends BroadcastReceiver {

public static String prevState = TelephonyManager.EXTRA_STATE_IDLE;
private final String LOG_TAG = "CallStateReceiver";


@Override
public void onReceive(Context context, Intent intent) {


    final PendingResult pendingResult = goAsync();
    Task asyncTask = new Task(pendingResult, intent, context);
    asyncTask.execute();


}

private static class Task extends AsyncTask  {

    private final PendingResult pendingResult;
    private final Intent intent;
    String PREFS_NAME = "MyPrefsFile";
    SharedPreferences serverInfoPrefs;
    String serverName;
    AudioManager audioManager;

    private Task(PendingResult pendingResult, Intent intent, Context context) {
        this.pendingResult = pendingResult;
        this.intent = intent;
        serverInfoPrefs = context. getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);




    }

    @Override
    protected void onPostExecute(Object o) {
        super.onPostExecute(o);
        pendingResult.finish();
    }

    @Override
    protected Object doInBackground(Object[] objects) {

        Bundle bundle = intent.getExtras();
        if (bundle == null) {
            return null;
        }


        serverName = serverInfoPrefs.getString("serverName", "Servidor1");

        String state = bundle.getString(TelephonyManager.EXTRA_STATE);

        if (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_IDLE)
                && !prevState.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_IDLE)) {

            FirebaseDatabase.getInstance().getReference().child("servers").child(serverName).child("current")
                    .child("callRequest").removeValue();
            audioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL, 5, 0);
            MyAccessibilityService.onCallAlready = false;


        } 

        prevState = state;



        return null;
    }


}




}

I enabled the debug logging and this line (not sure if this is related to the issue though):

03-02 18:08:55.360 10832-10840/? W/SQLiteConnectionPool: A SQLiteConnection object for database '/data/data/com.google.android.gsf/databases/gservices.db' was leaked!  Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.

And also found this solution but I don´t know how that applies to firebase

  • It's hard to say what's going on without seeing the [minimal code that reproduces the problem on the server app](http://stackoverflow.com/help/mcve). I'd also recommend that you [enable debug logging](https://firebase.google.com/docs/reference/android/com/google/firebase/database/FirebaseDatabase.html#setLogLevel(com.google.firebase.database.Logger.Level)) on that device, to see if it shows why the client disconnects. – Frank van Puffelen Feb 26 '19 at 14:06
  • According to this https://developer.android.com/reference/android/net/wifi/WifiManager.WifiLock , the Wi-Fi radio may turn off when the user has not used the device in a while. Could this be the reason why this is happening? If so, is there any way how to turn on the wifi radio without holding it? –  Feb 26 '19 at 14:46
  • The Firebase client should keep the connection open as long as there's an active listener, but will close it (after 5m iirc) if there are no listeners. But at that point, attaching a new listener should reopen the connection. My requests in my previous comment are to get us closer to understanding what you're hitting. Without code and logs we'll just be speculating. – Frank van Puffelen Feb 26 '19 at 15:05
  • 1
    If an Android app becomes inactive, Android may suspend it in order to save the user's battery and bandwidth. – Doug Stevenson Feb 26 '19 at 16:49
  • @FrankvanPuffelen I just added some code and a bit more explanation to my question, I did not want to add it at first because I thought it was going to add too much noise to a problem I thought was generic. –  Feb 26 '19 at 19:00
  • Are you certain that being a system app excludes it from Android's rules about how apps can operate? – Doug Stevenson Feb 26 '19 at 19:31
  • I decided to set the app as part of the system thinking that it would help keep it alive in the background, I also decided to put the logic to remove the node from the RDB on a broadcast receiver but none of this was of any help. I just need this app to be able to delete the node at the moment thats requested, but it´s not working because the firebase client becomes innactive –  Feb 28 '19 at 01:16

0 Answers0