android--service 你应该记住的一些事-1

Android Service和它绑定的Binder应该怎么用? 前言 这几天做公司内部的App做的脑壳疼,Service不仅要和当前的App要关联还要在App内部控制Service的内部业务逻辑,Service和Binder的形式就够用了,奈何是个新手,总得上网上学习学习binder怎么用,度娘一搜,可是为啥看见的都是把binder和Service代码放在一起的,用binder的内部几行函数来返回Service实例,眼看Activity都能直接访问Servi... 阅读详情

<1>Caution: A service runs in the main thread of its hosting process—the service does not create its own thread and does not run in a separate process (unless you specify otherwise). This means that, if your service is going to do any CPU intensive work or blocking operations (such as MP3 playback or networking), you should create a new thread within the service to do that work. By using a separate thread, you will reduce the risk of Application Not Responding (ANR) errors and the application's main thread can remain dedicated to user interaction with your activities.


<2>The most important callback methods you should override are:

onStartCommand()

onBind()

onCreate()

onDestroy()


<3>A service is simply a component that can run in the background even when the user is not interacting with your application.

If you need to perform work outside your main thread, but only while the user is interacting with your application, then you should probably instead create a new thread and not a service. For example, if you want to play some music, but only while your activity is running, you might create a thread in onCreate(), start running it in onStart(), then stop it in onStop(). Also consider usingAsyncTask or HandlerThread, instead of the traditional Thread class.

Remember that if you do use a service, it still runs in your application's main thread by default, so you should still create a new thread within the service if it performs intensive or blocking operations.


<4>If a component calls bindService() to create the service (and onStartCommand() is not called), then the service runs only as long as the component is bound to it. Once the service is unbound from all clients, the system destroys it.


<5>The Android system will force-stop a service only when memory is low and it must recover system resources for the activity that has user focus. If the service is bound to an activity that has user focus, then it's less likely to be killed, and if the service is declared to run in the foreground (discussed later), then it will almost never be killed. 


<6>By default, Android creates a process for an application when the first of its components needs to run. All components then run in that process. The name of the default process matches the package name set by the <manifest> element.

By setting this attribute to a process name that's shared with another application, you can arrange for components of both applications to run in the same process — but only if the two applications also share a user ID and be signed with the same certificate.

If the name assigned to this attribute begins with a colon (':'), a new process, private to the application, is created when it's needed. If the process name begins with a lowercase character, a global process of that name is created. A global process can be shared with other applications, reducing resource usage.


<7>Additionally, you can ensure that your service is private to your application only if you include the android:exported attribute and set it to "false". This is effective even if your service supplies intent filters.


<8>

Service
This is the base class for all services. When you extend this class, it's important that you create a new thread in which to do all the service's work, because the service uses your application's main thread, by default, which could slow the performance of any activity your application is running.
IntentService

This is a subclass of Service that uses a worker thread to handle all start requests, one at a time. This is the best option if you don't require that your service handle multiple requests simultaneously. All you need to do is implement onHandleIntent(), which receives the intent for each start request so you can do the background work.


<9>

The IntentService does the following:

  • Creates a default worker thread that executes all intents delivered to onStartCommand() separate from your application's main thread.
  • Creates a work queue that passes one intent at a time to your onHandleIntent() implementation, so you never have to worry about multi-threading.
  • Stops the service after all start requests have been handled, so you never have to call stopSelf().
  • Provides default implementation of onBind() that returns null.
  • Provides a default implementation of onStartCommand() that sends the intent to the work queue and then to your onHandleIntent() implementation.


<10>however, you require your service to perform multi-threading (instead of processing start requests through a work queue), then you can extend the Service class to handle each intent.

