当前位置:网站首页>Service服务
Service服务
2022-07-23 05:42:00 【shuo277】
Service
什么是Service
Service是Android四大组件之一 (必须在androidManifest.xml中进行注册)
可以在后台执行长时间运行操作而没有用户界面的应用组件
Activity MainActivity.java
activity_main.xml
Service XXXXXXX.java
Service在现阶段的APP什么位置有体现?
手机音乐播放器
创建Service
不使用Android Studio应该如何创建?
1. 创建java文件,继续Service 1. 在AndroidManifest.xml中进行注册
使用android Studio应该如何创建?
怎么调用Service
第一种方式:startService 访问者跟Service是不绑定的
启动Service
Intent intent = new Intent(MainActivity.this, MyService.class); startService(intent);
关闭Service
Intent intent = new Intent(MainActivity.this, MyService.class); stopService(intent);
第二种方式:bindService 访问者跟Service是绑定,访问者一旦退出了,Service也就终止了。
绑定service
Intent intent = new Intent(MainActivity.this, MyBindService.class); bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
public ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i("ServiceConnection", "onServiceConnected");
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i("ServiceConnection", "onServiceDisconnected");
}
};
4.Service的声明周期
startService的方式 onCreate() onStartCommand() onDestroy()
bindSer vice的方式 onCreate() onBind() onUnBind() onDestroy()
边栏推荐
- NFT digital collection platform development and construction, source code development digital collection
- 查看真机APP里面沙盒文件
- 1.认识数据库
- Yarn容量调度器设置
- Image fuzzy processing batch production fuzzy data set
- Ten year structure five year life-01 at the beginning of graduation
- MySQL sorts by Chinese field initials
- [system problems] Net Framework 3.5 installation error
- UItextview的textViewDidChange的使用技巧
- mysql修改函数权限未生效
猜你喜欢

Compilation principle - detailed explanation of syntax analysis
![[literature research] search PubMed for papers in journals with specific impact factors](/img/5e/ea762830e73eee2700402d70ba5c43.png)
[literature research] search PubMed for papers in journals with specific impact factors

9、 Practical class

编译原理-语法分析详解

Kubesphere haproxy+kept (I)

UE4解决WebBrowser无法播放H.264的问题

高德定位---权限弹框不出现的问题
![[flick]flick on yarn's flick conf simplest configuration](/img/de/0ec23f3379148dba27fe77dc51e22f.png)
[flick]flick on yarn's flick conf simplest configuration

ninja介绍及使用

数仓4.0笔记——用户行为数据采集一
随机推荐
Development of digital collection system: enterprise layout meta universe digital collection
Wordcount of the first Flink program
第一阶段复习
Ten year structure five year Life-02 first job
BST树
九、实用类
数仓4.0笔记——业务数据采集——Sqoop
MySQL数据库
Review of knowledge points
Method of recognizing b value from DICOM tag in DWI image
Typescript introduction
11. Multithreading
SQL realizes the user statistics of continuous login for more than 7 days
3.1. Simplified supplement to DQL
Yarn容量调度器设置
Kubesphere haproxy+kept (I)
第一个FLINK程序之WordCount
强迫症的硬盘分区
uni原生插件开发--友盟一键登录
MySQL sorts by Chinese field initials