android servicelifecycle 服务的生命周期和程序内部调用服务
2012-10-25 12:09
399 查看
创建服务类:创建MyService类继承Service,其中内部类IServiceImpl继承系统的binder类 和实现自定义接口IService
IService提供外部调用的方法:
service业务类:
创建布局文件:
IService提供外部调用的方法:
package com.example.serverlifecycle; public interface IService { public void callMethodInService(); }系统的binder类实现了IBinder接口.在创建的服务类重写父类的onBind方法中返回实现了binder类和继承iservice的类的一个对象.在实现了binder的类中重写IService接口中的方法,调用在实现了service的类中的方法.这样就能达到调用绑定服务的方法.可以获取服务中的数据.只有绑定的服务才能获取服务数据
service业务类:
package com.example.serverlifecycle; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; /** * 服务的开启方法<br> * 1:开启方法startService(intent)/stopService(intent)<br> * 2:绑定服务bindService(intent, conn, Context.BIND_AUTO_CREATE)/unbindService(conn) * 区别:开启服务,服务一旦开启,与调用者没有任何关系,调用者的activity即使是退出了.也不会影响后台service的运行, * 在activity里不能使用服务的方法<br> * 绑定服务,通过绑定开启的服务,服务跟调用者不求同生但求同死,如果调用者的activity退出了(或者在当前的activity点击返回按钮), * 那它绑定的服务也回跟着退出,绑定方式开启的服务,是可以在activity中调用服务的方法<br> * * @author Administrator * */ public class MyService extends Service { @Override /** * 绑定时调用的方法,如果服务不存在,就执行onCreate方法创建服务,然后执行onBind方法绑定服务,之后再执行onBind方法就没效果了 */ public IBinder onBind(Intent intent) { // IBinder 继承了Binder System.out.println("onBind"); return new IServiceImpl();// 返回自定义的继承了binfer类(等于也实现了IBinder)和实现了IService类的一个对象 } /** * 服务里面的方法 */ public void sayHelloInService() { System.out.println("hello service"); } /** * Binder实现了IBinder接口,IServiceImpl有继承了Binder,等于是IServiceImpl也实现了IBinder方法 * IService 自己定义的接口 * * @author Administrator * */ public class IServiceImpl extends Binder implements IService { @Override public void callMethodInService() { sayHelloInService(); } } @Override /** * 解除绑定调用的方法,调用了onUnbind方法后会跟着调用onDestroy方法,解除服务之后结束服务<br> * 绑定的服务职能解除一次,如果多次解除服务,程序会有异常 */ public boolean onUnbind(Intent intent) { // TODO Auto-generated method stub System.out.println("onUnbind"); return super.onUnbind(intent); } @Override /** * 只有在第一次启动服务或者调用了onDestroy之后再中心开启服务才会调用onCreate */ public void onCreate() { super.onCreate(); System.out.println("onCreate"); } @Override /** * 停止一个服务 */ public void onDestroy() { super.onDestroy(); System.out.println("onDestroy"); } @Override /** * 开始一个服务 */ public void onStart(Intent intent, int startId) { super.onStart(intent, startId); System.out.println("onStart"); } }在配置文件中的application标签中注册上面的服务类
<service android:name="MyService" />
创建布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="启动服务" /> <Button android:id="@+id/end" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="停止服务" /> <Button android:id="@+id/startBindService" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="绑定服务" /> <Button android:id="@+id/endBindService" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="解除绑定" /> <Button android:id="@+id/callMethod" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="调用服务里的方法" /> </LinearLayout>界面业务代码:
package com.example.serverlifecycle; import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity implements OnClickListener { private Button startButton;// 普通开启服务按钮 private Button endButton; private Intent intent; private Button startBindButton;// 绑定服务 private Button endBindButton;// 解除服务 private Button callMethodInService;// 解除服务 private MyServiceConnection conn; private IService iService; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); startButton = (Button) findViewById(R.id.start); endButton = (Button) findViewById(R.id.end); startButton.setOnClickListener(this); endButton.setOnClickListener(this); intent = new Intent(this, MyService.class); startBindButton = (Button) findViewById(R.id.startBindService); endBindButton = (Button) findViewById(R.id.endBindService); startBindButton.setOnClickListener(this); endBindButton.setOnClickListener(this); callMethodInService = (Button) findViewById(R.id.callMethod); callMethodInService.setOnClickListener(this); conn = new MyServiceConnection(); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.start: startService(intent);// 启动服务 break; case R.id.end: stopService(intent);// 停止服务 break; case R.id.startBindService:// 绑定服务 bindService(intent, conn, Context.BIND_AUTO_CREATE);// 绑定服务,参数3如果这个绑定的服务不存在的时候,绑定的时候就会创建出这个服务 break; case R.id.endBindService: unbindService(conn);// 解除绑定服务 break; case R.id.callMethod: iService.callMethodInService(); break; } } @Override protected void onDestroy() { unbindService(conn);// 在activity结束的时候,调用unbindService(conn)可避免出现serviceconnectionleaked异常 super.onDestroy(); } class MyServiceConnection implements ServiceConnection { @Override /** * 绑定某个服务成功后执行的方法,就是在绑定的服务执行了onCreate方法之后 * service,该对象是由绑定服务的onBind方法的返回值,因为onBind()返回值实现IService接口,所以可以强制转换为IService接口类型 */ public void onServiceConnected(ComponentName arg0, IBinder service) { iService = (IService) service; } @Override public void onServiceDisconnected(ComponentName arg0) { } } }
相关文章推荐
- Android(java)学习笔记229:服务(service)之绑定服务调用服务里面的方法 (采用接口隐藏代码内部实现)
- Android中Service的生命周期,远程服务,绑定远程服务,aidl服务调用
- Android管理服务(Service)的生命周期(lifecycle)
- Android管理服务(Service)的生命周期(lifecycle)
- Android 服务(service)的生命周期以及利用bindservice调用服务里面的方法
- 18_Android中Service的生命周期,远程服务,绑定远程服务,aidl服务调用,综合服务案例,编写一个应用程序调用远程支付宝远程服务场景
- android ,广播接受者。调用服务内部方法
- 简单音乐播放实例的实现,Android Service AIDL 远程调用服务
- Android:Service:服务的生命周期
- Android(java)学习笔记227:服务(service)之服务的生命周期(service)
- Android通过广播接收者调用服务(service)内方法
- android bindService 来调用服务中的方法
- Android服务Service详解(作用,生命周期,AIDL)系列文章--绑定服务
- Android调用系统内部的下载程序下载文件(一)
- Android调用系统内部的下载程序下载文件(二)
- 初学Android,闹钟服务调用Service(六十九)
- Android服务Service详解(作用,生命周期,AIDL)系列文章--进一步封装绑定服务(私有化代理类)
- Android调用系统内部的下载程序下载文件(一)
- android Service 服务的生命周期
- android基础笔记:服务(Service) 采用接口的方式调用本地服务的方法