基础概念
服务是一种在后台运行而无需与用户直接交互的组件。 由于服务没有用户界面,因此它不受Activity的生命周期的约束。
所以,服务用于重复且可能长期运行的操作。
服务有以下两种类型
状态 | 描述 |
---|---|
started | Android的应用程序组件,如活动,通过startService()启动了服务,则服务是Started状态。一旦启动,服务可以在后台无限期运行,即使启动它的组件已经被销毁。 |
bound | 当Android的应用程序组件通过bindService()绑定了服务,则服务是Bound状态。Bound状态的服务提供了一个客户服务器接口来允许组件与服务进行交互,如发送请求,获取结果,甚至通过IPC来进行跨进程通信。 |
启动方式一–StartService
通过startService启动后,service会一直无限期运行下去,只有外部调用了stopService()或stopSelf()方法时,该Service才会停止运行并销毁。
我们首先需要写一个类继承Service类,并实现其中的方法
public class MyService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Let it continue running until it is stopped.
Toast.makeText(this, "*******, ***", Toast.LENGTH_LONG).show();
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "服务已经停止", Toast.LENGTH_LONG).show();
}
}
然后需要在AndroidManifest里面进行注册:
添加这样一行代码:
<service android:name=".MyService"></service>
最终在MainActivity里面添加一些可以启动(调用onStart)该Service的组件,这里我还添加了一个摧毁该服务的按钮:
在layout中添加两个按钮,用来启动和摧毁Service
启动方式二–bindService
bindService的启动特点就是client-server型的。Service就是这里所谓的server,安卓组件可以通过bind来获取该service,这里的组件就是client
首先我们需要先定义一个BindService类
public class MyBindService extends Service {
public class MyBinder extends Binder{
public MyBindService getService(){
return MyBindService.this;
}
}
MyBinder myBinder = new MyBinder();
public void onCreate(){
Toast.makeText(this, "*****", Toast.LENGTH_LONG).show();
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "*****", Toast.LENGTH_LONG).show();
return START_NOT_STICKY;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
Toast.makeText(this, "Binding", Toast.LENGTH_LONG).show();
return myBinder;
}
@Override
public boolean onUnbind(Intent intent) {
Toast.makeText(this, "unBinding", Toast.LENGTH_LONG).show();
return false;
}
@Override
public void onDestroy() {
Toast.makeText(this, "Service Destoried", Toast.LENGTH_LONG).show();
super.onDestroy();
}
}
同样,在AndroidManifest里面注册此Service
<service android:name=".MyBindService">
新建一个Activity,然后添加一个connection,同样添加两个函数,bind和unbind
public class BindTestActivity extends AppCompatActivity {
private MyBindService myBindService = null;
private boolean isBind = false;
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
isBind=true;
MyBindService.MyBinder myBinder = (MyBindService.MyBinder)service;
myBindService = myBinder.getService();
}
@Override
public void onServiceDisconnected(ComponentName name) {
isBind=false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bind_test);
}
public void bindService(View view){
bindService(new Intent(this,MyBindService.class),connection,BIND_AUTO_CREATE);
}
public void unBindService(View view){
if (isBind){
unbindService(connection);
}
}
}
最后,在layout中添加相应的按钮:
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="44dp"
android:text="BindService"
android:onClick="bindService"
app:layout_constraintEnd_toEndOf="@+id/textView2"
app:layout_constraintHorizontal_bias="0.461"
app:layout_constraintStart_toStartOf="@+id/textView2"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="unBindService"
android:onClick="unBindService"
app:layout_constraintEnd_toEndOf="@+id/button3"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/button3"
app:layout_constraintTop_toBottomOf="@+id/button3" />