Android开发-Service

一、简介

service是android应用4种组件之一,是没有界面的在后台运行的服务。

service有两种形式

1. 启动的(Started)

通过应用组件的接口startService()启动,服务启动后独立运行。

2. 绑定的(Bound)

通过应用组件的接口bindService()启动,提供CS方式的进程间通讯,在所有绑定的组件解除绑定后,服务退出。

这两种形式可以单独也可以同时存在于一个service上。

注意:

默认service运行在主线程中,并没有独立线程、进程专门运行service。为了防止ANR,应专门启动一个线程运行service

对于有界面的应用,且后台只在界面运行时运行的,建议使用Thread或者AsyncTaskHandlerThread

为了防止其他应用访问服务,可以在AndroidManifest.xml定义service为private

二、service的生命周期

当内存不足时,service会被杀死,并且运行时间越久越容易被杀死。当service运行在前台,则几乎不被杀死。

三、使用

1. 声明服务

在AndroidManfest.xml中添加

<manifest ... >
  ...
  <application ... >
      <service android:enabled=["true" | "false"]      // 是否被系统枚举?
         android:exported=["true" | "false"]           // false表示只有相同userID的应用可以访问。
         android:icon="drawable resource"
         android:label="string resource"
         android:name="string"                         // 唯一的必须属性
         android:permission="string"                   // 其他组件调用startService、bindService、stopService时需要添加该权限
         android:process="string" >                    // 运行的进程名,":"开始表示应用私有。
              <intent-filter>
              . . .
             <meta-data>
              . . .
      </service>
      ...
  </application>
</manifest>

2、扩展IntentService

IntentService是Service的封装,它把多个Intent放在一个队列里,一个一个处理。应用开发者只需要实现构造函数及onHandleIntent()即可。

或者使用扩展Service。

3、扩展Service

当需要多线程处理Intent来加强实时性时,

public class HelloService extends Service {
  private Looper mServiceLooper;
  private ServiceHandler mServiceHandler;

  // Handler that receives messages from the thread
  private final class ServiceHandler extends Handler {
      public ServiceHandler(Looper looper) {
          super(looper);
      }
      @Override
      public void handleMessage(Message msg) {
          // Normally we would do some work here, like download a file.
          // For our sample, we just sleep for 5 seconds.
          long endTime = System.currentTimeMillis() + 5*1000;
          while (System.currentTimeMillis() < endTime) {
              synchronized (this) {
                  try {
                      wait(endTime - System.currentTimeMillis());
                  } catch (Exception e) {
                  }
              }
          }
          // Stop the service using the startId, so that we don't stop
          // the service in the middle of handling another job
          stopSelf(msg.arg1);
      }
  }

  @Override
  public void onCreate() {
    // Start up the thread running the service.  Note that we create a
    // separate thread because the service normally runs in the process's
    // main thread, which we don't want to block.  We also make it
    // background priority so CPU-intensive work will not disrupt our UI.
    HandlerThread thread = new HandlerThread("ServiceStartArguments",
            Process.THREAD_PRIORITY_BACKGROUND);
    thread.start();

    // Get the HandlerThread's Looper and use it for our Handler
    mServiceLooper = thread.getLooper();
    mServiceHandler = new ServiceHandler(mServiceLooper);
  }

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
      Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();

      // For each start request, send a message to start a job and deliver the
      // start ID so we know which request we're stopping when we finish the job
      Message msg = mServiceHandler.obtainMessage();
      msg.arg1 = startId;
      mServiceHandler.sendMessage(msg);

      // If we get killed, after returning from here, restart
      return START_STICKY;
  }

  @Override
  public IBinder onBind(Intent intent) {
      // We don't provide binding, so return null
      return null;
  }

  @Override
  public void onDestroy() {
    Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show();
  }
}

其中onStartCommand() 返回以下3个值之一

START_NOT_STICKY:表示服务被杀死后不再重新生成
START_STICKY:服务被杀死后重启,但 Intent为空,除非有未完成的Intent.
START_REDELIVER_INTENT:服务被杀死后重启,且发送最后一次的Intent。

4.启动服务

Intent intent = new Intent(this, HelloService.class);
startService(intent);

5. 停止服务

服务自己stopService()

或者其他组件调用stopService()

四、前台Service

前台服务通过在服务生命周期内有不可消除的通知来达到。

1. 启用前台服务

Notification notification = new Notification(R.drawable.icon, getText(R.string.ticker_text),
        System.currentTimeMillis());
Intent notificationIntent = new Intent(this, ExampleActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, getText(R.string.notification_title),
        getText(R.string.notification_message), pendingIntent);
startForeground(ONGOING_NOTIFICATION, notification);

2. 停止前台服务

通过调用stopForeground(true)

posted @ 2012-08-12 16:42  silencer  阅读(236)  评论(0编辑  收藏  举报