android 连接外部服务

CountService

package com.jf.lromateservice;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

/**
 * 如果之间需要传递参数或者方法调用。需要使用bind和unbind方法。
 * 具体做法是,服务类需要增加接口,比如ICountService,
 * 另外,服务类需要有一个内部类,这样可以方便访问外部类的封装数据,
 *  这个内部类需要继承Binder类并实现ICountService接口。
 * 还有,就是要实现Service的onBind方法,不能只传回一个null了。
 * 
 * @author  * 
 */
public class CountService extends Service {
	private boolean threadDisable;

    private int count; 

    private ICountService.Stub serviceBinder=new ICountService.Stub() {
		
		@Override
		public int getCount() throws RemoteException {
			// TODO Auto-generated method stub
			return count;
		}
	};
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return serviceBinder;
	}
	@Override
    public void onCreate() {
        super .onCreate();
        new Thread( new Runnable() {

            @Override
            public void run() {
                while ( ! threadDisable) {
                    try {
                        Thread.sleep( 1000 );
                    } catch (InterruptedException e) {
                    }
                    count ++ ;
                    Log.v( " CountService " , " Count is " + count);
                    System.out.println(" CountService  Count is---> " + count);
                }
            }
        }).start();
    }

    @Override
    public void onDestroy() {
        super .onDestroy();
        this .threadDisable = true ;
        Log.v( " CountService " , " on destroy " );
    }

} 

 

RomateActivity远程服务连接activity

package com.jf.lromateservice;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
/**
 * activity和远程服务直接的通信
 * @author *
 */
public class RomateActivity extends Activity {
    /** Called when the activity is first created. */
	/**
	 * aidl接口文件
	 */
	private ICountService iCountService;
	/**
	 * 连接服务内部类
	 */
	private ServiceConnection serviceConnection=new ServiceConnection() {
		/**
		 * 断开连接
		 */
		@Override
		public void onServiceDisconnected(ComponentName name) {
			// TODO Auto-generated method stub
			iCountService=null;
		}
		/**
		 * 连接上了
		 */
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			// TODO Auto-generated method stub
			iCountService=(ICountService)service;
			try {
	            Log.v( " CountService " , " on serivce connected, count is " 
	                    + iCountService.getCount());
	            System.out.println(" CountService  on serivce connected, count is " 
	                    + iCountService.getCount());
	        } catch (RemoteException e) {
	        	System.out.println("捕捉异常------------》");
	            throw new RuntimeException(e);
	        }

		}
	};
	/**
	 * 初始化
	 */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        System.out.println("begin");
        /**
         * 连接远程service
         * 绑定远程服务的intent 是aidl 而不是service
         */
        this.bindService(new Intent("com.jf.lromateservice.ICountService"), 
        		serviceConnection, BIND_AUTO_CREATE);
    }
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		//this.unbindService(serviceConnection);
		super.onDestroy();
	}
    
}

 aidl

package com.jf.lromateservice;
interface ICountService {
    int getCount();
} 

 

你可能感兴趣的:(android)