Android实现开机自动启动Service的示例教程 Android权限模型分为声明权限和运行时权限两种。声明权限是在应用的文件中声明需要使用的权限。运行时权限是在应用运行时请求用户授权,这从Android 6.0(API级别23)开始被引入,用于保护用户隐私和安全。上述代码声明了访问精确位置信息的权限,但用户必须在应用运行时授权。随着Android系统碎片化问题日益严重,各大手机厂商纷纷推出自己的系统优化方案,其中对自启动权限的管理差异尤为明显。例如,华为的EMUI系统对后台应用的管理较为严格,而小米的MIUI则提供了较为开放的权限管理。 阅读详情

相关推荐

手把手教你设计MTBF加速寿命试验:从阿伦尼斯模型到卡方计算(含Excel模板)

本文详细介绍了如何设计MTBF加速寿命试验,从阿伦尼斯模型到卡方计算,帮助工程师在合理时间内获得准确的MTBF数据。通过工业电源模块的实例演示,涵盖活化能确定、温度应力设计、样本量计算及数据记录规范,并提供Excel模板实现自动化计算,显著提升测试效率。

weixin_42563415的博客 546

android--service 你应该记住的一些事-2

[1].However, if you want the service to send a result back, then the client that starts the service can create a PendingIntent for a broadcast (with getBroadcast()) and deliver it to the service

好读书的程序员的博客 942

连续相位调制通信调制方法的载波同步和定时同步研究及MATLAB仿真

本文将探讨CPM调制方法中的载波同步和定时同步问题,并提供MATLAB仿真源代码。本文讨论了连续相位调制通信中的载波同步和定时同步问题,并提供了相应的MATLAB仿真源代码。通过适当的载波同步和定时同步方法,可以实现对CPM信号的准确解调。希望本文对您理解连续相位调制通信调制方法的载波同步和定时同步问题有所帮助,同时也为您提供了一些MATLAB仿真实现的思路和示例代码。在CPM系统中,载波同步的主要任务是估计接收信号的载波频率偏移和相位偏移。在CPM系统中,常用的定时同步方法是基于自相关函数的峰值检测。

2301_79331230的博客 692

Service 详解(一) --- 简介

什么是服务Service 是一个可以在后台执行长时间运行操作而不提供用户界面的应用组件。 服务可由其他应用组件启动,而且即使用户切换到其他应用,服务仍将在后台继续运行。 此外,组件可以绑定到服务,以与之进行交互,甚至是执行进程间通信 (IPC)。 例如,服务可以处理网络事务、播放音乐,执行文件 I/O 或与内容提供程序交互,而所有这一切均可在后台进行。服务的两种形式: 启动服务当应用组

Ling的专栏 702

【python】懂车帝字体反爬逐层解密案例(附完整代码)

懂车帝字体反爬解密,完整代码分享

景天科技苑 2万+

YOLO26优化:注意力魔改 | 用于微小目标检测的上下文增强和特征细化网络ContextAggregation,助力小目标检测,暴力涨点

用于微小目标检测的上下文增强和特征细化网络ContextAggregation,助力小目标检测

①答疑群聊服务;②YOLO大模型知识问答系统;③计算机视觉论文生成智能体; 492

解读Android之Service(1)基础知识

本文讲解Service的基础知识包括:基础概念,Service两种方式,Service注册,Service标签属性,Started Service方式,继承IntentService类,继承Service类,停止Service,管理Service生命周期 等。

sywyg 6万+

【深度学习】什么是自回归模型?

自回归模型是一种重要的时间序列模型,通过利用过去的值来预测当前或未来的值。它在统计学、经济学、自然语言处理等领域有广泛的应用。自回归模型的简单性和可解释性使其成为许多应用的首选,但在更复杂的情况下,可能需要结合其他模型如移动平均模型(MA模型)或混合模型(如ARIMA)来更好地捕捉时间序列中的模式。扩散模型(Diffusion Model)和自回归模型(Autoregressive Model)是两种不同的生成模型,各自有独特的特点和应用场景。

q742971636的博客 1674

23 个安卓重难点突破,带你吃透 Service 知识点「长达 1W+ 字」

