Non sticky service starts itself and crashes












0















My service class code:
I am using android.app.Service



public class ServiceClass extends Service {

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "onStartCommand: ");
// some work
stopService(intent);
return Service.START_NOT_STICKY;

}

@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "onBind: ");
return null;
}

@Override
public void onDestroy() {

Log.i(TAG, "onDestroy: ");
super.onDestroy();

}
}


I start it by:



startService(new Intent(MainActivity.this,ServiceClass.class));


When I run this it executes once properly. But then it tries to start again and crashes with the following error.



 ANR in com.sample.service
PID: 7381
Reason: executing service com.sample.service/.ServiceClass
Load: 0.93 / 0.73 / 0.82
CPU usage from 59831ms to 0ms ago (2018-11-15 13:26:46.668 to 2018-11-15 13:27:46.500):
13% 1810/system_server: 8.7% user + 4.5% kernel / faults: 12123 minor


stopSelf(); gives the same error. I have no pending intents no broadcast receivers, the service is registered in the manifest. the work part is a forloop counting from 1 to 100 with the current thread sleeping Thread.sleep(100);. Please don't recommend alternatives, I have a jobscheduler doing this same work and I just wanted to learn Services.










share|improve this question

























  • What makes you believe this is happening because "it starts itself again"?

    – Tim Castelijns
    Nov 15 '18 at 9:06













  • the reason in the error says so.

    – user10655681
    Nov 15 '18 at 9:12











  • no, it says "executing service" which doesn't mean it started itself another time

    – Tim Castelijns
    Nov 15 '18 at 9:13











  • what kind of work are you doing in the onStartCommand?

    – Tim Castelijns
    Nov 15 '18 at 9:14













  • just a for loop counting from 1 to 100. I delay the thread with Thread.sleep(1000). So what does "executing service" mean?

    – user10655681
    Nov 15 '18 at 9:16


















0















My service class code:
I am using android.app.Service



public class ServiceClass extends Service {

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "onStartCommand: ");
// some work
stopService(intent);
return Service.START_NOT_STICKY;

}

@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "onBind: ");
return null;
}

@Override
public void onDestroy() {

Log.i(TAG, "onDestroy: ");
super.onDestroy();

}
}


I start it by:



startService(new Intent(MainActivity.this,ServiceClass.class));


When I run this it executes once properly. But then it tries to start again and crashes with the following error.



 ANR in com.sample.service
PID: 7381
Reason: executing service com.sample.service/.ServiceClass
Load: 0.93 / 0.73 / 0.82
CPU usage from 59831ms to 0ms ago (2018-11-15 13:26:46.668 to 2018-11-15 13:27:46.500):
13% 1810/system_server: 8.7% user + 4.5% kernel / faults: 12123 minor


stopSelf(); gives the same error. I have no pending intents no broadcast receivers, the service is registered in the manifest. the work part is a forloop counting from 1 to 100 with the current thread sleeping Thread.sleep(100);. Please don't recommend alternatives, I have a jobscheduler doing this same work and I just wanted to learn Services.










share|improve this question

























  • What makes you believe this is happening because "it starts itself again"?

    – Tim Castelijns
    Nov 15 '18 at 9:06













  • the reason in the error says so.

    – user10655681
    Nov 15 '18 at 9:12











  • no, it says "executing service" which doesn't mean it started itself another time

    – Tim Castelijns
    Nov 15 '18 at 9:13











  • what kind of work are you doing in the onStartCommand?

    – Tim Castelijns
    Nov 15 '18 at 9:14













  • just a for loop counting from 1 to 100. I delay the thread with Thread.sleep(1000). So what does "executing service" mean?

    – user10655681
    Nov 15 '18 at 9:16
















0












0








0








My service class code:
I am using android.app.Service



public class ServiceClass extends Service {

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "onStartCommand: ");
// some work
stopService(intent);
return Service.START_NOT_STICKY;

}

@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "onBind: ");
return null;
}

@Override
public void onDestroy() {

Log.i(TAG, "onDestroy: ");
super.onDestroy();

}
}


I start it by:



startService(new Intent(MainActivity.this,ServiceClass.class));


When I run this it executes once properly. But then it tries to start again and crashes with the following error.



 ANR in com.sample.service
PID: 7381
Reason: executing service com.sample.service/.ServiceClass
Load: 0.93 / 0.73 / 0.82
CPU usage from 59831ms to 0ms ago (2018-11-15 13:26:46.668 to 2018-11-15 13:27:46.500):
13% 1810/system_server: 8.7% user + 4.5% kernel / faults: 12123 minor


stopSelf(); gives the same error. I have no pending intents no broadcast receivers, the service is registered in the manifest. the work part is a forloop counting from 1 to 100 with the current thread sleeping Thread.sleep(100);. Please don't recommend alternatives, I have a jobscheduler doing this same work and I just wanted to learn Services.










