Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
463 views
in Technique[技术] by (71.8m points)

binding - Display status bar notification from service in android

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...