thread是程序执行的最小单元,他是分配cpu的基本单位安卓系统中,我们常说的主线程,UI线程,也是线程的一种。当然,线程里面还可以执行一些耗时的异步操作。而service大家记住,它是安卓中的一种特殊机制,service是运行在主线程当中的,所以说它不能做耗时操作,它是由系统进程托管,其实service也是一种轻量级的IPC通信,因为activity可以和service绑定,可以和service进行数据通信。而且有一种情况,activity和service。

2501_90400519的博客 843

使用mindie:2.0.RC2-800I-A2-py311-openeuler24.03-lts制作一个通用的模型推理性能测试的镜像

本文详细介绍了在NPU环境下进行模型推理性能测试的完整流程。首先配置了测试所需的环境和设备信息,包括Python 3.10.9和3.11.6版本、测试脚本和镜像文件。然后分步骤说明了镜像制作过程:创建特权容器、安装Python 3.10.9、配置VLLM环境,并详细描述了测试容器的启动方法。最后提供了镜像打包和使用指南,包括如何加载镜像、启动容器、运行测试脚本(以DeepSeek-32B模型为例)以及更换测试模型的步骤。整个过程涉及环境变量配置、权限设置和服务启动等关键操作。

m0_50105717的博客 1015

产品包装质检实战:基于YOLOv5的智能包装合格检测系统全流程指南

摘要: 针对产品包装质检效率低、漏检率高的问题,本文提出基于YOLOv5的智能检测系统。系统通过采集包装缺陷数据(破损、缺标、封装不良等),利用YOLOv5实现多类别缺陷识别(准确率≥95%),并开发Flask Web界面实现检测可视化与报告生成。实验表明,该系统在30帧/秒的产线速度下可稳定运行,显著提升质检效率。项目涵盖数据标注、模型训练、系统开发全流程,具有工业落地价值,适用于智能制造场景的自动化质检需求。(149字)

LXY_LOVE36的博客 96

android动态加载service,安卓四大组件之二#2-使用AIDL技术实现动态调用其他app中的Service的方法...

上次我给大家介绍了Service的基本用法,其中有提到过如何访问自身app的Service,现在我来为大家介绍如何访问其他app的Service1:创建一个安卓项目app1并且新建一个service,在Service清单文件中配置访问此服务所需要的过滤条件清单代码如下android:name="com.example.aidl.MyService"android:enabled="true"and...

weixin_32346177的博客 519

倍福--RS232自由口实现

RS232/485通信协议是在现场中常用到的,比如触摸屏、温控模块等设备控控制器做通信,会常用到此类协议,本文介绍RS232/485自由口通信的实现,并用PC调试助手进行测试。 目 录 软硬件版本 3 1.1. 倍福Beckhoff 3 1.1.1. 控制器硬件 3 1.1.2. 控制器软件 3 硬件连接 3 2.1. PC COM介绍 3 2.2. RS232/RS485连接 3 3.2.1. N030…………. 4 3.2.2. N031…………. 4 2.3. 实际线路连接 5 PLC程序 7 3

weixin_41883890的博客 3284

Android 中 Service 用法

Android Service 是一种在后台执行任务的组件,适用于音乐播放、文件传输等场景。它分为启动式(一次性任务)和绑定式(持续交互)两种类型,默认运行在主线程,耗时任务需使用协程/线程池。Android 8+ 要求长期任务使用前台服务并显示通知。开发时需注意及时停止服务、适配不同系统版本的后台限制,并合理选择 onStartCommand() 的返回值。对于简单任务,建议优先使用 WorkManager 或协程替代 Service。

专注Android开发与面试题深度解析。分享源码、性能优化、Jetpack、Compose等进阶干货,以及大厂真题解题思路。拒绝废话,直击重点,帮你建立完整知识体系。 505

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

Android Service

