ImageVerifierCode 换一换
格式:DOCX , 页数:9 ,大小:95.75KB ,
资源ID:6516963      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/6516963.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(Android Service学习之本地服务.docx)为本站会员(b****6)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

Android Service学习之本地服务.docx

1、Android Service学习之本地服务Service是在一段不定的时间运行在后台,不和用户交互应用组件。每个Service必须在manifest中 通过来声明。可以通过contect.startservice和contect.bindserverice来启动。 Service和其他的应用组件一样,运行在进程的主线程中。这就是说如果service需要很多耗时或者阻塞的操作,需要在其子线程中实现。 service的两种模式(startService()/bindService()不是完全分离的): 本地服务Local Service 用于应用程序内部。 它可以启动并运行,直至有人停止了它或它

2、自己停止。在这种方式下,它以调用Context.startService()启动,而以调用Context.stopService()结束。它可以调用Service.stopSelf() 或 Service.stopSelfResult()来自己停止。不论调用了多少次startService()方法,你只需要调用一次stopService()来停止服务。 用于实现应用程序自己的一些耗时任务,比如查询升级信息,并不占用应用程序比如Activity所属线程,而是单开线程后台执行,这样用户体验比较好。 远程服务Remote Service 用于android系统内部的应用程序之间。 它可以通过自己定义

3、并暴露出来的接口进行程序操作。客户端建立一个到服务对象的连接,并通过那个连接来调用服务。连接以调用Context.bindService()方法建立,以调用 Context.unbindService()关闭。多个客户端可以绑定至同一个服务。如果服务此时还没有加载,bindService()会先加载它。 可被其他应用程序复用,比如天气预报服务,其他应用程序不需要再写这样的服务,调用已有的即可。生命周期 Service的生命周期并不像Activity那么复杂,它只继承了onCreate(),onStart(),onDestroy()三个方法,当我们第一次启动Service时,先后调用了onCre

4、ate(),onStart()这两个方法,当停止Service时,则执行onDestroy()方法,这里需要注意的是,如果Service已经启动了,当我们再次启动Service时,不会在执行onCreate()方法,而是直接执行onStart()方法。 而启动service,根据onStartCommand的返回值不同,有两个附加的模式: 1. START_STICKY 用于显示启动和停止service。 2. START_NOT_STICKY或START_REDELIVER_INTENT用于有命令需要处理时才运行的模式。 服务不能自己运行,需要通过调用Context.startService

5、()或Context.bindService()方法启动服务。这两个方法都可以启动Service,但是它们的使用场合有所不同。 1. 使用startService()方法启用服务,调用者与服务之间没有关连,即使调用者退出了,服务仍然运行。 如果打算采用Context.startService()方法启动服务,在服务未被创建时,系统会先调用服务的onCreate()方法,接着调用onStart()方法。 如果调用startService()方法前服务已经被创建,多次调用startService()方法并不会导致多次创建服务,但会导致多次调用onStart()方法。 采用startService(

6、)方法启动的服务,只能调用Context.stopService()方法结束服务,服务结束时会调用onDestroy()方法。 2. 使用bindService()方法启用服务,调用者与服务绑定在了一起,调用者一旦退出,服务也就终止,大有“不求同时生,必须同时死”的特点。 onBind()只有采用Context.bindService()方法启动服务时才会回调该方法。该方法在调用者与服务绑定时被调用,当调用者与服务已经绑定,多次调用Context.bindService()方法并不会导致该方法被多次调用。 采用Context.bindService()方法启动服务时只能调用onUnbind()

7、方法解除调用者与服务解除,服务结束时会调用onDestroy()方法。看看官方给出的比较流程示意图: 官方文档告诉我们,一个service可以同时start并且bind,在这样的情况,系统会一直保持service的运行状态如果service已经start了或者BIND_AUTO_CREATE标志被设置。如果没有一个条件满足,那么系统将会调用onDestory方法来终止service.所有的清理工作(终止线程,反注册接收器)都在onDestory中完成。拥有service的进程具有较高的优先级 官方文档告诉我们,Android系统会尽量保持拥有service的进程运行,只要在该service已经

8、被启动(start)或者客户端连接(bindService)到它。当内存不足时,需要保持,拥有service的进程具有较高的优先级。1 如果service正在调用onCreate,onStartCommand或者onDestory方法,那么用于当前service的进程则变为前台进程以避免被killed。2 如果当前service已经被启动(start),拥有它的进程则比那些用户可见的进程优先级低一些,但是比那些不可见的进程更重要,这就意味着service一般不会被killed.3 如果客户端已经连接到service (bindService),那么拥有Service的进程则拥有最高的优先级,可

