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

定义

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()

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

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理