分享

android 服务概述 本地服务示例

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

Android 中的服务
通过查看 android.app.Service的公共方法,我们可以更深入地理解服务的概念。
Application getApplication();
Abstract IBinder onBind(Intent intent);
void onConfigurationChanged(Configuration newConfig);
void onCreate();
void onDestroy();
void onLowMemory();
void onRebind(Intent intent);
void onStart(Intent intent, int startId);
boolean onUnbind(Intent intent);
final void setForeground(Boolean isForeground);
final void stopSelf();
final void stopSelf(int startId);
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)上检索数据的服务 (用于上传或下载信息)。
     二:任务执行程序服务,让应用程序的活动提交作业并对它们进行排队以供处理。

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







先来看看我们的Service类
BackgroundService.java
Java代码  收藏代码
  1. package xiaohang.zhimeng;  
  2.   
  3. import android.app.Notification;  
  4. import android.app.NotificationManager;  
  5. import android.app.PendingIntent;  
  6. import android.app.Service;  
  7. import android.content.Intent;  
  8. import android.os.IBinder;  
  9.   
  10. public class BackgroundService extends Service {  
  11.   
  12.     private NotificationManager notificationMgr;  
  13.   
  14.     @Override  
  15.     public void onCreate() {  
  16.         super.onCreate();  
  17.         notificationMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);  
  18.   
  19.         displayNotificationMessage("starting Background Service");  
  20.   
  21.         Thread thr = new Thread(nullnew ServiceWorker(), "BackgroundSercie");  
  22.         thr.start();  
  23.     }  
  24.   
  25.     @Override  
  26.     public IBinder onBind(Intent intent) {  
  27.         return null;  
  28.     }  
  29.   
  30.     class ServiceWorker implements Runnable {  
  31.         @Override  
  32.         public void run() {  
  33.             // do background processing here.....  
  34.             // stop the service when done...  
  35.             // BackgroundService.this.stopSelf()  
  36.         }  
  37.     }  
  38.   
  39.     @Override  
  40.     public void onDestroy() {  
  41.         displayNotificationMessage("stopping Background Service");  
  42.         super.onDestroy();  
  43.     }  
  44.   
  45.     @Override  
  46.     public void onStart(Intent intent, int startId) {  
  47.         super.onStart(intent, startId);  
  48.     }  
  49.   
  50.     private void displayNotificationMessage(String message) {  
  51.         Notification notification = new Notification(R.drawable.icon, message,  
  52.                 System.currentTimeMillis());  
  53.   
  54.         PendingIntent contentIntent = PendingIntent.getActivity(this0,  
  55.                 new Intent(this, Activity01.class), 0);  
  56.   
  57.         notification.setLatestEventInfo(this"Background Service", message,  
  58.                 contentIntent);  
  59.   
  60.         notificationMgr.notify(R.id.app_notification_id, notification);  
  61.     }  
  62. }  


Activity01类
Java代码  收藏代码
  1. package xiaohang.zhimeng;  
  2. import android.app.Activity;  
  3. import android.content.Intent;  
  4. import android.os.Bundle;  
  5. import android.util.Log;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9.   
  10. public class Activity01 extends Activity {  
  11.   
  12.     private static final String TAG = "Activity01";  
  13.   
  14.     @Override  
  15.     public void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.main);  
  18.   
  19.         Log.d(TAG, "starting service");  
  20.   
  21.         Button bindBtn = (Button) findViewById(R.id.bindBtn);  
  22.         bindBtn.setOnClickListener(new OnClickListener() {  
  23.             @Override  
  24.             public void onClick(View v) {  
  25.                 startService(new Intent(Activity01.this,  
  26.                         BackgroundService.class));  
  27.             }  
  28.         });  
  29.   
  30.         Button unbindBtn = (Button) findViewById(R.id.unbindBtn);  
  31.         unbindBtn.setOnClickListener(new OnClickListener() {  
  32.             @Override  
  33.             public void onClick(View v) {  
  34.                 stopService(new Intent(Activity01.this, BackgroundService.class));  
  35.             }  
  36.         });  
  37.     }  
  38. }  


布局文件
Xml代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas./apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView    
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:text="@string/hello"  
  11.     />  
  12. <Button  android:id="@+id/bindBtn"  
  13.     android:layout_width="wrap_content"  
  14.     android:layout_height="wrap_content"  
  15.     android:text="Bind"/>  
  16.   
  17.     <Button android:id="@+id/unbindBtn"  
  18.         android:layout_width="wrap_content"  
  19.         android:layout_height="wrap_content"  
  20.         android:text="UnBind"/>  
  21. </LinearLayout>  


我们需要创建一个图标 并将其放在项目的drawable文件夹内,我用的项目自带的那个。需要为通知管理器NotificationManager提供一个应用程序级唯一ID(整数)。要创建唯一ID,可以向字符串资源文件 /res/values/strings.xml 添加一个项ID。当调用notify()方法时,会将唯一ID 传递给通知管理器。
Xml代码  收藏代码
  1. <item type=”id” name=”app_notification_id”/>  


最后,需要向 AndroidManifest.xml 文件添加
Xml代码  收藏代码
  1. <service android:name=”BackgroundService”/>  

标记,将其作为<application>的子标签。



     BackgroundServie是承载服务的应用程序组件使用服务的一个典型例子。换言之,运行服务的应用程序也是服务的唯一使用者。因为该服务不支持其进程外的客户端,所以它是本地服务。由于本地服务不是远程服务,所以它在bind()方法中返回null。因此,绑定此服务的唯一 方法是调用Context.startService()。本地服务的重要方法包括 onCreate()、onStart()、stop()*和onDestroy()。

     在BackgroundService的 onCreate()方法中,我们创建了一个线程来完成服务的主要工作。我们需要应用程序的主线程来处理用户界面,所以将服务的工作委派给一个辅助线程。另请注意,我们在onCreate()而不是onStart()中创建和启动线程。这样做是因为, onCreate()只会被调用一次,我们也希望在服务生命周期内只创建一次该线程。onStart()方法可以调用多次,所以它不符合此处的要求。我们在线程的run()方法实现中没有做任何事情,可以在这里执行HTTP调用、查询数据库等。

     BackgroundService还使用NotificationManager类,以在服务启动和停止时向用户发送通知。这是本地服务将信息传递回用户的一种方式。要向用户发送通知,需要调用 getSystemService(NOTIFICATION_SERVICE)来获得通知管理器。来自通知管理器的消息将显示在状态栏中。

   源码附件!

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多