说明
Service 工作在主进程上。生命周期图
两种状态
Started
比如Activity通过调用startService 方法。一旦被启动(Started),服务就永久在后台运行,即使创建他的Activity被销毁。
Bound
当一个Component通过调用bindService方法来绑定该服务。服务提供接口让组件和服务交互。
回调方法说明
onStartCommand 其他组件通过调用startService时,被调用,如果实现该方法,需要调用stopSelf或者stopService来结束服务。
onBind 其他组件通过调用bindService时,被调用。需要实现接口,返回IBinder对象,让Client和服务交互。如果不绑定服务,返回null
onUnbind 所有已经绑定的组件断开
onCreate创建服务只有一次。
onDestroy服务被销毁时调用,这里应该清理相应资源。
例子1:
主界面两个按钮
Activity对应的两个方法
public void startService(View view) { startService(new Intent(getBaseContext(), MyService.class)); } public void stopService(View view) { stopService(new Intent(getBaseContext(), MyService.class)); }
服务类的内容
public class MyService extends Service { @Nullable @Override public IBinder onBind(Intent intent) { return null; } @Override public int onStartCommand(Intent intent, int flags, int startId) { Toast.makeText(this, "Servcie Started", Toast.LENGTH_LONG).show(); return START_STICKY; } @Override public void onDestroy() { super.onDestroy(); Toast.makeText(this, "Service Destroyed", Toast.LENGTH_SHORT).show(); } }
然后在AndroidManifest.xml中添加
运行效果
点击启动服务
点击停止服务
例子2:使用绑定服务,并且在Activity中调用Service的内部类的方法
在例子1的基础上
public class MyService extends Service { public static final String SERVICE_LOG = "service_LOG"; @Nullable @Override public IBinder onBind(Intent intent) { Log.d(SERVICE_LOG, "MyServic Bound"); return new Mybind(); } @Override public boolean onUnbind(Intent intent) { Log.d(SERVICE_LOG, "MyService Unbound"); return super.onUnbind(intent); } @Override public void onCreate() { super.onCreate(); Log.d(SERVICE_LOG, "Myservice created"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d(SERVICE_LOG, "Myservice Started"); return START_STICKY; } @Override public void onDestroy() { super.onDestroy(); Log.d(SERVICE_LOG, "MyService destroyed"); } public class Mybind extends Binder { public void getString() { Log.d(SERVICE_LOG, "============> get a string"); } } }
主要变化,新增内部类Mybind,并且在onBind返回Mybind对象。
Activity中新增
.
新增代码
private MyService.Mybind mybind; private ServiceConnection connection=new ServiceConnection() { @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { mybind = (MyService.Mybind) iBinder; mybind.getString(); } @Override public void onServiceDisconnected(ComponentName componentName) { } }; public void BindService(View view) { Intent bindIntent = new Intent(LoginActivity.this, MyService.class); bindService(bindIntent, connection, BIND_AUTO_CREATE); } public void UnbindService(View view) { unbindService(connection); }
全部代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class LoginActivity extends AppCompatActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_login); } public void startService(View view) {startService(new Intent(getBaseContext(), MyService.class)); } public void stopService(View view) { stopService(new Intent(getBaseContext(), MyService.class)); } private MyService.Mybind mybind; private ServiceConnection connection=new ServiceConnection() { @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { mybind = (MyService.Mybind) iBinder; mybind.getString(); } @Override public void onServiceDisconnected(ComponentName componentName) { } }; public void BindService(View view) { Intent bindIntent = new Intent(LoginActivity.this, MyService.class); bindService(bindIntent, connection, BIND_AUTO_CREATE); } public void UnbindService(View view) { unbindService(connection); } }
运行效果
A测试
点击绑定服务,此时创建服务,然后绑定服务,
Myservice created
MyService Bound
============> get a string
点击解除绑定(解绑之后,再点击解绑,程序会崩溃)
MyService Unbound
MyService destroyed
如果Activity销毁,服务同样自动解绑,销毁。
B测试
点击启动服务
Myservice created
Myservice Started
点击绑定服务
MyServic Bound
============> get a string
点击解除绑定
MyService Unbound
因为是通过startService创建的后台Service,不会销毁。
如果有client绑定,点击“停止服务”,service 不会停止,一旦所有client解除绑定,因为已经点击过“停止服务”,此时服务停止。