在Android的服务显示状态栏通知 [英] Display status bar notification from service in android

查看:308
本文介绍了在Android的服务显示状态栏通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有个不会说话的问题。即时试图显示从服务的通知。当活动开始叫我像这样startService:

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

业务计算的东西,应该显示通知,然后停止。在code是如下:

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

     }

有)两种方式,这种服务可以停止,从自身醚与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);

问题是,即使当我停止服务指定的时间经过后,显示该通知。我试着用它绑定和比调用的onDestroy()中,我取消的通知,并再次呼吁stopSelf()停止setvice。再次通知所示。

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?

推荐答案

您不表明precisely你在哪里表演在你的第二个code以上片段所示的工作。

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


  • 如果这项工作在在onStart正在做的() onStartCommand(),工作是主应用程序线程正在执行的,因此一旦开始就禁止所有其他主应用程序线程的工作,如 stopService()的onDestroy()

  • 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.

这篇关于在Android的服务显示状态栏通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