Android提高21篇之五:Service

鸡啄米  •  扫码分享
我是创始人李岩:很抱歉!给自己产品做个广告,点击进来看看。  
Java代码
  1. package com.testService;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.Service;  
  5. import android.content.ComponentName;  
  6. import android.content.Intent;  
  7. import android.content.ServiceConnection;  
  8. import android.os.Bundle;  
  9. import android.os.IBinder;  
  10. import android.util.Log;  
  11. import android.view.View;  
  12. import android.widget.Button;  
  13.   
  14. public class testService extends Activity {  
  15.     Button btnStartMyService,btnStopMyService,btnBindMyService,btnUnbindMyService,btnExit;  
  16.     @Override  
  17.     public void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.main);  
  20.         btnStartMyService=(Button)this.findViewById(R.id.btnStartMyService);  
  21.         btnStartMyService.setOnClickListener(new ClickEvent());  
  22.           
  23.         btnStopMyService=(Button)this.findViewById(R.id.btnStopMyService);  
  24.         btnStopMyService.setOnClickListener(new ClickEvent());  
  25.           
  26.         btnBindMyService=(Button)this.findViewById(R.id.btnBindMyService);  
  27.         btnBindMyService.setOnClickListener(new ClickEvent());  
  28.           
  29.         btnUnbindMyService=(Button)this.findViewById(R.id.btnUnbindMyService);  
  30.         btnUnbindMyService.setOnClickListener(new ClickEvent());   
  31.           
  32.         btnExit=(Button)this.findViewById(R.id.btnExit);  
  33.         btnExit.setOnClickListener(new ClickEvent());  
  34.     }  
  35.     @Override  
  36.     public void onDestroy()  
  37.     {  
  38.             super.onDestroy();  
  39.             Log.e("Activity","onDestroy");  
  40.     }  
  41.       
  42.     private ServiceConnection _connection = new ServiceConnection() {    
  43.                 @Override  
  44.                 public void onServiceConnected(ComponentName arg0, IBinder arg1) {  
  45.                         // TODO Auto-generated method stub  
  46.                 }  
  47.   
  48.                 @Override  
  49.                 public void onServiceDisconnected(ComponentName name) {  
  50.                         // TODO Auto-generated method stub  
  51.                 }    
  52.     };    
  53.     class ClickEvent implements View.OnClickListener{  
  54.   
  55.                 @Override  
  56.                 public void onClick(View v) {  
  57.                         Intent intent=new Intent(testService.this,MyService.class);  
  58.                         if(v==btnStartMyService){  
  59.                                 testService.this.startService(intent);  
  60.                         }  
  61.                         else if(v==btnStopMyService){  
  62.                                 testService.this.stopService(intent);  
  63.                         }  
  64.                         else if(v==btnBindMyService){  
  65.                                 testService.this.bindService(intent, _connection, Service.BIND_AUTO_CREATE);  
  66.                         }  
  67.                         else if(v==btnUnbindMyService){  
  68.                                 if(MyService.ServiceState=="onBind")//Service绑定了之后才能解绑  
  69.                                         testService.this.unbindService(_connection);  
  70.                         }  
  71.                         else if(v==btnExit)  
  72.                         {  
  73.                                 testService.this.finish();  
  74.                         }  
  75.                           
  76.                 }  
  77.               
  78.     }  
  79. }  

随意打赏

提交建议
微信扫一扫,分享给好友吧。