android-学习篇-Service(服务)

简介

Service 是一种可在后台执行长时间运行操作而不提供界面的应用组件。

Service 两种工作状态:

  • 启动状态,主要用于执行后台计算。startService()
  • 绑定状态,主要用于其他组件和 Service 的交互。bindService()

Service

Service 是适用于所有服务的基类。扩展此类时,您必须创建用于执行所有服务工作的新线程,因为服务默认使用应用的主线程,这会降低应用正在运行的任何 Activity 的性能。

package android.app;
public abstract class Service extends ContextWrapper implements ComponentCallbacks2

方法介绍:

// 首次创建服务时,系统会(在调用 onStartCommand() 或 onBind() 之前)调用此方法来执行一次性设置程序。如果服务已在运行,则不会调用此方法。
public void onCreate() 
// 当不再使用服务且准备将其销毁时,系统会调用此方法。服务应通过实现此方法来清理任何资源,如线程、注册的侦听器、接收器等。
public void onDestroy()

// 当另一个组件(如 Activity)请求启动服务时,系统会通过调用 startService() 来调用此方法。
int onStartCommand(Intent intent, @StartArgFlags int flags, int startId)
// 当另一个组件想要与服务绑定(例如执行 RPC)时,系统会通过调用 bindService() 来调用此方法。
public abstract IBinder onBind(Intent intent);

// 停止服务。此外,其他组件也可通过调用 stopService() 来停止此服务
public final void stopSelf()

// 开启前台服务
public final void startForeground(int id, Notification notification)
// 从前台移除服务。此方法采用布尔值,指示是否需同时移除状态栏通知。此方法不会停止服务。但是,如果您在服务仍运行于前台时将其停止,则通知也会随之移除。
public final void stopForeground(boolean removeNotification)

生命周期

public class ExampleService extends Service {
    int startMode;       // indicates how to behave if the service is killed
    IBinder binder;      // interface for clients that bind
    boolean allowRebind; // indicates whether onRebind should be used

    @Override
    public void onCreate() {
        // The service is being created
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // The service is starting, due to a call to startService()
        return mStartMode;
    }
    @Override
    public IBinder onBind(Intent intent) {
        // A client is binding to the service with bindService()
        return mBinder;
    }
    @Override
    public boolean onUnbind(Intent intent) {
        // All clients have unbound with unbindService()
        return mAllowRebind;
    }
    @Override
    public void onRebind(Intent intent) {
        // A client is binding to the service with bindService(),
        // after onUnbind() has already been called
    }
    @Override
    public void onDestroy() {
        // The service is no longer used and is being destroyed
    }
}

无论所有服务是通过 startService() 还是 bindService() 创建,系统均会为其调用 onCreate() 和 onDestroy() 方法。

启动与绑定方法

package android.content;
public class ContextWrapper extends Context 

public ComponentName startService(Intent service)
public boolean bindService(Intent service, ServiceConnection conn, int flags)

创建 Service

  1. 自定义类继承 Service
  2. 清单文件中注册服务

IntentService

IntentService 是 Service 的子类,其使用工作线程逐一处理所有启动请求。如果您不要求服务同时处理多个请求,此类为最佳选择。实现 onHandleIntent(),该方法会接收每个启动请求的 Intent,以便您执行后台工作。

前台服务

前台服务是用户主动意识到的一种服务,因此在内存不足时,系统也不会考虑将其终止。前台服务必须为状态栏提供通知,将其放在运行中的标题下方。这意味着除非将服务停止或从前台移除,否则不能清除该通知。

Intent notificationIntent = new Intent(this, ExampleActivity.class);
PendingIntent pendingIntent =
        PendingIntent.getActivity(this, 0, notificationIntent, 0);

Notification notification =
          new Notification.Builder(this, CHANNEL_DEFAULT_IMPORTANCE)
    .setContentTitle(getText(R.string.notification_title))
    .setContentText(getText(R.string.notification_message))
    .setSmallIcon(R.drawable.icon)
    .setContentIntent(pendingIntent)
    .setTicker(getText(R.string.ticker_text))
    .build();

// 注意:提供给 startForeground() 的整型 ID 不得为 0。
startForeground(ONGOING_NOTIFICATION_ID, notification);

参考

猜你喜欢

转载自blog.csdn.net/u010019244/article/details/106173910
今日推荐