第二篇 (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();
			}
		}
	};
}

转载于:https://www.cnblogs.com/jason-jo/archive/2011/09/08/2171592.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值