当前位置: 首页 > news >正文

android services

Service 介绍

代码参考https://github.com/Jkoala/koala_android/tree/main/service_demo
Service不同于后台线程它是跟Activity一个层级的

Service 使用

创建

  1. 创建一个自定义Service 继承Service
  2. 重写onCreate onBind onStartCommand
  3. 在Mainfest.xml 注册我们的Service

启动Service

Intent intent = new Intent(this, MyService.class);
startService(intent);

前台服务

在Android中,前台服务(Foreground Service)是一种特殊类型的服务,它在用户界面上显示一个通知,并且保持唤醒状态,直到服务被停止。通常,前台服务用于执行一些与应用程序的主要任务相关的重要任务,例如下载文件、播放音乐、上传数据等。

要创建一个前台服务,你需要执行以下步骤:

创建一个服务类,并继承Service类。
在AndroidManifest.xml文件中,将服务声明为前台服务,并在标签中添加foregroundServiceType属性。
在服务中,调用startForeground()方法,并传递一个通知对象和通知的样式。
在服务中,处理与主要任务相关的逻辑。
停止服务时,调用stopForeground()方法来移除通知。

public class MyForegroundService extends Service {  private NotificationManager mNotificationManager;  private int mNotificationId = 1;  private String mTag = "MyForegroundService";  @Override  public IBinder onBind(Intent intent) {  return null; // No binding provided  }  @Override  public void onCreate() {  super.onCreate();  mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);  startForeground(mNotificationId, createNotification());  }  @Override  public int onStartCommand(Intent intent, int flags, int startId) {  // Perform your main task here  // ...  return START_STICKY; // Service is restarted if it gets terminated  }  @Override  public void onDestroy() {  super.onDestroy();  stopForeground(true); // Removes the notification  }  private Notification createNotification() {  NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)  .setSmallIcon(R.drawable.ic_notification)  .setContentTitle("My Foreground Service")  .setContentText("This is a foreground service notification.")  .setPriority(NotificationCompat.PRIORITY_DEFAULT); // Optional, but recommended for foreground services  return builder.build();  }  
}

Service 与Activity 通信

通过onBind 方法 activity绑定服务获得服务的binder来实现与之通信

不懂就问

为什么还需要Service?

我们都知道在主线程可以用Thread开启一个线程去执行一些耗时的操作,这样不会阻塞UI,但是如果当前的Activity销毁了,就没有办法获取Thread的实例,也就不能再去操作那个Thread。这样Thread的生命周期和Activity就绑定在一起了。Service不一样,只要启动了没有销毁,就一直存在后台中,多个Activity能和一个Service进行关联调用。

相关文章:

  • 国际多语言出海商城源码/返佣产品自动匹配拼单商城源码
  • MacOS将Node.js升级到最新版本
  • pytest 使用(二):前后置(固件,夹具)
  • asp.net老年大学教务管理信息系统VS开发sqlserver数据库web结构c#编程
  • Web Woeker和Shared Worker的使用以及案例
  • 【牛客网】安全—加密和安全
  • CSS3盒模型
  • C语言实现 1.在一个二维数组中形成 n 阶矩阵,2.去掉靠边元素,生成新的 n-2 阶矩阵;3.求矩阵主对角线下元素之和:4.以方阵形式输出数组。
  • Linux编辑器-vim使用
  • Cross Site Scripting (XSS)
  • MongoDB——MongoDB删除系统自带的local数据库
  • 淘宝协议最新版
  • GZ035 5G组网与运维赛题第7套
  • ssh登录界面变成vim提示,进不去系统
  • 分类预测 | Matlab实现KOA-CNN-GRU-selfAttention多特征分类预测(自注意力机制)
  • Apache Zeppelin在Apache Trafodion上的可视化
  • Consul Config 使用Git做版本控制的实现
  • C语言笔记(第一章:C语言编程)
  • express + mock 让前后台并行开发
  • JavaScript 无符号位移运算符 三个大于号 的使用方法
  • JavaScript类型识别
  • leetcode388. Longest Absolute File Path
  • Linux中的硬链接与软链接
  • 高程读书笔记 第六章 面向对象程序设计
  • 基于HAProxy的高性能缓存服务器nuster
  • 技术:超级实用的电脑小技巧
  • 聊聊redis的数据结构的应用
  • 目录与文件属性:编写ls
  • 区块链分支循环
  • 让你成为前端,后端或全栈开发程序员的进阶指南,一门学到老的技术
  • 如何实现 font-size 的响应式
  • 双管齐下,VMware的容器新战略
  • 智能合约Solidity教程-事件和日志(一)
  • ​iOS实时查看App运行日志
  • #单片机(TB6600驱动42步进电机)
  • #鸿蒙生态创新中心#揭幕仪式在深圳湾科技生态园举行
  • (2)STL算法之元素计数
  • (阿里巴巴 dubbo,有数据库,可执行 )dubbo zookeeper spring demo
  • (附源码)spring boot球鞋文化交流论坛 毕业设计 141436
  • (附源码)基于SpringBoot和Vue的厨到家服务平台的设计与实现 毕业设计 063133
  • (附源码)计算机毕业设计SSM基于java的云顶博客系统
  • (教学思路 C#之类三)方法参数类型(ref、out、parmas)
  • (理论篇)httpmoudle和httphandler一览
  • (论文阅读26/100)Weakly-supervised learning with convolutional neural networks
  • (强烈推荐)移动端音视频从零到上手(下)
  • (三)c52学习之旅-点亮LED灯
  • (三)uboot源码分析
  • (一)u-boot-nand.bin的下载
  • (原創) 是否该学PetShop将Model和BLL分开? (.NET) (N-Tier) (PetShop) (OO)
  • (转)3D模板阴影原理
  • (转)德国人的记事本
  • .bat批处理(二):%0 %1——给批处理脚本传递参数
  • .NET CF命令行调试器MDbg入门(三) 进程控制
  • .net CHARTING图表控件下载地址
  • .NET CORE Aws S3 使用