0

I have a method that detects call state and sends it to DB, but only when activity in foreground. How can i continue method to work when app is in backgound? How and what should i change so it will run when app goes to background?

Here i call method in onCreate() of MainActivity:

callStateListener = new PhoneStateListener() {
            public void onCallStateChanged(final int state, String incomingNumber) {

                switch (state){
                    case TelephonyManager.CALL_STATE_IDLE:
                        Log.i(TAG, "onCallStateChanged: IDLE = "+state);
                        if (token!=null) {
                            updateState(token, String.valueOf(state));
                        }else {
                            Log.i(TAG, "onCallStateChanged: ERROR");
                        }
                        break;

                    case TelephonyManager.CALL_STATE_RINGING:
                        Log.i(TAG, "onCallStateChanged: RING = "+state);
                        if (token!=null) {
                            updateState(token, String.valueOf(state));
                        }
                        break;
                    case TelephonyManager.CALL_STATE_OFFHOOK:
                        Log.i(TAG, "onCallStateChanged: CALL = "+state);
                        if (token!=null) {
                            updateState(token, String.valueOf(state));
                        }
                        break;
                }
            }
        };

This is method:

private void updateState(String token, String state) {
        Log.i(TAG, "updateState: state"+state);
        OkHttpClient client = new OkHttpClient();
        RequestBody body = new FormBody.Builder()
                .add("token", token)
                .add("state", state)
                .build();

        Request request = new Request.Builder()
                .url("http://update.php")
                .post(body)
                .build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d("OkHttp", call.toString());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    String responseStr = response.body().string();
                    Log.d("OkHttp", responseStr);
                }
            }
        });
    }
user3360453
  • 27
  • 1
  • 3
  • https://developer.android.com/reference/android/os/AsyncTask – Raj Nov 22 '18 at 05:40
  • Will AsyncTask in myActivity continue to work when i open another app? – user3360453 Nov 22 '18 at 05:44
  • Yes, it should. – Sub 6 Resources Nov 22 '18 at 05:46
  • or use Background services for that: https://guides.codepath.com/android/starting-background-services – Raj Nov 22 '18 at 05:47
  • Please user background service in that case , check how to start and stop service, run it continuously ,when phone restarts restart the service again,how to keep it running on app killed for MI,Huawei and other Custom OS Devices etc, These things you need to keep in mind to run your listener in background – Quick learner Nov 22 '18 at 05:51
  • Use JobIntentService https://developer.android.com/reference/android/support/v4/app/JobIntentService – Rabindra Khadka Nov 22 '18 at 05:53
  • @RabindraKhadka it wont work continuously like very second/minute , its minimum repetition time is 15 minutes – Quick learner Nov 22 '18 at 06:22
  • https://stackoverflow.com/questions/52381137/i-want-to-show-gif-image-show-on-screen-even-when-app-is-closed/52381383#52381383 , go through this answer and do some edit like declaring listener and calling api – Quick learner Nov 22 '18 at 06:23

0 Answers0