在Android的服务显示状态栏通知

编程入门 行业动态 更新时间:2024-10-25 00:35:55
本文介绍了在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的服务显示状态栏通知

本文发布于:2023-11-27 12:09:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638012.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:状态栏   通知   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!