1、服务的概念
就是默默运行在后台的组件,可以理解为是没有前台的activity,适合用来运行不需要前台界面的代码,比如说下载文件
服务可以被手动关闭,不会重启;但是如果被自动关闭,内存充足就会重启
startService启动服务的生命周期
onCreate-onStartCommand-onDestroy
重复的调用startService会导致onStartCommand被重复调用,但是不会一直create
注意:在内存充足的情况下,你退出应用程序,你的下载在子线程中还在继续执行,但是在内存不足的情况下就很容易进程被杀死,下载也就停止了,所以我们要在服务中运行
2、5个进程优先级
1. 前台进程:拥有一个正在与用户交互的activity(onResume方法被调用)的进程
2. 可见进程:拥有一个非前台,但是对用户可见的activity(onPause方法被调用)的进程
3. 服务进程:拥有一个通过startService方法启动的服务的进程
4. 后台进程:拥有一个后台activity(onStop方法被调用)的进程(比如你按home键退出到主页了)
5. 空进程:没有拥有任何活动的应用组件的进程,也就是没有任何服务和activity在运行。(比如你按返回键退出了,但是进程不会被销毁,便于下一次启动更快,但是非常容易被杀死)
3、服务的启动与停止:系统杀死服务进程会自动重启,但是用户手动停止就不能启动了。
Service和activity很类似,也是个上下文,也需要在清单文件中配置,也可以显示或隐式启动。
但是服务在内存中只有一个实例,不像activity那样可以启动多个实例。
a,定一个服务 MyService.java 继承Service。
b,配置下清单文件:
因为我们显示启动,不用配置intent-filter和action
<service android:name="com.example.runservice.MyService"></service>
代码演示:
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void start(View v){//绑定一个按钮事件,在xml文件中定义
//启动服务
Intent intent = new Intent(this, MyService.class);
startService(intent);
}
public void stop(View v){
//停止服务
Intent intent = new Intent(this, MyService.class);
stopService(intent);
}
}
MyService.java
public class MyService extends Service {
@Override
public IBinder onBind(Intent intent) {//暂时先不用,这个是绑定服务会调用
System.out.println("bind");
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {//启动服务系统调用
System.out.println("start");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onCreate() {//创建服务系统调用
System.out.println("create");
super.onCreate();
}
@Override
public void onDestroy() {//停止服务系统调用
System.out.println("destroy");
super.onDestroy();
}
}
4、服务的案例-电话侦听
电话状态:
空闲 TelephonyManager.CALL_STATE_IDLE
响铃 TelephonyManager.CALL_STATE_OFFHOOK
接听 TelephonyManager.CALL_STATE_RINGING
获取电话管理器,设置侦听
TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
tm.listen(new MyPhoneStateListener(), PhoneStateListener.LISTEN_CALL_STATE);
代码演示:
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View v){//点击按钮启动服务
Intent intent = new Intent(this, RecorderService.class);
startService(intent);
}
}
RecorderService.java代码:
public class RecorderService extends Service {
private MediaRecorder recorder;//定义一个录音机
@Override
public IBinder onBind(Intent intent) {//暂时不用
就是默默运行在后台的组件,可以理解为是没有前台的activity,适合用来运行不需要前台界面的代码,比如说下载文件
服务可以被手动关闭,不会重启;但是如果被自动关闭,内存充足就会重启
startService启动服务的生命周期
onCreate-onStartCommand-onDestroy
重复的调用startService会导致onStartCommand被重复调用,但是不会一直create
注意:在内存充足的情况下,你退出应用程序,你的下载在子线程中还在继续执行,但是在内存不足的情况下就很容易进程被杀死,下载也就停止了,所以我们要在服务中运行
2、5个进程优先级
1. 前台进程:拥有一个正在与用户交互的activity(onResume方法被调用)的进程
2. 可见进程:拥有一个非前台,但是对用户可见的activity(onPause方法被调用)的进程
3. 服务进程:拥有一个通过startService方法启动的服务的进程
4. 后台进程:拥有一个后台activity(onStop方法被调用)的进程(比如你按home键退出到主页了)
5. 空进程:没有拥有任何活动的应用组件的进程,也就是没有任何服务和activity在运行。(比如你按返回键退出了,但是进程不会被销毁,便于下一次启动更快,但是非常容易被杀死)
3、服务的启动与停止:系统杀死服务进程会自动重启,但是用户手动停止就不能启动了。
Service和activity很类似,也是个上下文,也需要在清单文件中配置,也可以显示或隐式启动。
但是服务在内存中只有一个实例,不像activity那样可以启动多个实例。
a,定一个服务 MyService.java 继承Service。
b,配置下清单文件:
因为我们显示启动,不用配置intent-filter和action
<service android:name="com.example.runservice.MyService"></service>
代码演示:
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void start(View v){//绑定一个按钮事件,在xml文件中定义
//启动服务
Intent intent = new Intent(this, MyService.class);
startService(intent);
}
public void stop(View v){
//停止服务
Intent intent = new Intent(this, MyService.class);
stopService(intent);
}
}
MyService.java
public class MyService extends Service {
@Override
public IBinder onBind(Intent intent) {//暂时先不用,这个是绑定服务会调用
System.out.println("bind");
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {//启动服务系统调用
System.out.println("start");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onCreate() {//创建服务系统调用
System.out.println("create");
super.onCreate();
}
@Override
public void onDestroy() {//停止服务系统调用
System.out.println("destroy");
super.onDestroy();
}
}
4、服务的案例-电话侦听
电话状态:
空闲 TelephonyManager.CALL_STATE_IDLE
响铃 TelephonyManager.CALL_STATE_OFFHOOK
接听 TelephonyManager.CALL_STATE_RINGING
获取电话管理器,设置侦听
TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
tm.listen(new MyPhoneStateListener(), PhoneStateListener.LISTEN_CALL_STATE);
代码演示:
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View v){//点击按钮启动服务
Intent intent = new Intent(this, RecorderService.class);
startService(intent);
}
}
RecorderService.java代码:
public class RecorderService extends Service {
private MediaRecorder recorder;//定义一个录音机
@Override
public IBinder onBind(Intent intent) {//暂时不用