9、以认为service是可见的。4 如果service可以使用startForeground(int, Notification)方法来将service设置为前台状态,那么系统就认为是对用户可见的,并不会在内存不足时killed。如果有其他的应用组件作为Service,Activity等运行在相同的进程中,那么将会增加该进程的重要性。本地service1.不需和Activity交互的本地服务publicclassLocalServiceextendsServiceprivatestaticfinalString TAG =LocalService;OverridepublicIBinder on

10、Bind(Intent intent) Log.i(TAG,onBind);returnnull;OverridepublicvoidonCreate() Log.i(TAG,onCreate);super.onCreate();OverridepublicvoidonDestroy() Log.i(TAG,onDestroy);super.onDestroy();OverridepublicvoidonStart(Intent intent,intstartId) Log.i(TAG,onStart);super.onStart(intent, startId);Activity:publi

11、cclassServiceActivityextendsActivity OverrideprotectedvoidonCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState);setContentView(R.layout.servicedemo);(Button) findViewById(R.id.startLocalService).setOnClickListener(newView.OnClickListener()OverridepublicvoidonClick(View view) / TODO

12、Auto-generated method stub startService(newIntent(com.demo.SERVICE_DEMO); );(Button) findViewById(R.id.stopLocalService).setOnClickListener(newView.OnClickListener()OverridepublicvoidonClick(View view) / TODO Auto-generated method stubstopService(newIntent(com.demo.SERVICE_DEMO););在AndroidManifest.x

13、ml添加:否则启动服务时会提示new Intent找不到com.demo.SERVICE_DEMO。 对于这类不需和Activity交互的本地服务,是使用startService/stopService的最好例子。 运行时可以发现第一次startService时,会调用onCreate和onStart,在没有stopService前,无论点击多少次startService,都只会调用onStart。而stopService时调用onDestroy。再次点击stopService,会发现不会进入service的生命周期的,即不会再调用onCreate,onStart和onDestroy。 而on

14、Bind在startService/stopService中没有调用。2.本地服务和Activity交互 对于这种case,官方的sample(APIDemoapp.LocalService)是最好的例子:/* This is an example of implementing an application service that runs locally* in the same process as the application.The link LocalServiceController* and link LocalServiceBinding classes show how

15、 to interact with the* service.* Notice the use of the link NotificationManager when interesting things* happen in the service.This is generally how background services should* interact with the user, rather than doing something more disruptive such as* calling startActivity().*/publicclassLocalServ

16、iceextendsService privateNotificationManager mNM;/* * Class for clients to access.Because we know this service always * runs in the same process as its clients, we dont need to deal with * IPC. */publicclassLocalBinderextendsBinder LocalService getService() returnLocalService.this;Overridepublicvoid

17、onCreate() mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);/ Display a notification about us starting.We put an icon in the status bar.showNotification();OverridepublicintonStartCommand(Intent intent,intflags,intstartId) Log.i(LocalService,Received start id + startId +: + intent);/

18、 We want this service to continue running until it is explicitly/ stopped, so return sticky.returnSTART_STICKY;OverridepublicvoidonDestroy() / Cancel the persistent notification.mNM.cancel(R.string.local_service_started);/ Tell the user we stopped.Toast.makeText(this, R.string.local_service_stopped,

19、 Toast.LENGTH_SHORT).show();Overridepublic IBinder onBind(Intent intent) return mBinder;/ This is the object that receives interactions from clients.See/ RemoteService for a more complete example.privatefinalIBinder mBinder =newLocalBinder();/* * Show a notification while this service is running. */

20、privatevoidshowNotification() / In this sample, well use the same text for the ticker and the expanded notificationCharSequence text = getText(R.string.local_service_started);/ Set the icon, scrolling text and timestampNotification notification =newNotification(R.drawable.stat_sample, text,System.cu

21、rrentTimeMillis();/ The PendingIntent to launch our activity if the user selects this notificationPendingIntent contentIntent = PendingIntent.getActivity(this, 0,newIntent(this, LocalServiceController.class), 0);/ Set the info for the views that show in the notification panel.notification.setLatestE

22、ventInfo(this, getText(R.string.local_service_label), text, contentIntent);/ Send the notification./ We use a layout id because it is a unique number.We use it later to cancel.mNM.notify(R.string.local_service_started, notification); 这里可以发现onBind需要返回一个IBinder对象。也就是说和上一例子LocalService不同的是,1. 添加了一个publ

23、ic内部类继承Binder,并添加getService方法来返回当前的Service对象;2. 新建一个IBinder对象new那个Binder内部类;3. onBind方法返还那个IBinder对象。Activity:/* Example of binding and unbinding to the link LocalService.* This demonstrates the implementation of a service which the client will* bind to, receiving an object through which it can communicate with the service.*/publicclassLocalServiceBindingextendsActivity privatebooleanmIsBound;privateLocalService mBoundService;Override protectedvoidonCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState);setContentView(R.layout.local_service_binding);/ Wat

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1