Service的启动,绑定,绑定后通信,跨应用绑定,跨应用绑定后通信

Service介绍:

与Acitivity平级的Android四大组件之一,对比Activity而言,就是没有交互界面,且可以一直在后台执行,只用于处理下载,I/O等长时间信息交流的任务。

Service的生命周期和运行顺序:

Service的生命周期只有启动状态和摧毁状态,相比Activity而言,更需要特别注意创建和摧毁的过程。

Service运行或被杀死的情况:

1.直接startService()后,若不调用stopService()或stopService()来停止的话,Service将一直运行下去,即使启动的控件已经退出

2.当Service使用bindService()方法来启动的时候,当绑定的组件全部解除绑定则Service被系统杀死,若启动方式是startService()->bindService(),则必须依次使用stopService()和unbindService()来停止Service,此时只使用一种方法,无法杀死Service。

 

Service有两种运行轨迹:

未绑定的Service:

调用startService()->onCreate()->onstartConmmand()->stopService()->ondestory()

直接绑定方法启动Service:

调用bindService()->onCreate()->onBind()->onServiceConnected()->unbindService()->onDestory()

注:onServiceDisConnected() 在所在的线程被杀死和摧毁时调用,但目前没有找到测试运行的方法。

 

这两种状态Service的运行流程的在信息传递上应用:

未绑定Service信息传递的原理:

1.startService()方法会传递给onstartConmmand()方法一个Intent对象,Intent对象可以携带信息

2.被onstartConmmand()接收后,就从Activity对象将信息传递到了Service中

3.Service可以被很多空间绑定,以实现多个Activity之间的信息共享

 

绑定Service在同应用之间的信息传递原理:

1.onBind()方法会返回一个Ibinder对象被onServiceConnected()接收,

2.返回的Ibinder对象由自己自定义一个类,继承android.os.Binder,携带需要传递的信息或提供修改Service属性地方法

3.通过onBind()返回自定义的类的对象给onServiceConnected(),在onServiceConnected()的方法中,接收这个自定义的类的对象实例的信息,就完成了Service到Activity之间的信息交流。

 

绑定Service在不同应用之间的信息传递原理:

1.onBind()方法会返回一个Ibinder对象被onServiceConnected()接收

2.为了实现不同应用之间的交流需要使用到AIDL文件中的接口,首先在被启动的Service所在的Project中新建一个AIDLinterface,在这个接口中可以给出用来信息交互的抽象函数

3.然后在被启动的Service中的onBind()方法中返回一个该接口对象.Stub()方法,并在Service中实现此接口的抽象函数,用于改变Service中的属性或对外提供获取Service属性的方法

4.然后将此Service所在的Project的AIDL文件包拷贝一份在另一个调用的程序Project中

5.在调用Service的程序中定义一个AIDLinterface实例,实现的onServiceConnected()用来接收返回的interface.Stub()

6.此时接口中包含了修改Service属性的方法和获取Service中属性的方法,以此来实现不同程序之间的Activity和Service之间的信息交流

 

 

在同一应用下的Service应用:

Service的启动:

Intent i = new Intent(Activity.this,Service.class);
startService(i);

Service的绑定:

bindService(new Intent(Activity.this,Service.class),MainActivity.this,
Context.BIND_AUTO_CREATE)
//需要在此方法内实现 onServiceConnected()和 onServiceDisConnected()

通过Service传递信息:

Intent i = new Intent(Activity.this,Service.class);
i.putExtra("StringName","StringInformation");
startService(i);

 

在不同应用之间的Service应用:

Service的启动:

Intent i = new Intent();
i.setComponent(new ComponentName("andrew.com.destinationservice","andrew.com.destinationservice.MyService"));
//i.
i.setComponent(new ComponentName("所在程序的完整包名","目标Service在项目中的索引"));
startService(i);

 

Service的绑定:

bindService(new Intent().setComponent(new ComponentName("andrew.com.destinationservice",
"andrew.com.destinationservice.MyService")),MainActivity.this, Context.BIND_AUTO_CREATE);

 

Service绑定后传递信息:

AIDLW接口中的代码信息:

interface IMyAidlInterface {
    /**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */
    void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
            double aDouble, String aString);
    void setData(String data);//设置Service中的data属性
    String getData();    //向外界提供获取Service中data属性的方法
}

Service.java中对于媒介interface的处理代码:

@Override
    public IBinder onBind(Intent intent) {
        Toast.makeText(getApplicationContext(),"onBind",Toast.LENGTH_SHORT).show();
        return new IMyAidlInterface.Stub() {
            @Override
            public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {

            }

            @Override
            public void setData(String data) throws RemoteException {
                MyService.this.data=data;
            }

            @Override
            public String getData() throws RemoteException {
                return MyService.this.data;
            }
        };
    }

Activity中使用AIDLinterface实现信息交流的代码:

  
//接收onBind()传回来的interface.stub() 
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
   binder
= IMyAidlInterface.Stub.asInterface(service); } //使用按钮来同步service和Activity的信息部分 sync.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {   if (binder!=null){ try {   binder.setData(show.getText().toString()); Toast.makeText(getApplicationContext(),binder.getData(),Toast.LENGTH_SHORT).show(); } catch (RemoteException e) {   e.printStackTrace(); } } } });

 

注:

1.interface.stub()方法的具体意义尚未研究

2.Service还有一些在Manifests文件中的配置信息没有去研究和测试

 

posted @ 2016-07-01 23:09  thinfog  阅读(539)  评论(0编辑  收藏  举报