Service:startService()、stopService()、bindService()、unbi

开门见山

开启服务有三种情况:如果直接使用服务,则没有必要进行绑定,但是如果要使用服务里面的方法,则要进行绑定。

具体的启动情况有下:①调用startService(),再调用stopService()。②单独调用bindService()方法,再unbindService()后,以执行服务内部的方法。③先调用startService(),再调用bindService()方法,再调用unbindService(),最后调用stopService()。特殊情况:只要使用了bindService,不管之后是否解绑和停止服务,都可以调用服务中的方法

下面针对这三种启动顺序分别做详细说明。

在讲解之前先贴一下代码: MyService类,里面就不加注释了

{@Overridepublic IBinder onBind(Intent intent) {return new MyBinder();}() {System.out.println(“MyService onCreate():Called by the system when the service is first created”);super.onCreate();}(Intent intent) {System.out.println(“MyService onUnbind():Called when all clients have disconnected from a particular interface published by the service.”);return super.onUnbind(intent);}(Intent intent, int flags, int startId) {System.out.println(“MyService onStartCommand():Called by the system every time a client explicitly starts the service by calling android.content.Context.startService, providing the arguments it supplied and a unique integer token representing the start request.”);return super.onStartCommand(intent, flags, startId);}() {System.out.println(“MyService onDestroy():Called by the system to notify a Service that it is no longer used and is being removed. “);super.onDestroy();}() {System.out.println(“MyService is method1”);}() {System.out.println(“MyService is method2”);}class MyBinder extends Binder {() {method1();}() {method2();}}}

MainActivity类

{private MyBinder myBinder;(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);conn = new MyServiceConnection();}ServiceConnection conn;(View v) {Intent service = new Intent(this, MyService.class);startService(service);}(View v) {Intent service = new Intent(this, MyService.class);bindService(service, conn, Context.BIND_AUTO_CREATE);}(View v) {unbindService(conn);}(View v) {Intent service = new Intent(this, MyService.class);stopService(service);}(View v) {myBinder.callMethod1();}{(ComponentName name, IBinder service) {System.out.println(“MyServiceConnection connection success”);myBinder = (MyBinder) service;}(ComponentName name) {System.out.println(“MyServiceConnection disconnection success”);}}}

activity_main.xml页面布局

===”.MainActivity” ><Button=”start”android:text=”startService” /><Button=”bind”android:text=”bindService” /><Button=”unbind”android:text=”unbindService” /><Button=”stop”android:text=”stopService” /><Button=>

第一种

调用startService(),再调用stopService()。这种情况适用于直接使用Service,不需要外部调用服务内部的方法。

在这一种中,我们分别会点击startService和stopService,在类MyService中,会以onCreate()开始 — 代表第一次创建服务;以onDestory()结束 — 代表服务被销毁;中间会一次或者多次调用(当反复startService时)onStartCommand()方法 — 来表示客户端想明确的启动服务。

当点击startService时,会触发onCreate()和onStartCommand()方法,表示服务是第一次创建并且是客户端明确要求的。这两个方法执行完毕后,后台的服务线程启动。

看一下这张图:

这个过程对应的Log图和应用后台服务进程图如下:

可以清楚的看到,调用了onCreate()和onStartCommand()方法,,同时后台的服务进程也已经启动。

当点击stopService时,会触发onDesctory(),此时会去销毁后台服务进程。 看一下这张图:

这个过程对应的Log图和应用后台服务进程图如下:

可以清楚的看到,调用onDesctory()方法,同时后台服务线程也被销毁了。

第二种

单独调用bindService()方法将Activity和Service绑定,以达到服务内部方法的目的,再调用unbindService()解绑。

不付出,却一定不会有收获,不要奢望出现奇迹。

Service:startService()、stopService()、bindService()、unbi

相关文章:

你感兴趣的文章:

标签云: