Android服务之Service

转载:http://www.cnblogs.com/zhangdongzi/archive/2012/01/08/2316711.html

service定义

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

public class LocalService extends Service {    
	private static final String TAG = "LocalService";     
	private IBinder binder=new LocalService.LocalBinder(); 

	MediaPlayer mediaPlayer=null;    
	
	@Override    
	public IBinder onBind(Intent intent) {
		return binder;    
	}    
	
	@Override     
	public void onCreate() {             
		Log.i(TAG, "onCreate");             
		// if(mediaPlayer==null)           
		//     mediaPlayer=MediaPlayer.create(this, uri);            
		super.onCreate();     
	}     
	
	@Override     
	public void onStart(Intent intent, int startId) {
		Log.i(TAG, "onStart");             
		super.onStart(intent, startId);     
	}     
	
	@Override     
	public int onStartCommand(Intent intent, int flags, int startId) {
		Log.i(TAG, "onStartCommand");         
		return START_STICKY;    
	}            

	@Override     
	public void onDestroy() {
		Log.i(TAG, "onDestroy");
		super.onDestroy();     
	}         

	public class LocalBinder extends Binder{
		LocalService getService(){
			return LocalService.this;
		}    
	}        
}

service本地调用

private void startCustomService(){
	Intent intent=new Intent(this,LocalService.class);         
	startService(intent);    
}

service远程调用

private void BinderService(){
	Intent intent=new Intent(this,LocalService.class);
	bindService(intent, new ServiceConnection(){
		@Override            
		public void onServiceConnected(ComponentName componentName, IBinder binder) {
			localService=((LocalService.LocalBinder)binder).getService();
		}            

		@Override            
		public void onServiceDisconnected(ComponentName componentName) {
			localService=null;                
		}              
	}, Context.BIND_AUTO_CREATE);    
}


补充说明

Service声明

AndroidManifest.xml需要增加

      	<service android:name="com.example.servicedemo.ServiceDemo">  
          <intent-filter>  
              <action android:name="com.example.servicedemo.ServiceDemo"/>  
          </intent-filter>               
		</service>


本地调用

创建Intent时可以使用

new Intent(MainActivity.this,ServiceDemo.class)


或者

new Intent(ServiceDemo.ACTION)

其中,ACTION在ServiceDemo中定位为

public static final String ACTION = "com.example.servicedemo.ServiceDemo";

远程调用

调用Service(bind)的Activity应该拥有以下成员变量,在bind之后就可以通过mLocalService来使用LocalService的方法

	LocalService mLocalService;
	ServiceConnection mSc = new ServiceConnection(){
		@Override
		public void onServiceConnected(ComponentName name,IBinder binder){
			mLocalService = ((LocalService.LocalBinder)binder).getService();
		}
		@Override
		public void onServiceDisconnected(ComponentName name){
			mLocalService = null;
		}
	};

调用方法如下:

		Intent service = new Intent(this.getApplicationContext(),LocalService.class);
		this.bindService(service, mSc, Context.BIND_AUTO_CREATE);




 

你可能感兴趣的:(android,service)