Android Service组件总结
2015-01-21 11:34
399 查看
Service是在后台运行的没有用户界面的应用程序组件。其它组件可以启动一个Service并与Service进行数据交互。包括本地交互以及跨线程通信(IPC, InterProcess Communication)。
关于Service最容易混淆的两点
1、一个Service不是一个独立的进程。一般情况下它与它所属的应用程序是在一个进程里面的。可以通过设置
2、一个Service不是一个线程,它也是运行在主线程中的,如果要做一些耗时的操作可以在Serivce中另外新建线程,如在service的onCreate方法中启动新线程。
开发Service的步骤
1、定义一个继承Service的子类。
2、在AndroidManifest.xml文件中配置该Service。
使用Service的两种方式
1、用startService()方法
除非手动调用stopService(),否则Service的生命周期就由它自己控制。比如service在进行下载的任务,当下载完成时它应该自动停止。这种方式下访问者和Service之间无法进行方法调用以及数据交互
2、用bindServce()方法
bindService方法的完整参数为:bindService(Intent intent, ServiceConnection conn, int flags)。
访问者与service成功连接时会回调conn的onServiceConnected(ComponentName name, IBinder binder),通过返回的这个binder可以实现访问者与service之间的通信。可以将binder看做是service的代理。开发Service子类时必须提供一个onBind()方法,这个方法所返回的IBinder对象会传给上面的binder参数,一般会在Service子类中实现自己的IBinder对象(继承IBinder的实现类Binder)。
访问者通过bindService调用
可以先startService()方法启动service,再用bindService进行绑定。
Service的生命周期
启动方式不同,Service的生命周期也略有差异。
多次调用startService()方法,会多次调用onStartCommand()方法,只有第一次会先调用onCreate()方法。
多次调用bindService()方法,只会调用onCreate()和onBind()方法一次。
当Activity绑定到一个已启动的Service时,该Service的生命周期不会完全绑定到该Activity,当Activity调用unBindService()方法时只会切断与该Service间的联系,不会调用Service的onDestroy()方法。
下面两站图可以帮助理解。
IntentService的使用
IntentService会自动创建单独的线程来处理耗时任务。继承IntentService时只需要重写onHandleIntent方法就可以了。
关于Service最容易混淆的两点
1、一个Service不是一个独立的进程。一般情况下它与它所属的应用程序是在一个进程里面的。可以通过设置
2、一个Service不是一个线程,它也是运行在主线程中的,如果要做一些耗时的操作可以在Serivce中另外新建线程,如在service的onCreate方法中启动新线程。
开发Service的步骤
1、定义一个继承Service的子类。
2、在AndroidManifest.xml文件中配置该Service。
使用Service的两种方式
1、用startService()方法
final Intent intent = new Intent(); intent.setAction("com.example.user.firstapp.FIRST_SERVICE"); intent.setPackage(this.getPackageName()); startService(intent);
除非手动调用stopService(),否则Service的生命周期就由它自己控制。比如service在进行下载的任务,当下载完成时它应该自动停止。这种方式下访问者和Service之间无法进行方法调用以及数据交互
2、用bindServce()方法
bindService方法的完整参数为:bindService(Intent intent, ServiceConnection conn, int flags)。
访问者与service成功连接时会回调conn的onServiceConnected(ComponentName name, IBinder binder),通过返回的这个binder可以实现访问者与service之间的通信。可以将binder看做是service的代理。开发Service子类时必须提供一个onBind()方法,这个方法所返回的IBinder对象会传给上面的binder参数,一般会在Service子类中实现自己的IBinder对象(继承IBinder的实现类Binder)。
publice class MyService extends Service { //定义一些类的成员变量 //...... private int value; private MyBinder binder = new MyBinder(); public class MyBinder extends Binder { //定义一些方法,访问service的数据 public int getValue { return value; } } //必须实现的方法 @Override public IBinder onBind(Intent intent) { return binder; } }
访问者通过bindService调用
public class MainActivity extends ActionBarActivity { private MyService.MyBinder binder; private ServiceConnection conn = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { binder = (MyService.MyBinder)service; } @Override public void onServiceDisconnected(ComponentName name) { binder = null; } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent intent = new Intent(); intent.setAction("com.sysu.aidlclient.action.AIDL_SERVICE"); //this is important intent.setPackage("com.sysu.aidlclient.aidlclient"); bindService(intent,conn, Service.BIND_AUTO_CREATE); } public void getRemoteService(View view) { try { Toast.makeText(this,"the value from the service is: "+binder.getValue()", Toast.LENGTH_LONG).show(); }catch (RemoteException e) { e.printStackTrace(); } System.out.println("button click"); } @Override protected void onDestroy() { super.onDestroy(); this.unbindService(conn); } }
可以先startService()方法启动service,再用bindService进行绑定。
Service的生命周期
启动方式不同,Service的生命周期也略有差异。
多次调用startService()方法,会多次调用onStartCommand()方法,只有第一次会先调用onCreate()方法。
多次调用bindService()方法,只会调用onCreate()和onBind()方法一次。
当Activity绑定到一个已启动的Service时,该Service的生命周期不会完全绑定到该Activity,当Activity调用unBindService()方法时只会切断与该Service间的联系,不会调用Service的onDestroy()方法。
下面两站图可以帮助理解。
IntentService的使用
IntentService会自动创建单独的线程来处理耗时任务。继承IntentService时只需要重写onHandleIntent方法就可以了。
public class MyIntentService extends IntentService { public MyIntentService() { super("MyIntentService"); } @Override protected void onHandleIntent(Intent intent) { long endTime = System.currentTimeMillis()+20*1000; System.out.println("intent service on start"); //下面是耗时20秒的操作 while(System.currentTimeMillis() < endTime) { synchronized (this) { try { wait(endTime - System.currentTimeMillis()); }catch (Exception e) { } } } System.out.println("intent service finish"); } }
相关文章推荐
- android service组件总结
- Android开发总结笔记 四大组件之Service(下) 1-2-6
- android之Service组件学习总结-IntentService(二)
- android四大组件(总结)activity、service、content provider、broadcast receiver
- Android四大组件之二:Service总结
- Android开发总结笔记 四大组件之Service(上) 1-2-4
- Android开发四大组件之Service总结
- android中Service组件总结
- android 四大组件之service学习总结(一)
- Android开发总结笔记 四大组件之Service(中) 1-2-5
- Android基础知识总结---四大组件之一Service(一)
- Android个人每天总结.doc(day07四大组件之:广播接收者&服务Service)
- android四大组件之service知识点最强总结
- Android四大组件之Service(服务)简单总结
- Android Service即四大组件总结
- android四大组件之Service个人总结
- android 四大组件之service学习总结(二)
- Android Service组件的生命周期及用法总结
- Android 组件Service 详解总结,Thread
- Android学习总结——Service组件