android之callback(service,BroadcastReceiver)

ICallBack.aidl

package com.opensource.aidl.service.callback;

interface ICallBack
{
    void asycMsg(String result);

}


package com.opensource.aidl.service.callback;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
public class CallBackService extends Service
{
    @Override
    public IBinder onBind(Intent intent)
    {
        return binder;
    }
    
    private ICallBack.Stub binder = new ICallBack.Stub()
    {
        @Override
        public void asycMsg(String result)
            throws RemoteException
        {
            Intent intent = new Intent();
            intent.putExtra("hello", "The answter is : " + result);
            intent.setAction("com.opensource.aidl.receiver");
            sendBroadcast(intent);
            
        }
    };
    
}



IntentFilter inf = new IntentFilter();
        inf.addAction("com.opensource.aidl.receiver");
        registerReceiver(br, inf);
        
        Intent intent = new Intent("com.opensource.aidl.service.callback.ICallBack");
        //        unbindService(connection);
        bindService(intent, connection, BIND_AUTO_CREATE);



BroadcastReceiver br = new BroadcastReceiver()
    {
        public void onReceive(android.content.Context context, Intent intent)
        {
            if (mTextView.getText() != null)
            {
                mTextView.setText(mTextView.getText().toString() + "\r\n" + intent.getStringExtra("hello"));
            }
            else
            {
                mTextView.setText(intent.getAction());
            }
        };
    };



 public ICallBack callBackService;
    
    private ServiceConnection connection = new ServiceConnection()
    {
        public void onServiceDisconnected(ComponentName name)
        {
            callBackService = null;
        }
        
        public void onServiceConnected(ComponentName name, IBinder service)
        {
            callBackService = ICallBack.Stub.asInterface(service);
            
            try
            {
                callBackService.asycMsg("OK!!!");
            }
            catch (RemoteException e)
            {
                e.printStackTrace();
            }
        }
    };



 <service
            android:name="com.opensource.aidl.service.callback.CallBackService"
            android:process=":remote" >
            <intent-filter>
                <action android:name="com.opensource.aidl.service.callback.ICallBack" />
            </intent-filter>
        </service>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值