介绍
- Server是Android中实现程序后台运行的解决方案.
- 它非常适合去执行那些不需要和用户交互而且还要求长期运行的任务
- 服务的运行不依赖于任何界面,即使程序被切换到后台,或者用户打开另一个应用程序,服务任然能够正常运行
- 我们需要在Server的内部创建子线程,否则可能出现主线程被阻塞的情况
创建自己的MyServer类
继承Service类
public class MyService extends Service {
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
//和活动进行通信使用
throw new UnsupportedOperationException("Not yet implemented");
}
}
继承完还不够,还需重写几个常用的方法
public class MyService extends Service {
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
//和活动进行通信使用
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
//在服务创建的时候调用
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//每次服务启动的时候调用
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
//在服务销毁的时候调用
super.onDestroy();
}
}
别忘了在AndroidManifest进行注册
<service
android:name=".MyService"
android:enabled="true"
android:exported="true">
</service>
enabled | 是否启用这个服务 |
exported | 是否允许除了当前程序之外的其他程序访问这个服务 |
启动和暂停服务
布局文件代码
只有2个按钮,一个启动,一个暂停
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/start_service"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="start_service" />
<Button
android:id="@+id/stop_service"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="stop_service" />
</LinearLayout>
MainActivity
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button startService;
private Button stopService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化View
startService = (Button) findViewById(R.id.start_service);
stopService = (Button) findViewById(R.id.stop_service);
//注册监听
startService.setOnClickListener(this);
stopService.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.start_service:
//开启服务
Intent startIntent = new Intent(MainActivity.this, MyService.class);
startService(startIntent);
break;
case R.id.stop_service:
//暂停服务
Intent stopIntent = new Intent(MainActivity.this, MyService.class);
stopService(stopIntent);
break;
default:
break;
}
}
}
活动和服务进行通信
上面再MainActivity中启动了服务,但是启动后,就没下文了…,服务确实启动了,然而活动并不知道服务到底去做了什么事情,以及完成的如何.
比如说,我们希望在MyService里提供一个下载功能,然后在活动中决定何时开始下载,以及随时查看下载进度.
重写下MyService代码
public class MyService extends Service {
private DownloadBinder mBinder = new DownloadBinder();
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
//和活动进行通信使用
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
//在服务创建的时候调用
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//每次服务启动的时候调用
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
//在服务销毁的时候调用
super.onDestroy();
}
class DownloadBinder extends Binder {
public void startDownload() {
//开始下载
}
public void getProgress() {
//查看进度
}
}
}
布局文件修改,添加2个按钮,一个绑定服务,一个解绑服务
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/start_service"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="start_service" />
<Button
android:id="@+id/stop_service"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="stop_service" />
<Button
android:id="@+id/bind_service"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="bind_service" />
<Button
android:id="@+id/unbind_service"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="unbind_service" />
</LinearLayout>
MainActivity修改
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button startService;//开启服务
private Button stopService;//暂停服务
private Button bindService;//绑定服务
private Button unbindService;//解绑服务
private MyService.DownloadBinder downloadBinder;
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//当活动与服务成功绑定
//service向下转型
downloadBinder=(MyService.DownloadBinder)service;
//开始下载
downloadBinder.startDownload();
//查看进度
downloadBinder.getProgress();
}
@Override
public void onServiceDisconnected(ComponentName name) {
//当活动与服务连接断开时
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化View
startService = (Button) findViewById(R.id.start_service);
stopService = (Button) findViewById(R.id.stop_service);
bindService = (Button) findViewById(R.id.bind_service);
unbindService = (Button) findViewById(R.id.unbind_service);
//注册监听
startService.setOnClickListener(this);
stopService.setOnClickListener(this);
bindService.setOnClickListener(this);
unbindService.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.start_service:
//开启服务
Intent startIntent = new Intent(MainActivity.this, MyService.class);
startService(startIntent);
break;
case R.id.stop_service:
//暂停服务
Intent stopIntent = new Intent(MainActivity.this, MyService.class);
stopService(stopIntent);
break;
case R.id.bind_service:
//绑定服务
Intent bindIntent = new Intent(this, MyService.class);
bindService(bindIntent, connection, BIND_AUTO_CREATE);
break;
case R.id.unbind_service:
//解绑服务
unbindService(connection);
break;
default:
break;
}
}
}