Android 服务(本地服务示例)(一)

 Android 支持服务的概念。服务是在后台运行组件,没有用户界面。可以将这些组件想象为Windows 服务或UNIX服务。 与这些服务类型类似,Android服务始终可用,但无需主动执行某些操作。
       Android 支持两种服务类型的服务:本地服务和远程服务。本地服务无法供在设备上运行其他应用程序访问。一般而言,这些服务类型仅支持承载该服务的应用程序。而对于远程服务,除了可以承载服务的应用程序访问,还可以从其他应用程序访问。远程服务使用 AIDL(Android Interface Definition Language, Android接口定义语言)向客户端定义其自身。

       Android 中的服务
       通过查看 android.app.Service的公共方法,我们可以更深入地理解服务的概念。

Java代码:

  1. Application getApplication();
  2. Abstract IBinder onBind(Intent intent);
  3. void onConfigurationChanged(Configuration newConfig);
  4. void onCreate();
  5. void onDestroy();
  6. void onLowMemory();
  7. void onRebind(Intent intent);
  8. void onStart(Intent intent, int startId);
  9. boolean onUnbind(Intent intent);
  10. final void setForeground(Boolean isForeground);
  11. final void stopSelf();
  12. final void stopSelf(int startId);
  13. final boolean stopSelfResult(int startId);
复制代码

       getApplication()方法返回实现服务的应用程序。 onBind()方法为在同一设备上运行的外部应用程序提供一个接口来与服务通信,此方法在远程服务中特别重要。onConfigurationChanged();支持服务在设备配置更改时重新配置自身。
   
       系统在首次创建服务时调用onCreate(),然后才调用onStart()。此过程类似创建一个Activity的过程,在启动时执行一次初始化。例如,如果创建后台线程,可以在onCreate()方法中进行,在确保在onDestroy()中停止该线程。系统调用 onCreate(),然后调用 onStart(), 最后在关闭服务时调用 onDestroy()。onDestroy()方法为服务提供了一种机制,在关闭之前执行最后的清理。

       请注意,onStart()、onCreate()、和 onDestroy()都是由系统调用的,不应该直接调用它们。而且,如果在服务类中重写任何on*()方法,请确保从这些方法调用了超类的方法版本。stopSelf()的各种版本为应用程序提供了一种机制来停止服务。客户端也可以调用 Context.stopService()来停止服务。

       Android支持服务的概念有2个原因。第一,简化后台任务的实施,这种服务也就是本地服务;第二,在同一设备上运行的应用程序之间执行进程通信,这种服务也就是远程服务。本地服务与远程服务的一些重要区别。具体来讲,如果服务完全只供同一进程中的组件使用(以运行后台任务),那么客户端必须调用 Context.startService()来启动该服务。这种类型的服务为本地服务,因为它的一般用途是运行承载服务的应用程序的后台任务。如果服务支持onBind()方法,那么它属于远程服务,可通过进程间通信 (Context.bindService())进程调用。我们也将远程服务称为AIDL 支持服务,因为客户端使用 AIDL 与服务通信。
   
       尽管 android.app.Service接口同时支持本地服务和远程服务,但并不建议提供一种服务的实现同时支持两种类型。因为每种服务的类型都有预定义的生命周期,将两种服务合并在一起可能导致错误(尽管允许这么做)。

       说明:Android中的第二种服务类型有多种叫法:远程服务、AIDL服务、外部服务和RPC服务。这些名称都是指向同一类型的服务----------可供在设备上运行的其他应用程序远程访问的服务。


       本地服务
       本地服务由Context.startService()启动。启动以后,这些类型的服务将持续运行,直到客户端调用服务的 Context.stopService()或者服务自己调用 stopSelf()。请注意,当调用Context.startService()时,系统将实例化服务并调用服务的 onStart()方法。请记住,如果在服务启动之后(也就是服务运行时)调用 Context.startService()不会为服务创建另一个实例,但这样做将调用服务的 onStart()方法。下面给出了两个本地服务的实例。


     一:定期在网络(比如Intent)上检索数据的服务 (用于上传或下载信息)。
     二:任务执行程序服务,让应用程序的活动提交作业并对它们进行排队以供处理。


       下面这个例子是一一个本地服务的例子 这里例子是 我们在服务的不同状态,如服务创建、服务启动、服务停止等 定义一个消息 通过状态栏来提醒用户 后台服务的不同状态。
       运行效果如下










Java代码:

  1. package eoe.Demo;

  2. import android.app.Notification;
  3. import android.app.NotificationManager;
  4. import android.app.PendingIntent;
  5. import android.app.Service;
  6. import android.content.Intent;
  7. import android.os.IBinder;

  8. public class BackgroundService extends Service {

  9. private NotificationManager notificationMgr;

  10. @Override
  11. public void onCreate() {
  12. super.onCreate();
  13. notificationMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

  14. displayNotificationMessage("starting Background Service");

  15. Thread thr = new Thread(null, new ServiceWorker(), "BackgroundSercie");
  16. thr.start();
  17. }

  18. @Override
  19. public IBinder onBind(Intent intent) {
  20. return null;
  21. }

  22. class ServiceWorker implements Runnable {
  23. @Override
  24. public void run() {
  25. }
  26. }

  27. @Override
  28. public void onDestroy() {
  29. displayNotificationMessage("stopping Background Service");
  30. super.onDestroy();
  31. }

  32. @Override
  33. public void onStart(Intent intent, int startId) {
  34. super.onStart(intent, startId);
  35. }

  36. private void displayNotificationMessage(String message) {
  37. Notification notification = new Notification(R.drawable.icon, message,
  38. System.currentTimeMillis());

  39. PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, Activity01.class), 0);

  40. notification.setLatestEventInfo(this, "Background Service", message, contentIntent);

  41. notificationMgr.notify(R.id.app_notification_id, notification);
  42. }
  43. }
复制代码
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值