Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Hi i've got a kind of a dumb problem. Im trying to display a notification from a service. When an activity starts i call the startService like so:

      Intent myIntent = new Intent(getApplicationContext(),notif_service.class);
      startService(myIntent); 

the service calculates something and should display the notification and then stop. the code is as follows:

    if (limit_time_value == 2 && start >= 6300000 && notif_past)
     {  

        notif_past=false;
        showNotification();
        stopSelf(); 

     }

There are two ways that this service can be stopped, ether from itself with stopSelf() or from a button in my activity with

           Intent myIntent = new Intent(getApplicationContext(),notif_service.class);
      stopService(myIntent);

the problem is that even when i stop the service the notification is shown after the specified time passes. I tried to stop the setvice with Binding it and than calling onDestroy() in which I cancel the notification and again call stopSelf(). Again the notification is shown.

What am I doing wrong? Do I misunderstand how notifications or services work?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
449 views
Welcome To Ask or Share your Answers For Others

1 Answer

You do not indicate precisely where you are performing the work shown in your second code snippet above.

  • If that work is being done in onStart() or onStartCommand(), that work is being performed on the main application thread, and therefore once it starts it blocks all other main application thread work, such as stopService() and onDestroy().

  • If that work is being done on a background thread you create, unless you are terminating that background thread, that thread will continue to completion, regardless of whether the service is destroyed. You will need to arrange to terminate the thread yourself.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...