Service初步了解

来源:互联网 发布:域名信息模板认证 编辑:程序博客网 时间:2024/06/05 17:54

               Android提供Service类来处理长生命周期操作的应用程序和不需要用户界面的功能。Service的优先级比非活动状态的Activity要高。

               Service的生命周期 有 onCreate----onStart----onDestroy, 其中,onCreate 和onDestroy只能运行一次,onStart可以执行多次。在Android2.0(Api level 5)以后引入OnStartCommand来处理程序。

               创建一个Service

                定义一个Service,要重写onCreate和onBind方法,for example:

               public class MyService extend Service{

                @Override

                public void onCreate(){

                 super.onCreate();

                System.out.println("***************************onCreate********************");

}

                 @Override

                 public IBinder onBind(Intent intent){

                 return null;

}

}

              Service 同Activity一样需要在应用程序的清单文件中进行注册,需要在application 节点内包含一个service标记

              <service android:enabled ="true" android:name=".MyService" android:permission="com.test.MY_SERVICE_PERMISSION"/>

              最后一个permission属性是确保Service只能由自己的应用程序启动和停止。

              执行一个Service

              public int onStartComand(Intent intent,int flags , int startId){

               startBackgroundTask(intent , startId);

               return Service.START_STICKY;   这个返回值主要是用来控制当Service被在运行时被终止后,系统应该如何响应Servicr的重新启动,

}

              返回值有以下几种类型:START_STICKY        START_NOT_STICKY      STAR_REDELIVER_INTENT

               现在看下onStartCommand方法的参数  Intent参数 总所周知,flag参数是用来找出启动Service的方式。

               flag参数:

               START_FLAG_REDELIVERY  表示Intent参数是由于系统运行时在通过调用stopSelf显式停止Service之前终止它而重新传递的;

               FLAG_RETRY  表示Service已经在异常终止后重新启动。如果Service之前设为START_STICKY,则会传入这个标志。

               启动和终止Service

               启动------》

                显式启动MyService

                Intent intent = new Intent(this,MyService.class);

                startService(intent);

                隐式启动一个音乐Service  要进行隐式启动,需要注册合适的Intent Receiver

                Intent intent =  new Intent(MyMusicService.PLAY_ALBUM);

                intent.putExtra(MyMusicService.ALBUM_NAME_EXTRA,"United");

                intent.putExtra(MyMusicService.ARTIST_NAME_EXTRA,"Pheonix");

                startService(intent);

                停止--------》

                方法同启动相似  将startService换成stopService即可

                绑定Service到Activity

                第一步   实现一个可以进行绑定的Service 

                实现onBind方法

                public IBinder onBind (Intent intent){

                return binder;

}

                public class MyBinder extends Binder(

                   MyMusicService getService(){

                        return MyMusicService.this;

}

)

                private final IBinder binder = new MyBinder();

Service 与其他链接的时候有个ServiceConnection参数,实现ServiceConnection,建立连接后,重写onServiceConnected和onServiceDisconnected两个方法

              第二步 实现连接用的ServiceConnection

              private MyMusicService mservice;     //引用Service

              private ServiceConnection mConnection  = new ServiceConnection(){

                  public void onServiceConnected (ComponentName className, IBinder service){

                       mservice = ((MyMusicService.MyBinder)service).getService();          //建立连接

}

              public void onServiceDisconnected(ComponentName className){

                   mservice = null;           //断开连接

}

}


               第三步 绑定Service

               Intent  bindIntent  = new Intent (MyActivity.this , MyMusicService.class);

               bindService(bindIntent,mConnection,Context.BIND_AUTO_CREATE);

               其中,bindService方法的第三个标志位有以下几个:BIND_ADJUST_WITH_ACTIVITY        BIND_IMPORTANT和BIND_ABOVE_CLIENT     BIND_NOT_FOREGROUND                BIND_WAIVE_PRIORITY

               一旦Service被绑定之后,就可以通过从onServiceConnected处理程序获得的serviceBinder对象来使用Service所有的公有属性和方法!


              for  example:  生命周期

              有AService和BActivity

              BActivity界面有四个按钮  分别为   startServicebtn(启动Service按钮)   stopServicebtn(停止Service按钮)    BindServicebtn(绑定Service按钮)   UnBindServicebtn(解除绑定Service按钮)

            情况一:  绑定Service后 ,运行应用程序BActivity,启动Service等操作,之后关闭应用程序Bactivty,AService也会运行onDestroy方法。

            情况二: startServuce后,运行应用程序BActivity,依旧能够绑定成功,关闭应用程序Bactivty,AService不会随着BActivit的关闭而关闭。

            Service的启动。停止和控制是通过其他应用程序组件来实现的,包括Activity、Broadcast receiver和其他Service。运行中的Service具有比处于非激活状态的或者不可见状态(已经停止)的Activity要高的优先级,所以他们被运行时的资源管理终止的可能性要小。

            本文是笔者看到的相关Service资料的一个初步总结之后将会有通过Broadcastreceiver启动的介绍等等


             

0 0