news 2026/6/16 6:06:14

安卓基础之《(28)—Service组件》

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
安卓基础之《(28)—Service组件》

一、Service概述

1、Service是什么
Service是一个应用组件,它用来在后台完成一个时间跨度比较大的工作且没有关联任何界面

2、应用退出,Service还在运行

3、一个Service可以完成:访问网络、播放音乐、文件IO操作、大数据量的数据库操作

4、Service的特点
(1)Service在后台运行,不用与用户进行交互
(2)即使应用退出,服务也不会停止
(3)启动应用会创建一个进程,应用退出,但是进程没死
(4)在默认情况下,Service运行在应用程序进程的主线程(UI线程)中,如果需要在Service中处理一些网络连接等耗时的操作,那么应该将这些任务放在子线程中处理,避免阻塞用户界面

二、Service的分类

1、Local Service(本地服务)
Service对象与Service的启动者在同个进程中运行,两者的通信是进程内通信

2、Remote Service(远程服务)
Service对象与Service的启动者不在同一个进程中运行,这时存在一个进程间通信的问题,android专门为此设计了AIDL来实现进程间通信

三、本地Service

1、声明Service
在清单文件AndroidManifest.xml中静态声明‌

2、启动与停止Service
方式一:一般启动
startService(Intent intent)
stopService(Intent intent)

方式二:绑定启动
bindService(Intent intent, ServiceConnection connection)
unbindService(ServiceConnection connection)

3、只有当onBind()、onUnBind()方法执行了,才会执行ServiceConnection里的方法
当服务连接时,如果绑定的服务onBind()没有返回值,则不执行onServiceConnected()方法

4、例子
LocalServiceActivity.java

package com.example.chapter10; import androidx.appcompat.app.AppCompatActivity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.util.Log; import android.view.View; import android.widget.Button; import com.example.chapter10.service.LocalService; public class LocalServiceActivity extends AppCompatActivity implements View.OnClickListener { private Button btn_start; private Button btn_stop; private Button btn_bind; private Button btn_unbind; private ServiceConnection conn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_local_service); findViewById(R.id.btn_start).setOnClickListener(this); findViewById(R.id.btn_stop).setOnClickListener(this); findViewById(R.id.btn_bind).setOnClickListener(this); findViewById(R.id.btn_unbind).setOnClickListener(this); } @Override public void onClick(View view) { if (view.getId() == R.id.btn_start) { startMyService(view); } else if (view.getId() == R.id.btn_stop) { stopMyService(view); } else if (view.getId() == R.id.btn_bind) { bindMyService(view); } else if (view.getId() == R.id.btn_unbind) { unbindMyService(view); } } // 启动服务 public void startMyService(View v) { // 用显式意图,传入要启动的服务类 Intent intent = new Intent(this, LocalService.class); startService(intent); Log.d("sam", "startMyService 启动服务"); } // 停止服务 public void stopMyService(View v) { Intent intent = new Intent(this, LocalService.class); stopService(intent); Log.d("sam", "stopMyService 停止服务"); } // 绑定服务 public void bindMyService(View v) { Intent intent = new Intent(this, LocalService.class); if (conn == null) { // 创建连接对象 conn = new ServiceConnection() { @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { // 当activity和service连接上时,调用这个方法 Log.d("sam", "onServiceConnected 服务连接"); } @Override public void onServiceDisconnected(ComponentName componentName) { Log.d("sam", "onServiceDisconnected 服务断开连接"); } }; // 绑定service bindService(intent, conn, Context.BIND_AUTO_CREATE); } else { Log.d("sam", "已经绑定"); } } // 解绑服务 public void unbindMyService(View v) { if (conn != null) { // 解绑service unbindService(conn); conn = null; } else { Log.d("sam", "还没有绑定"); } } }

布局文件,activity_local_service.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".LocalServiceActivity"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="测试启动本地服务" android:textSize="17sp" android:gravity="center"/> <Button android:id="@+id/btn_start" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="启动服务"/> <Button android:id="@+id/btn_stop" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="停止服务"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="测试绑定本地服务" android:textSize="17sp" android:gravity="center"/> <Button android:id="@+id/btn_bind" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="绑定服务"/> <Button android:id="@+id/btn_unbind" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="解绑服务"/> </LinearLayout>

清单文件

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.MyApplication"> <activity android:name=".LocalServiceActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".service.LocalService" android:enabled="true" android:exported="false" /> </application> </manifest>

自定义本地服务,LocalService.java

