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);
}
}
});
}









share|improve this question






















  • 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















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);
}
}
});
}









share|improve this question






















  • 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













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);
}
}
});
}









share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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


















  • 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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

What visual should I use to simply compare current year value vs last year in Power BI desktop

Alexandru Averescu

Trompette piccolo