就是默默运行在后台的组件,可以理解为是没有前台的activity,适合用来运行不需要前台界面的代码
服务可以被手动关闭,不会重启,但是如果被自动关闭,内存充足就会重启
1.写一个类继承 Service ,实现onBind方法。
public class MyServer extends Service{ @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public int onStartCommand(Intent intent, int flags, int startId) { // TODO Auto-generated method stub return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); } }
2.在AndroidManifest.xml中进行配制
<service android:name="com.itheima.runservice.MyService"></service>
3.开启服务
Intent intent=new Intent(this, MyServer.class); startService(intent);
public class MyService extends Service{ //绑定时调用 @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } //解绑时调用 @Override public boolean onUnbind(Intent intent) { // TODO Auto-generated method stub return super.onUnbind(intent); } @Override public int onStartCommand(Intent intent, int flags, int startId) { // TODO Auto-generated method stub System.out.println("onStartCommand方法"); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { // TODO Auto-generated method stub System.out.println("onDestroy方法"); super.onDestroy(); } @Override public void onCreate() { System.out.println("onCreate方法"); super.onCreate(); } }
03-16 10:33:21.977: I/System.out(18621): onCreate方法 03-16 10:33:21.977: I/System.out(18621): onStartCommand方法
class MyServiceConnection implements ServiceConnection{ //服务连接成功调用 @Override public void onServiceConnected(ComponentName name, IBinder service) { // TODO Auto-generated method stub System.out.println("onServiceConnected方法"); } //服务失去连接调用 @Override public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub System.out.println("onServiceDisconnected方法"); } } intent=new Intent(this,MyServer.class); bindService(intent, conn,BIND_AUTO_CREATE);<span style="color:#008000;"> //解绑度unbindService(conn);那么这个bindService有什么作用呢???
public class MyServer extends Service{ //绑定时调用 @Override public IBinder onBind(Intent intent) { System.out.println("onBind方法"); return new MyIBinder(); } //解绑时调用 @Override public boolean onUnbind(Intent intent) { // TODO Auto-generated method stub System.out.println("onUnbind方法"); return super.onUnbind(intent); } @Override public int onStartCommand(Intent intent, int flags, int startId) { // TODO Auto-generated method stub System.out.println("onStartCommand方法"); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { // TODO Auto-generated method stub System.out.println("onDestroy方法"); super.onDestroy(); } @Override public void onCreate() { System.out.println("onCreate方法"); super.onCreate(); } public class MyIBinder extends Binder{ public void bz(){ leaderBz(); } } public void leaderBz(){ System.out.println("领导办证方法"); } }在MyService 写的一类继承Ibinder 的子类 Binder;在onBind方法中返回Binder继承类的对像。
class MyServiceConnection implements ServiceConnection{ //服务连接成功调用 @Override public void onServiceConnected(ComponentName name, IBinder service) { // TODO Auto-generated method stub System.out.println("onServiceConnected方法"); MyIBinder binder=(MyIBinder) service; binder.bz(); } //服务失去连接调用 @Override public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub System.out.println("onServiceDisconnected方法"); } } intent=new Intent(this,MyServer.class); bindService(intent, conn,BIND_AUTO_CREATE);service连接成功后会在 onServiceConnected方法中返回Binder继承类的对像。就可以调用到bz的方法了。