Android学习(6)Service_服务
Service是什么:
- Service是一个应用程序组件
- Service没有图形界面
- Service通常用来处理一些耗时比较长的操作
- Service可以用来做闹钟等后台持续运行的程序
- Android四大组件之一
1.创建Service
MyService.java
要继承Service
/*
* 启动service时:onCreate->onStartCommand
* 停止service时:onDestroy
* 如果不执行stopService(),那么就算程序退出,service也不会停止
* 如果service不停止,onCreate之执行一次
* */
public class MyService extends Service {
private Timer timer;
private TimerTask task;
private int i=0;
public MyService() {
}
//创建Service时,需要复写的一个onBind()方法。主要是实现与Activity的绑定
@Override
public IBinder onBind(Intent intent) {
return binder;
}
//得到binder对象
private MyBinder binder = new MyBinder();
//自定义MyBinder继承Binder实现通信
public class MyBinder extends Binder{
//获得当前Service的状态
public MyService getMyService(){
return MyService.this;
}
}
//创建
@Override
public void onCreate() {
super.onCreate();
System.out.println("--------Service onCreate--------");
startTimer();
}
//开始执行Service
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("--------Service onStartCommand--------");
return super.onStartCommand(intent, flags, startId);
}
//销毁Service
@Override
public void onDestroy() {
System.out.println("--------Service onDestroy--------");
stopTimer();
super.onDestroy();
}
//开始执行timer
public void startTimer(){
timer = new Timer();
task = new TimerTask(){
@Override
public void run() {
i ++;
System.out.println(i);
}
};
timer.schedule(task,1000,1000);
}
//关闭timer
public void stopTimer(){
timer.cancel();
}
//实现timer的可读性
public int getTimer(){
return i;
}
}
2.创建一个Activity来承载、启动Service
activity_service.xml
给布局文件添加五个按钮,分别是开启service、关闭service、与Activity绑定、解除绑定、获取service的值
<?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">
<Button
android:id="@+id/btnSrv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="start_service"/>
<Button
android:id="@+id/btnSrv2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="stop_service"/>
<Button
android:id="@+id/bindSrv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="bind_service"/>
<Button
android:id="@+id/unbindSrv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="unbind_service"/>
<Button
android:id="@+id/getSrv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="get_service"/>
</LinearLayout>
ServiceActivity
要使用service,Avtivity必须实现ServiceConnection接口,并实现该接口的两个方法onServiceConnected()和onServiceDisconnected()
public class ServiceActivity extends AppCompatActivity implements View.OnClickListener,ServiceConnection{
private Button startSrv;
private Button stopSrv;
private Button bindSrv;
private Button unbindSrv;
private Button getSrv;
private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_service);
startSrv = (Button)findViewById(R.id.btnSrv1);
stopSrv = (Button)findViewById(R.id.btnSrv2);
bindSrv = (Button)findViewById(R.id.bindSrv);
unbindSrv = (Button)findViewById(R.id.unbindSrv);
getSrv = (Button)findViewById(R.id.getSrv);
//给Button做监听
startSrv.setOnClickListener(this);
stopSrv.setOnClickListener(this);
bindSrv.setOnClickListener(this);
unbindSrv.setOnClickListener(this);
getSrv.setOnClickListener(this);
//设置跳转,调用service
intent = new Intent(ServiceActivity.this,MyService.class);
}
public void onClick(View v){
switch (v.getId()){
case R.id.btnSrv1:
//启动Service
startService(intent);
break;
case R.id.btnSrv2:
//停止Service
stopService(intent);
break;
case R.id.bindSrv:
//绑定Service
bindService(intent,this,BIND_ABOVE_CLIENT);
break;
case R.id.unbindSrv:
//解除绑定service
unbindService(this);
break;
case R.id.getSrv:
//提示框显示当前service中timer的值
Toast.makeText(ServiceActivity.this,
"当前service值为:"+my.getTimer(),Toast.LENGTH_LONG).show();
break;
}
}
//得到service
private MyService my;
//要使用service,Avtivity必须实现ServiceConnection接口,并实现该接口的两个方法onServiceConnected()和onServiceDisconnected()
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
System.out.println("onServiceConnected");
MyService.MyBinder m = (MyService.MyBinder)service;
my = m.getMyService();
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
}
3.测试
注意事项:
- 启动service时:onCreate->onStartCommand
- 停止service时:onDestroy
- 如果不执行stopService(),那么就算程序退出,service也不会停止
- 如果service不停止,onCreate只执行一次
启动service,timer开始技时,并打印在控制台中
点击BIND_SERVICE按钮进行绑定Activity后,才能通过点击GET_SERVICE获取当前service中timer的值,并自屏幕中显示
停止service,service销毁,timer不再进行输出
声明:
1.知识点来源于《网易云课堂》——《Android基础视频教程》
2.本文只用于本人自身学习记录,如有侵权,请立即通知我更改或删除