关于android:Android基础知识梳理四大组件之Service

5次阅读

共计 2834 个字符,预计需要花费 8 分钟才能阅读完成。

定义

Service 是一种可在后盾执行长时间运行操作而不提供界面的利用组件。服务可由其余利用组件启动,而且即便用户切换到其余利用,服务仍将在后盾持续运行。此外,组件可通过绑定到服务与之进行交互,甚至是执行过程间通信 (IPC)。例如,服务可在后盾解决网络事务、播放音乐,执行文件 I/O 或与内容提供程序进行交互。

服务类型

  • 前台服务

    前台服务必须显示告诉,个别用于执行一些须要用户留神的操作。例如音频播放器应用前台服务来播放音乐。

  • 后盾服务

    后盾服务用于执行用户不会间接留神的操作。通过 startService()启动

  • 绑定服务

    绑定服务会提供客户端 - 服务器接口,以便组件与服务进行交互、发送申请、接管后果,甚至是利用过程间通信 (IPC) 跨过程执行这些操作。仅当与另一个利用组件绑定时,绑定服务才会运行。多个组件可同时绑定到该服务,但全副勾销绑定后,该服务即会被销毁。

创立 Service

1. 全局配置文件申明

<manifest ... >
  ...
  <application ... >
      <service android:name=".ExampleService"
               android:exported="false"/>
      ...
  </application>
</manifest>

增加 \<service> 标签并应用 android:name 指定 service 实现类,android:exported置为 false 能够确保以后 service 只被以后利用应用,如果其余利用也要应用到此 service,则应该置为 true。

2. 代码实现

public class ExampleService extends Service {public ExampleService() { }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {return START_STICKY;}

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}
  • onStartCommand():每次调用 startService() 接口启动服务时,都会触发服务的该回调办法
  • onBind():定义与客户端交互的接口并返回用于通信的 IBinder 实例,能够通过扩大 Binder 类、应用 Messenger、应用 AIDL 三种形式定义

运行 Service

1. 启动服务

// 启动服务
private void startLocalService() {Intent intent = new Intent(this, ExampleService.class);
    startService(intent);
}

// 进行服务
private void stopLocalService() {Intent intent = new Intent(this, ExampleService.class);
    stopService(intent);
}

2. 前台服务

定义 notification,运行前台服务

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(ONGOING_NOTIFICATION_ID, notification);

3. 绑定服务

绑定服务须要应用 ServiceConnection 来监听服务的连贯状态,并在连贯胜利的回调办法 onServiceConnected() 中获取到能够用以与服务通信的 Binder 实例

// 监听服务绑定状态
private ServiceConnection serviceConnection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) { }

    @Override
    public void onServiceDisconnected(ComponentName name) {}};

// 绑定服务
private void bindLocalService() {Intent intent = new Intent(this, ExampleService.class);
    bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
}

// 解绑服务
private void unbindLocalService() {unbindService(serviceConnection);
}

生命周期

对于不同的服务启动形式,服务的生命周期也有所不同。如上图,右边时通过 startService() 启动服务的生命周期,左边时通过 bindService() 启动服务的生命周期。

如果是 bindService() 形式启动服务,当 onUnbind() 返回 true,客户端再次 bindService() 时会触发到 onRebind() 回调

  • onCreate()

    首次创立服务时,会调用该办法,如果服务已在运行,则不会被调用,该办法在服务的整个生命周期中只会被调用一次。

  • onStartCommand()

    当另一个组件通过 startService() 办法启动服务时,会调用到该办法

  • onBind()

    当另一个组件通过 bindService() 办法绑定服务时,会调用到该办法

  • onUnbind()

    当另一个组件通过 unbindSevice()办法解绑服务时,会调用到该办法

  • onRebind()

    当旧的组件已与服务解绑后,新的组件绑定服务时,如果 onUnbind() 返回 true,则会调用该办法

  • onDestory()

    当绑定服务的所有组件均已解绑或者服务进行时,会调用到该办法

正文完
 0