Service

一、Service 概述


Service是在后台运行,不可见,没有用户界面的组件。通常用于播放音乐、记录地理位置的改变、监听某种动作等等。但优先级高于Activity。

Service运行在主线程,不能让它做耗时的请求或动作,可以在单独开一个线程,在线程中做耗时动作。


二、Service种类


1、本地服务(Local Service)

    -用于应用程序内部


     -startService启动 

      stopService结束 

      stopSelf 或stopSelfResult自己停止


     -bindService绑定 

      unbindService解绑定


2、远程服务(Remote Service)


    -Android系统内部的应用程序之间

    -定义IBinder接口


三、Service使用方式


1、Start


    -服务跟启动源没有任何联系

    -无法得到服务对象


2、Bind


    -通过Ibinder接口实例,返回一个ServiceConnection对象给启动源

    -通过ServiceConnection对象的相关方法可以得到Service对象


四、Service生命周期


1、startServiceonCreate() ->onStartCommand()(可多次调用) ->onDestroy()


2、bindServiceonCreate() ->onBind()(不可多次绑定)->onUnbind() -> onDestroy()


3、生命周期图

@Override
public void onCreate() {
    super.onCreate();
    Log.i("info", "Android--onCreate");
}


@Override
public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.
    throw new UnsupportedOperationException("Not yet implemented");
}




@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    new Thread(){
        @Override
        public void run() {
            super.run();
            Log.i("info","Android--onStartCommand");
        }
    }.start();
    return super.onStartCommand(intent, flags, startId);
}


@Override
public void onDestroy() {
    super.onDestroy();
    Log.i("info", "Android--onDestroy");
}

五、使用方法


    -新建自定义Service继承自Service

    -在AndroidManifest.xml中注册这个Service

      android:enabled    Service是否能被系统初始化,默认为true(<application> and<service> 的enable属性必须都为true,该Service才能被系统初                                          始化,缺一不可。 当然,默认情况下就是true,所以一般也不用设置)

 

       android:exported     标识该组件是否可以被其他应用程序调用,或者与其交互(该参数的默认值取决于<service>是否包含<intent-filter>,如果不包                                        含,则默认值为false,反之则为true)

 

       android:icon            进程菜单中显示该Service的Logo

 

       android:label           进程菜单中显示该Service的Name

 

       android:permission  标识此Service的调用权限,如果没有设置,则默认使用<application>的权限设置。

 


1、Start启动


    -在主线程Activity中通过startService(intent)方式启动
     通过stopService(intent)方式停止


   -启动方式是通过启动intent方式实现

    启动后该Service和启动源没有关系,即使主线程退出,service仍会继续运行


2、Bind启动


    -自定义类继承Binder类并实现方法getService(),该方法返回一个自定义BindService类的对象


public class MyBinder extends Binder{
      public MyBindService getService(){
    <span style="white-space:pre">	</span>    return MyBindService.this;
      }
}


@Override
public IBinder onBind(Intent intent) {
    return new MyBinder();


}

     返回一个ServiceConnection对象给启动源,重写onServiceConnected()和onServiceDisconnected()

     启动源通过调用其onServiceConnected()方法取得Service对象

     bindService()中指定ServiceConnection参数

     调用Service中定义的方法

private Intent intent;
MyBindService service;
ServiceConnection con = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName name, IBinder binder) {
        service = ((MyBindService.MyBinder)binder).getService();
    }


    @Override
    public void onServiceDisconnected(ComponentName name) {


    }
};


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    intent = new Intent(MainActivity.this,MyBindService.class);


    findViewById(R.id.btn_start).setOnClickListener(this);


    findViewById(R.id.btn_stop).setOnClickListener(this);


    findViewById(R.id.btn_Bind).setOnClickListener(this);


    findViewById(R.id.btn_UnBind).setOnClickListener(this);


    findViewById(R.id.btn_play).setOnClickListener(this);
}


@Override
public void onClick(View v) {
    switch(v.getId()){
        case R.id.btn_start:
            startService(intent);
            break;
        case R.id.btn_stop:
            stopService(intent);
            break;
        case R.id.btn_Bind:
            bindService(intent,con,Context.BIND_AUTO_CREATE);
            break;
        case R.id.btn_UnBind:
            unbindService(con);
            break;
        case R.id.btn_play:
            service.play();
            break;
    }

}

三、常用系统服务


     


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值