第二篇 (Aidl回调)

废话不说,兄弟们继续奔主题 ^_^

结构如图:

第二篇 (Aidl回调)

Icall代码:

package Aidl_Activity_Service_simple_Icall.Jason;



interface Icall {

	 void showdialog(String p_string);

}

Json代码:

package Aidl_Activity_Service_simple_Icall.Jason;

import Aidl_Activity_Service_simple_Icall.Jason.Icall;

interface Json {

     void register(Icall b);

     void unregister(Icall b);

}

myService代码:

package Aidl_Activity_Service_simple_Icall.Jason;



import android.app.Service;

import android.content.Intent;

import android.os.Handler;

import android.os.IBinder;

import android.os.Message;

import android.os.RemoteCallbackList;

import android.os.RemoteException;



public class myService extends Service {



	

	RemoteCallbackList<Icall> rc=new RemoteCallbackList<Icall>();

	

	Handler hd=new Handler()

	{



		@Override

		public void handleMessage(Message msg) {

			// TODO Auto-generated method stub

			//super.handleMessage(msg);

			if(msg.what==123)

			{

				CallBack();

			}

		//	CallBack();

		}

		

	};

	private Json.Stub js=new Json.Stub() {

		

		public void unregister(Icall b) throws RemoteException {

			// TODO Auto-generated method stub

			rc.unregister(b);

		}

		

		public void register(Icall b) throws RemoteException {

			// TODO Auto-generated method stub

		

			Message msg=hd.obtainMessage(123);

			hd.sendMessage(msg);

			rc.register(b);

			

			

		}

	};

	

	@Override

	public IBinder onBind(Intent intent) {

		// TODO Auto-generated method stub

		//return null;

		return js;

	}



	protected void CallBack() {

		// TODO Auto-generated method stub

		 int i=rc.beginBroadcast();

		 while (i > 0) {

		     i--;

		     try {

		         rc.getBroadcastItem(i).showdialog("成功注册");

		     } catch (RemoteException e) {

		         // The RemoteCallbackList will take care of removing

		         // the dead object for us.

		     }

		 }

		 rc.finishBroadcast();

	}



}

Aidl_Activity_Service_simple_IcallActivity代码:

package Aidl_Activity_Service_simple_Icall.Jason;





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.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.Toast;



public class Aidl_Activity_Service_simple_IcallActivity extends Activity {

	

	

	private Button bind_btn;

	private Button unbind_btn;

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        findAll();

        bind();

    }

    

    public void findAll()

    {

    	bind_btn=(Button) this.findViewById(R.id.bind_btn);

    	unbind_btn=(Button) this.findViewById(R.id.unbind_btn);

    }

    public void bind()

    {

    	bind_btn.setOnClickListener(mylistener);

    	unbind_btn.setOnClickListener(mylistener);

    }

    public View.OnClickListener mylistener=new OnClickListener() {

		

		public void onClick(View v) {

			// TODO Auto-generated method stub

			switch(v.getId())

			{

			case R.id.bind_btn:

				Intent it=new Intent(Aidl_Activity_Service_simple_IcallActivity.this,myService.class);

				bindService(it, sc, BIND_AUTO_CREATE);

				break;

			case R.id.unbind_btn:

				unbindService(sc);

				break;

				default:

					break;

			}

		}

	};

	

	Json js=null;

	Icall.Stub mi=new Icall.Stub() {

		

		public void showdialog(String p_string) throws RemoteException {

			// TODO Auto-generated method stub

			Toast.makeText(Aidl_Activity_Service_simple_IcallActivity.this, p_string, Toast.LENGTH_LONG).show();

			

			

		}

	};

	private ServiceConnection sc=new ServiceConnection() {

		

		public void onServiceDisconnected(ComponentName name) {

			// TODO Auto-generated method stub

			try {

				js.unregister(mi);

			} catch (RemoteException e) {

				// TODO Auto-generated catch block

				e.printStackTrace();

			}

		}

		

		public void onServiceConnected(ComponentName name, IBinder service) {

			// TODO Auto-generated method stub

			js=Json.Stub.asInterface(service);

			try {

				js.register(mi);

			} catch (RemoteException e) {

				// TODO Auto-generated catch block

				e.printStackTrace();

			}

		}

	};

}

你可能感兴趣的:(aidl)