package com.example.chapter10.service; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.util.Log; import androidx.annotation.Nullable; /** * 自定义本地服务 */ public class LocalService extends Service { public LocalService() { Log.d("sam", "LocalService()"); } @Nullable @Override public IBinder onBind(Intent intent) { Log.d("sam", "LocalService onBind()"); // 返回自定义Binder实例 return new BasisBind(); } @Override public boolean onUnbind(Intent intent) { Log.d("sam", "LocalService onUnbind()"); return super.onUnbind(intent); } @Override public void onCreate() { super.onCreate(); Log.d("sam", "LocalService onCreate()"); } @Override public void onDestroy() { super.onDestroy(); Log.d("sam", "LocalService onDestroy()"); } public class BasisBind extends Binder { } }

5、日志
绑定/解绑

2026-04-13 17:17:10.517 13398-13398 sam com.example.chapter10 D LocalService() 2026-04-13 17:17:10.523 13398-13398 sam com.example.chapter10 D LocalService onCreate() 2026-04-13 17:17:10.526 13398-13398 sam com.example.chapter10 D LocalService onBind() 2026-04-13 17:17:10.536 13398-13398 sam com.example.chapter10 D onServiceConnected 服务连接 2026-04-13 17:17:14.453 13398-13398 sam com.example.chapter10 D LocalService onUnbind() 2026-04-13 17:17:14.454 13398-13398 sam com.example.chapter10 D LocalService onDestroy()

onServiceDisconnected仅在服务因异常情况被系统终止时触发‌,而不是在正常解绑服务时调用

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/16 6:06:05

2026年AI搜索优化平台评测:技术架构与效果保障对比分析

进入2026年&#xff0c;企业获取线上流量的核心战场正从传统搜索引擎向AI搜索迁移。然而&#xff0c;面对市场上众多声称能提供GEO&#xff08;生成式引擎优化&#xff09;服务的平台&#xff0c;决策者常陷入困惑&#xff1a;技术参数晦涩难懂&#xff0c;效果承诺真假难辨&am…

作者头像 李华
网站建设 2026/6/16 6:06:05

Wan2.2-I2V-A14B创意工坊:基于STM32F103C8T6的实体交互艺术装置

Wan2.2-I2V-A14B创意工坊&#xff1a;基于STM32F103C8T6的实体交互艺术装置 1. 当硬件遇见AI&#xff1a;一场跨界艺术实验 在创客空间的一角&#xff0c;一块不起眼的蓝色开发板正通过传感器感知着周围环境的变化。这不是普通的电子项目&#xff0c;而是一个将嵌入式系统与A…

作者头像 李华
网站建设 2026/6/16 6:03:54

云服务商等保三级认证:最低费用分析与省钱实战

前言&#xff1a;云上等保&#xff0c;到底能省多少钱&#xff1f; 在上一篇文章中&#xff0c;我们分析了传统等保三级认证的典型成本&#xff1a;80-120万元。对于预算有限的中小企业来说&#xff0c;这个数字可能难以承受。 好消息是&#xff1a;使用云服务商的等保合规解…

作者头像 李华
网站建设 2026/6/16 6:05:51

七、深入探讨 RAG(检索增强生成)的准确率问题。

RAG 系统的准确率不仅仅取决于大模型本身&#xff0c;更是一个贯穿 “数据→检索→生成→评估” 全链路的系统工程。任何一个环节的短板&#xff08;如数据脏乱、检索不准、模型幻觉&#xff09;都会导致最终回答的失败。 基于行业实战经验&#xff0c;我将影响 RAG 准确率的核…

作者头像 李华
网站建设 2026/4/14 7:53:10

Unlock Music Electron:一站式音乐加密文件解锁解决方案

Unlock Music Electron&#xff1a;一站式音乐加密文件解锁解决方案 【免费下载链接】unlock-music-electron Unlock Music Project - Electron Edition 在Electron构建的桌面应用中解锁各种加密的音乐文件 项目地址: https://gitcode.com/gh_mirrors/un/unlock-music-electr…

作者头像 李华
网站建设 2026/4/14 7:50:27

Lingbot-Depth-Pretrain-ViTL-14基础部署教程:Python环境配置与一键启动

Lingbot-Depth-Pretrain-ViTL-14基础部署教程&#xff1a;Python环境配置与一键启动 想试试那个能看懂图片里物体远近的AI模型吗&#xff1f;就是那个叫Lingbot-Depth-Pretrain-ViTL-14的深度估计模型。听起来名字挺长&#xff0c;但说白了&#xff0c;它就是能帮你分析一张照…

作者头像 李华