service的几种启动模式(交互)

1,Start的方式与service交互
2,bind的方式与service交互
3, AIDL的方式与service交互

1,Start的方式与service交互

Activity是不能很直接的与Service进行交互,需要借助于其它组件来完成。常见的就是利用广播接收器。Service发送广播,Activity接收广播
主要代码如下,最后附有完整代码

    protected void onResume() {
        super.onResume();
        myreceiver=new MyReceiver();
        IntentFilter filter=new IntentFilter();
        filter.addAction("com.tarena.My_service");
        registerReceiver(myreceiver, filter);
    }

    public void bindstart(View v){
        Intent intent=new Intent(this,MyReceiver.class);
        bindService(intent, conn, Context.BIND_AUTO_CREATE);
    }
    public void bindstop(View v){
        Intent intent=new Intent(this,MyReceiver.class);
        unbindService(conn);
    }

    public class MyReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            String Action=intent.getAction();
            if("com.tarena.My_service".equals(Action)){
                //从intent(service)把时间取出来
                String time=intent.getStringExtra("time");
                tvtime.setText(time);
            }
        }

    }

    protected void onPause() {
        unregisterReceiver(myreceiver);
        super.onPause();
    }   

2,bind的方式与service交互
需要在Service中准备一个IBinder接口的实现类。将该实现类的对象作为onBind方法的返回值。在Activity中需要一ServiceConnection对象,它会有两个回调方法,在onServiceConnected方法中获得Service内onBind方法返回的IBinder对象。通过IBinder来实现与Service的交互。
private void initView() {
tvtime=(TextView)findViewById(R.id.textView1);
conn=new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
}

        @Override
        public void onServiceConnected(ComponentName name, IBinder ibinder) {
            binder=(MyBinder)ibinder;
            handler.postDelayed(new Runnable() {

                @Override
                public void run() {
                    tvtime.setText(binder.getTime());
                    handler.postDelayed(this, 1000);
                }
            }, 1000);
        }
    };
    handler=new Handler();
}
public void bindstart(View v){
    Intent intent=new Intent(this,MyReceiver.class);
    bindService(intent, conn, Context.BIND_AUTO_CREATE);
}
public void bindstop(View v){
    Intent intent=new Intent(this,MyReceiver.class);
    unbindService(conn);
}
public void Start(View v){
    Intent intent=new Intent(this,Myservice.class);
    startService(intent);
}
public void Stop(View v){
    Intent intent=new Intent(this,Myservice.class);
    stopService(intent);
}
public class MyReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        String Action=intent.getAction();
        if("com.tarena.My_service".equals(Action)){
            //从intent(service)把时间取出来
            String time=intent.getStringExtra("time");
            tvtime.setText(time);
        }
    }

}

3, AIDL的方式与service交互
bind方式进行交互是有个前提,Activity和Service是处于同一个app中(同一个进程中)通过AIDL可以实现跨进程的调用一个APP的Activity可以调用另一个App中Service的方法进行AIDL跨进程调用调用,必须使用Service的一个.aidl描述文件才可以进行

private void initview() {
tvtime=(TextView)findViewById(R.id.textView1);
handler=new Handler();
conn=new ServiceConnection() {

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            binder=MyBinder.Stub.asInterface(service);
            handler.postDelayed(new Runnable() {

                @Override
                public void run() {
                    try {
                        tvtime.setText(binder.getTime());
                        handler.postDelayed(this, 1000);
                    } catch (RemoteException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }, 1000);
        }                            
    };
}
public void bind(View v){
    //通过隐式意图启动远程服务
    Intent intent=new Intent("com.tarena.service_time_remove");
    bindService(intent, conn, Context.BIND_AUTO_CREATE);
}
public void unbind(View v){
    unbindService(conn);
    handler.removeCallbacksAndMessages(null);
}

服务在另外一个工程中启动。通过的是intent的隐式传递
aidl文件代码如下:
package com.example.serviceprovider;

interface MyBinder {
String getTime();
}

提供服务工程关键代码:

public class MyService extends Service {
public MyService() {
}

@Override
public IBinder onBind(Intent intent) {
    return new MyBinderImpl();
}
public String getServiceTime(){
    SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss");
    return sdf.format(new Date());
}

public class MyBinderImpl extends MyBinder.Stub{
    //远程调用报异常  抛出的异常
    @Override
    public String getTime() throws RemoteException {
        return getServiceTime();
    }

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值