share|improve this question
















My service class code:
I am using android.app.Service



public class ServiceClass extends Service {

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "onStartCommand: ");
// some work
stopService(intent);
return Service.START_NOT_STICKY;

}

@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "onBind: ");
return null;
}

@Override
public void onDestroy() {

Log.i(TAG, "onDestroy: ");
super.onDestroy();

}
}


I start it by:



startService(new Intent(MainActivity.this,ServiceClass.class));


When I run this it executes once properly. But then it tries to start again and crashes with the following error.



 ANR in com.sample.service
PID: 7381
Reason: executing service com.sample.service/.ServiceClass
Load: 0.93 / 0.73 / 0.82
CPU usage from 59831ms to 0ms ago (2018-11-15 13:26:46.668 to 2018-11-15 13:27:46.500):
13% 1810/system_server: 8.7% user + 4.5% kernel / faults: 12123 minor


stopSelf(); gives the same error. I have no pending intents no broadcast receivers, the service is registered in the manifest. the work part is a forloop counting from 1 to 100 with the current thread sleeping Thread.sleep(100);. Please don't recommend alternatives, I have a jobscheduler doing this same work and I just wanted to learn Services.







android android-service






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 9:15

























asked Nov 15 '18 at 8:55







user10655681




















  • What makes you believe this is happening because "it starts itself again"?

    – Tim Castelijns
    Nov 15 '18 at 9:06













  • the reason in the error says so.

    – user10655681
    Nov 15 '18 at 9:12











  • no, it says "executing service" which doesn't mean it started itself another time

    – Tim Castelijns
    Nov 15 '18 at 9:13











  • what kind of work are you doing in the onStartCommand?

    – Tim Castelijns
    Nov 15 '18 at 9:14













  • just a for loop counting from 1 to 100. I delay the thread with Thread.sleep(1000). So what does "executing service" mean?

    – user10655681
    Nov 15 '18 at 9:16





















  • What makes you believe this is happening because "it starts itself again"?

    – Tim Castelijns
    Nov 15 '18 at 9:06













  • the reason in the error says so.

    – user10655681
    Nov 15 '18 at 9:12











  • no, it says "executing service" which doesn't mean it started itself another time

    – Tim Castelijns
    Nov 15 '18 at 9:13











  • what kind of work are you doing in the onStartCommand?

    – Tim Castelijns
    Nov 15 '18 at 9:14













  • just a for loop counting from 1 to 100. I delay the thread with Thread.sleep(1000). So what does "executing service" mean?

    – user10655681
    Nov 15 '18 at 9:16



















What makes you believe this is happening because "it starts itself again"?

– Tim Castelijns
Nov 15 '18 at 9:06







What makes you believe this is happening because "it starts itself again"?

– Tim Castelijns
Nov 15 '18 at 9:06















the reason in the error says so.

– user10655681
Nov 15 '18 at 9:12





the reason in the error says so.

– user10655681
Nov 15 '18 at 9:12













no, it says "executing service" which doesn't mean it started itself another time

– Tim Castelijns
Nov 15 '18 at 9:13





no, it says "executing service" which doesn't mean it started itself another time

– Tim Castelijns
Nov 15 '18 at 9:13













what kind of work are you doing in the onStartCommand?

– Tim Castelijns
Nov 15 '18 at 9:14







what kind of work are you doing in the onStartCommand?

– Tim Castelijns
Nov 15 '18 at 9:14















just a for loop counting from 1 to 100. I delay the thread with Thread.sleep(1000). So what does "executing service" mean?

– user10655681
Nov 15 '18 at 9:16







just a for loop counting from 1 to 100. I delay the thread with Thread.sleep(1000). So what does "executing service" mean?

– user10655681
Nov 15 '18 at 9:16














1 Answer
1






active

oldest

votes


















0














You're getting ANR since onStartCommand is on UI thread. Just use IntentService or JobIntentService (if you don't care about exact time of launch and you want to make it works on Oreo with new limitations) for this kind of jobs.






share|improve this answer
























  • timing is important here.What if I do the work in a new Thread?

    – user10655681
    Nov 15 '18 at 9:34











  • IntentService = HandlerThread + Service, so just use IntentService. Also IntentService make some works in onHandleIntent and will make stopself after that. Just check IntentService source it's pretty simple. Don't orget that u will have problems on Oreo devices, cuz of limitations (so there are bunch of options to avoid this limitations: 1) make startForegroundService, but user will see notification that your service is running and doing some kind of job (simplest solution), 2) JobService (JonIntentService), 3) WorkManager)

    – HeyAlex
    Nov 15 '18 at 9:38













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',
autoActivateHeartbeat: false,
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%2f53315620%2fnon-sticky-service-starts-itself-and-crashes%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown
























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You're getting ANR since onStartCommand is on UI thread. Just use IntentService or JobIntentService (if you don't care about exact time of launch and you want to make it works on Oreo with new limitations) for this kind of jobs.






