本博客将围绕Android Security展开,介绍APP/Framework/BSP相关基础知识,博主比较懒但会认真保证质量
Android四大组件–Activity启动与停止
Android四大组件–Broadcast介绍
Android四大组件–服务
加密算法介绍及应用–RSA算法
本文介绍Android四大组件之Service,服务(Service)和活动(Activity)一样有生命周期的概念,不同的是服务是没有页面显示的,服务一直保持后台运行,对于不需要页面显示的功能非常有用,常见的功能比如后台播放音乐。
生命周期描述了从创建到销毁的全流程,是我们认识服务、操作服务的需要掌握的基础知识。
我前面有一篇文章介绍过Activity,Activity的每个生命周期都有一个对应的方法,Service也一样,有以下方法:
onCreate()
: 创建服务onStart()
: 开始服务,API已弃用,android studio
会提示DeprecatedonStartCommand()
: 开始服务onBind()
: 绑定服务(首次)onRebind()
: 重新绑定服务onUnbind()
: 解除绑定onDestroy
’: 销毁服务startService()
启动的服务,右侧是通过bindService()
启动的服务。和声明activity
/receiver
一样,需要将
声明到
标签内。具体做法是添加如下内容到`AndroidManifest.xml
<application>
...
<service android:name=".MyService"/>
application>
在项目包名上右键->New->Java Class,创建MyService类,继承Service
类,重写各方法
package com.example.chapter08;
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 MyService extends Service {
private final static String TAG = MyService.class.getSimpleName();
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.d(TAG, "onBind()...");
return mBinder;
}
@Override
public void onRebind(Intent intent) {
Log.i(TAG, "onRebind()...");
super.onRebind(intent);
}
@Override
public void onCreate() {
Log.d(TAG, "onCreate()...");
super.onCreate();
}
@Override
public void onStart(Intent intent, int startId) {
Log.d(TAG, "onStart()...");
super.onStart(intent, startId);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand()...");
return super.onStartCommand(intent, flags, startId);
}
@Override
public boolean onUnbind(Intent intent) {
Log.d(TAG, "onUnbind()...");
// return super.onUnbind(intent);
return true;
}
@Override
public void onDestroy() {
Log.d(TAG, "onDestroy()...");
super.onDestroy();
}
//mBinder是黏合剂,用于绑定服务后调用服务提供的方法
private final IBinder mBinder = new LocalBinder();
public class LocalBinder extends Binder {
public MyService getService() {
return MyService.this;
}
}
//用于绑定服务的测试方法,绑定后可发起调用
public void testFunc() {
Log.d(TAG, "testFunc()...");
}
}
有两种方式启停服务,startService()
是最简单的启动服务,还有一种是通过绑定服务
startService()
会直接调用onStartCommand()
//启动服务
startService(new Intent(this, MyService.class));
//停止服务
stopService(new Intent(this, MyService.class));
测试日志输出:
//startService()
11-16 20:15:53.618 4769 4769 D MyService: onCreate()...
11-16 20:15:53.620 4769 4769 D MyService: onStartCommand()...
11-16 20:15:53.620 4769 4769 D MyService: onStart()...
//stopService()
11-16 20:16:00.556 4769 4769 D MyService: onDestroy()...
绑定服务复杂点,需要定义一个ServiceConnection
对象,需要实现两个函数,对应服务绑定和解绑时的行为
private MyService myService;
//定义ServiceConnection对象
private ServiceConnection connection = new ServiceConnection() {
@Override
//当绑定上时,获取绑定的服务对象,调用服务的方法
public void onServiceConnected(ComponentName name, IBinder service) {
myService = ((MyService.LocalBinder)service).getService();
myService.testFunc();
}
@Override
public void onServiceDisconnected(ComponentName name) {
myService = null;
}
};
//绑定服务
Intent intent = new Intent(this, MyService.class);
bindService(intent, connection, Context.BIND_AUTO_CREATE);
//解除绑定
unbindService(connection);
测试日志输出:
//bindService()
11-16 20:09:17.253 4769 4769 D MyService: onCreate()...
11-16 20:09:17.265 4769 4769 D MyService: onStartCommand()...
11-16 20:09:17.265 4769 4769 D MyService: onStart()...
11-16 20:09:18.953 4769 4769 D MyService: onBind()...
11-16 20:09:18.964 4769 4769 D MyService: testFunc()...
//unbindService()
11-16 20:09:20.357 4769 4769 D MyService: onUnbind()...
11-16 20:09:21.290 4769 4769 D MyService: onDestroy()...