??????????????????? 前者我们必须通过Activity调用onStop来关闭service,如果Activity不管他,小service会一直运行下去,我们希望的是将Activity与Service绑定,实现关闭Activity同时关闭service。
这里需要一个:
public ServiceConnection connect=new ServiceConnection(){
??@Override
??public void onServiceConnected(ComponentName arg0, IBinder service) {
???// TODO Auto-generated method stub
???try {
????System.out.println("###ServiceConnection Sucess,service is"+service.getInterfaceDescriptor());
???} catch (RemoteException e) {
????// TODO Auto-generated catch block
????e.printStackTrace();
???}
??}
};
来管理绑定和解除绑定
现在我们定义service代码:
public class MyService extends Service{ private IBinder mybinder=new IBinder(){ //取得接口描述信息 public String getInterfaceDescriptor() throws RemoteException { // TODO Auto-generated method stub return "MyService.class";//返回service类名称 } @Override public void dump(FileDescriptor arg0, String[] arg1) throws RemoteException { // TODO Auto-generated method stub } @Override public void dumpAsync(FileDescriptor arg0, String[] arg1) throws RemoteException { // TODO Auto-generated method stub } @Override public boolean isBinderAlive() { // TODO Auto-generated method stub return false; } @Override public void linkToDeath(DeathRecipient arg0, int arg1) throws RemoteException { // TODO Auto-generated method stub } @Override public boolean pingBinder() { // TODO Auto-generated method stub return false; } @Override public IInterface queryLocalInterface(String arg0) { // TODO Auto-generated method stub return null; } @Override public boolean transact(int arg0, Parcel arg1, Parcel arg2, int arg3) throws RemoteException { // TODO Auto-generated method stub return false; } @Override public boolean unlinkToDeath(DeathRecipient arg0, int arg1) { // TODO Auto-generated method stub return false; } }; //绑定时触发 public IBinder onBind(Intent intent) { // TODO Auto-generated method stub System.out.println("service:onBind Intent"+intent); return mybinder; } //重新绑定时触发 public void onRebind(Intent intent) { // TODO Auto-generated method stub System.out.println("service:onRebind Intent"+intent); super.onRebind(intent); } //接触绑定时触发 public boolean onUnbind(Intent intent) { // TODO Auto-generated method stub System.out.println("service:onUnbind Intent"+intent); return super.onUnbind(intent); } //创建时触发 public void onCreat(){ System.out.println("service:onCreat "); super.onCreate(); } // public void onCreate() { // System.out.println("****onCreate"); // // } // // @Override // //销毁时触发 public void onDestroy(){ System.out.println("****onDestroy"); } //启动时触发 public int onStartCommand(Intent intent,int flag,int startid){ System.out.println("****onStart Intent"+intent+" flag "+flag+" startid:"+startid); return Service.START_CONTINUATION_MASK; } }
?
布局函数加两个按钮:
<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="start" android:text="启动Service" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="stop" android:text="停止Service" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="Bind" android:text="启动绑定" /> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="Unbind" android:text="解除绑定" />
?
Activity:
public class MainActivity extends Activity { public ServiceConnection connect=new ServiceConnection(){ @Override public void onServiceConnected(ComponentName arg0, IBinder service) { // TODO Auto-generated method stub try { System.out.println("###ServiceConnection Sucess,service is"+service.getInterfaceDescriptor()); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void onServiceDisconnected(ComponentName arg0) { // TODO Auto-generated method stub System.out.println("###ServiceConnection Failure"); } }; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void start(View v){ MainActivity.this.startService(new Intent(MainActivity.this,MyService.class)); } public void stop(View v){ MainActivity.this.stopService(new Intent(MainActivity.this,MyService.class)); } public void Bind(View v){ MainActivity.this.bindService(new Intent(MainActivity.this,MyService.class) ,MainActivity.this.connect,Context.BIND_AUTO_CREATE); } public void Unbind(View v){ MainActivity.this.unbindService(MainActivity.this.connect); } }
?
按绑定:
接触绑定:
?
当我突然退出程序的时候:
?实例完成,大家课后可以深入思考一下service工作原理
?
?
原文:http://429899791.iteye.com/blog/2211762