share|improve this answer
























  • timing is important here.What if I do the work in a new Thread?

    – user10655681
    Nov 15 '18 at 9:34











  • IntentService = HandlerThread + Service, so just use IntentService. Also IntentService make some works in onHandleIntent and will make stopself after that. Just check IntentService source it's pretty simple. Don't orget that u will have problems on Oreo devices, cuz of limitations (so there are bunch of options to avoid this limitations: 1) make startForegroundService, but user will see notification that your service is running and doing some kind of job (simplest solution), 2) JobService (JonIntentService), 3) WorkManager)

    – HeyAlex
    Nov 15 '18 at 9:38


















0














You're getting ANR since onStartCommand is on UI thread. Just use IntentService or JobIntentService (if you don't care about exact time of launch and you want to make it works on Oreo with new limitations) for this kind of jobs.






share|improve this answer
























  • timing is important here.What if I do the work in a new Thread?

    – user10655681
    Nov 15 '18 at 9:34











  • IntentService = HandlerThread + Service, so just use IntentService. Also IntentService make some works in onHandleIntent and will make stopself after that. Just check IntentService source it's pretty simple. Don't orget that u will have problems on Oreo devices, cuz of limitations (so there are bunch of options to avoid this limitations: 1) make startForegroundService, but user will see notification that your service is running and doing some kind of job (simplest solution), 2) JobService (JonIntentService), 3) WorkManager)

    – HeyAlex
    Nov 15 '18 at 9:38
















0












0








0







You're getting ANR since onStartCommand is on UI thread. Just use IntentService or JobIntentService (if you don't care about exact time of launch and you want to make it works on Oreo with new limitations) for this kind of jobs.






share|improve this answer













You're getting ANR since onStartCommand is on UI thread. Just use IntentService or JobIntentService (if you don't care about exact time of launch and you want to make it works on Oreo with new limitations) for this kind of jobs.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 9:30









HeyAlexHeyAlex

1,0181619




1,0181619













  • timing is important here.What if I do the work in a new Thread?

    – user10655681
    Nov 15 '18 at 9:34











  • IntentService = HandlerThread + Service, so just use IntentService. Also IntentService make some works in onHandleIntent and will make stopself after that. Just check IntentService source it's pretty simple. Don't orget that u will have problems on Oreo devices, cuz of limitations (so there are bunch of options to avoid this limitations: 1) make startForegroundService, but user will see notification that your service is running and doing some kind of job (simplest solution), 2) JobService (JonIntentService), 3) WorkManager)

    – HeyAlex
    Nov 15 '18 at 9:38





















  • timing is important here.What if I do the work in a new Thread?

    – user10655681
    Nov 15 '18 at 9:34











  • IntentService = HandlerThread + Service, so just use IntentService. Also IntentService make some works in onHandleIntent and will make stopself after that. Just check IntentService source it's pretty simple. Don't orget that u will have problems on Oreo devices, cuz of limitations (so there are bunch of options to avoid this limitations: 1) make startForegroundService, but user will see notification that your service is running and doing some kind of job (simplest solution), 2) JobService (JonIntentService), 3) WorkManager)

    – HeyAlex
    Nov 15 '18 at 9:38



















timing is important here.What if I do the work in a new Thread?

– user10655681
Nov 15 '18 at 9:34





timing is important here.What if I do the work in a new Thread?

– user10655681
Nov 15 '18 at 9:34













IntentService = HandlerThread + Service, so just use IntentService. Also IntentService make some works in onHandleIntent and will make stopself after that. Just check IntentService source it's pretty simple. Don't orget that u will have problems on Oreo devices, cuz of limitations (so there are bunch of options to avoid this limitations: 1) make startForegroundService, but user will see notification that your service is running and doing some kind of job (simplest solution), 2) JobService (JonIntentService), 3) WorkManager)

– HeyAlex
Nov 15 '18 at 9:38







IntentService = HandlerThread + Service, so just use IntentService. Also IntentService make some works in onHandleIntent and will make stopself after that. Just check IntentService source it's pretty simple. Don't orget that u will have problems on Oreo devices, cuz of limitations (so there are bunch of options to avoid this limitations: 1) make startForegroundService, but user will see notification that your service is running and doing some kind of job (simplest solution), 2) JobService (JonIntentService), 3) WorkManager)

– HeyAlex
Nov 15 '18 at 9:38






















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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53315620%2fnon-sticky-service-starts-itself-and-crashes%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

Bressuire

Vorschmack

Quarantine