【Android Service】 Service 简介(★★★) 很多情况下,一些与用户很少需要产生交互的应用程序,我们一般让它们在后台运 行就行了,而且在它们运行期间我们仍然能运行其他的应用。为了处理这种后台进程, Android 引入了 Service 的概念。 Service 在 Android 中是一种长生命周期的组件,它不实现任何用户界面,是一个没有界面的 Activity

moira33的博客 1173

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

23 个安卓重难点突破,带你吃透 Service 知识点「长达 1W+ 字」(2)

thread是程序执行的最小单元,他是分配cpu的基本单位安卓系统中,我们常说的主线程,UI线程,也是线程的一种。当然,线程里面还可以执行一些耗时的异步操作。而service大家记住,它是安卓中的一种特殊机制,service是运行在主线程当中的,所以说它不能做耗时操作,它是由系统进程托管,其实service也是一种轻量级的IPC通信,因为activity可以和service绑定,可以和service进行数据通信。而且有一种情况,activity和service。

2401_84544495的博客 905

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

Android开发必须知道SERVICE的10件事

这些年我在和其他安卓攻城狮交流时经常谈到的一个话题就是Service组件被开发者错误地理解,不管是新手还是老司机。这篇文章就是交流的成果。 这篇文章不会讲解Service的使用方法,这个要去看官方的(被墙的)文档:http://developer.android.com/guide/components/services.htmlhttps://developer.android.com/int...

weixin_34060299的博客 127

Service——(1)概述

本文始发于github,由于文内部分链接使用的是相对路径,如果访问不到,请移步github项目内阅读即可正常访问。 原文(英文)地址 文章目录在Service和Thread之间做出选择基础知识使用清单文件声明Service创建启动(started)Service扩展 IntentService 类扩展Service类启动Service停止Service创建绑定(bound)Service向用户发送...

ai-exception的博客 774

Android Service 通知Activity更新界面的方法研究|Service通过Broadcast更新UI

Android的最重要的组件式service和activity,那么在使用的过程中,我们最常遇到的问题是他们之间的通信问题。当然今天我们不研究底层的实现问题,我从上层的应用的如何使用的角度进行研究。 首先Activity调用Service 这个是比较基础的,它有两种常见的方法; 第一, 通过Intent,这个比较简单了,可以指定package name和class name的方式

neiloid的专栏 1万+

Android:Service通知Activity更新界面

Android有四大组件,其中包括service和activity,那么在使用的过程中,我们最常遇到的问题是他们之间的通信问题。1.首先Activity调用Service这个是比较基础的,它有两种常见的方法;1. 通过Intent可以指定package name和class name的方式来调用,Intent.setClassName这个成员即可。通过putString来装载数据,startServ

BAOLIANG196的博客 5105

Service组件

Service组件 1.概述:  (1)是什么?   2)安卓中的应用组件(生命周期方法)   3)安卓中的一个服务(长时间的工作在后台)   4)安卓中的一个Context(具备强大的资源访问能力)  (2)Service的应用场合: Service在实际应用通常用于执行长时间的耗时操作;例如下载,音乐的播放,新闻信息的获取。。。。。 2.FAQ:  1)耗时操作不是应该在工作

zhangbenzhi的博客 454

Service十件你不知道的事

Service十件你不知道的事 原文链接 : 10 things you didn’t know about Android’s Service component原文作者 : Eugenio Marletti译文出自 : 开发技术前线 www.devtf.cn转载声明: 本译文已授权开发者头条享有独家转载权,未经允许,不得转载!译者 : chaossss校对者: desmond11

357

【Android】16.1 Android Service基本概念

分类:C#、Android、VS2015; 创建日期:2016-03-01 一、简介 为了解决在后台运行任务的问题,Android引入了一个称为Service的应用程序组件。Service的职责是专门用于创建、开始和停止在后台执行的任务,同时提供前台和后台交互的编程接口。 注意Android系统中所指的Service和我们平常所说的Web Service不是一回事,Android系统提供的Serv...

weixin_34202952的博客 94
上一篇: java 中的引用类型
下一篇: android--service 你应该记住的一些事-2
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值