Service 工做在主进程上。生命周期图ide
两种状态测试
Startedthis
好比Activity经过调用startService 方法。一旦被启动(Started),服务就永久在后台运行,即便建立他的Activity被销毁。spa
Bound3d
当一个Component经过调用bindService方法来绑定该服务。服务提供接口让组件和服务交互。code
回调方法说明component
onStartCommand 其余组件经过调用startService时,被调用,若是实现该方法,须要调用stopSelf或者stopService来结束服务。xml
onBind 其余组件经过调用bindService时,被调用。须要实现接口,返回IBinder对象,让Client和服务交互。若是不绑定服务,返回null对象
onUnbind 全部已经绑定的组件断开blog
onCreate建立服务只有一次。
onDestroy服务被销毁时调用,这里应该清理相应资源。
主界面两个按钮
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中添加
运行效果
点击启动服务
点击中止服务
在例子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); }
所有代码
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解除绑定,由于已经点击过“中止服务”,此时服务中止。