Android HTTP Post request in background
up vote
0
down vote
favorite
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);
}
}
});
}
android service background http-post android-background
|
show 3 more comments
up vote
0
down vote
favorite
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);
}
}
});
}
android service background http-post android-background
developer.android.com/reference/android/os/AsyncTask
– emaillenin
Nov 22 at 5:40
Will AsyncTask in myActivity continue to work when i open another app?
– user3360453
Nov 22 at 5:44
Yes, it should.
– Sub 6 Resources
Nov 22 at 5:46
or use Background services for that: guides.codepath.com/android/starting-background-services
– emaillenin
Nov 22 at 5: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 at 5:51
|
show 3 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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);
}
}
});
}
android service background http-post android-background
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);
}
}
});
}
android service background http-post android-background
android service background http-post android-background
asked Nov 22 at 5:38
user3360453
72
72
developer.android.com/reference/android/os/AsyncTask
– emaillenin
Nov 22 at 5:40
Will AsyncTask in myActivity continue to work when i open another app?
– user3360453
Nov 22 at 5:44
Yes, it should.
– Sub 6 Resources
Nov 22 at 5:46
or use Background services for that: guides.codepath.com/android/starting-background-services
– emaillenin
Nov 22 at 5: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 at 5:51
|
show 3 more comments
developer.android.com/reference/android/os/AsyncTask
– emaillenin
Nov 22 at 5:40
Will AsyncTask in myActivity continue to work when i open another app?
– user3360453
Nov 22 at 5:44
Yes, it should.
– Sub 6 Resources
Nov 22 at 5:46
or use Background services for that: guides.codepath.com/android/starting-background-services
– emaillenin
Nov 22 at 5: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 at 5:51
developer.android.com/reference/android/os/AsyncTask
– emaillenin
Nov 22 at 5:40
developer.android.com/reference/android/os/AsyncTask
– emaillenin
Nov 22 at 5:40
Will AsyncTask in myActivity continue to work when i open another app?
– user3360453
Nov 22 at 5:44
Will AsyncTask in myActivity continue to work when i open another app?
– user3360453
Nov 22 at 5:44
Yes, it should.
– Sub 6 Resources
Nov 22 at 5:46
Yes, it should.
– Sub 6 Resources
Nov 22 at 5:46
or use Background services for that: guides.codepath.com/android/starting-background-services
– emaillenin
Nov 22 at 5:47
or use Background services for that: guides.codepath.com/android/starting-background-services
– emaillenin
Nov 22 at 5: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 at 5:51
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 at 5:51
|
show 3 more comments
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424490%2fandroid-http-post-request-in-background%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
developer.android.com/reference/android/os/AsyncTask
– emaillenin
Nov 22 at 5:40
Will AsyncTask in myActivity continue to work when i open another app?
– user3360453
Nov 22 at 5:44
Yes, it should.
– Sub 6 Resources
Nov 22 at 5:46
or use Background services for that: guides.codepath.com/android/starting-background-services
– emaillenin
Nov 22 at 5: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 at 5:51