博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android-Srvice服务
阅读量:3943 次
发布时间:2019-05-24

本文共 7001 字,大约阅读时间需要 23 分钟。

7、Service

7.1 Service初探

  • A is an application component that can perform long-running operations in the background and does not provide a user interface.

服务是一个应用程序组件,它可以在后台执行长时间运行的操作,而不提供用户界面。

  • A Service is not a separate process.
  • A Service is not a thread.
  • Two Forms
    • Started
    • Bound

7.2 Service的生命周期

Start :

Android的应用程序组件,如活动,通过startService()启动了服务,则服务是Started状态。一旦启动,服务可以在后台无限期运行,即使启动它的组件已经被销毁。

7.3 使用Service做一个播放音乐的实例

  • 7.3.1 新建一个TestService——Module

  • 7.3.2 新建一个MusicService类

    • 在 res下新建一个 raw文件夹,选取一个 mp3文件存放在 res/raw文件夹中

      image.png

      .mp3 文件名称必须是小写!小写!小写!

    public class MusicService extends Service {    private static final String TAG = MusicService.class.getSimpleName();    private MediaPlayer mediaPlayer; // 创建 MediaPlayer对象    @Override    public void onCreate() {        super.onCreate();        Log.i(TAG, "onCreate");        // 初始化 MediaPlayer        mediaPlayer = MediaPlayer.create(this,R.raw.tianhou);    }    @Override    public int onStartCommand(Intent intent, int flags, int startId) {        Log.i(TAG, "onStartCommand");        mediaPlayer.start();        return START_NOT_STICKY;    }    @Override    public void onDestroy() {        Log.i(TAG, "onDestroy");        mediaPlayer.stop();        super.onDestroy();    }    @Nullable    @Override    public IBinder onBind(Intent intent) {        return null;    }}

    **START_NOT_STICKY 😗*这种模式对于那些想通过启动来完成一些工作的事情来说是有意义的,但是在内存压力下可以停止,并且以后会显式地重新启动自己来完成更多的工作。

    android-START_NOT_STICKY.png


  • 7.3.3 新建一个activity_music.xml布局文件,并创建MusicButtonActivity类

    public class MusicButtonActivity extends AppCompatActivity implements View.OnClickListener {
    private Button mStartButton; private Button mStopButton; @Override protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); setContentView(R.layout.activity_music); mStartButton = findViewById(R.id.startBtn); mStopButton = findViewById(R.id.stopBtn); mStartButton.setOnClickListener(this); mStopButton.setOnClickListener(this); } @Override public void onClick(View v) {
    switch (v.getId()) {
    case R.id.startBtn: // 启动服务 startService(new Intent(MusicButtonActivity.this, MusicService.class)); break; case R.id.stopBtn: // 关闭服务 stopService(new Intent(MusicButtonActivity.this, MusicService.class)); break; } }}
  • 7.3.4 在activity_main.xml布局文件中添加一个Button按钮并设置点击事件

    public class MainActivity extends AppCompatActivity {
    @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button = findViewById(R.id.musicBtn_service); button.setOnClickListener(new View.OnClickListener() {
    @Override public void onClick(View v) {
    Intent intent = new Intent(MainActivity.this, MusicButtonActivity.class); startActivity(intent); } }); }}
  • 7.3.5 在AndroidManifest.xml文件中添加 MusicButtonActivity和 MusicService

  • 7.3.6 效果预览 [动态图片听不见音乐]

    android-service.gif


Bind :

当Android的应用程序组件通过bindService()绑定了服务,则服务是Bound状态。Bound状态的服务提供了一个客户服务器接口来允许组件与服务进行交互,如发送请求,获取结果,甚至通过IPC来进行跨进程通信。
  • 7.3.3 创建ServiceConnection并实现接口中的方法

    public class MusicButtonActivity extends AppCompatActivity implements View.OnClickListener {
    private Button mStartButton; private Button mStopButton; private MusicService musicService; // 实现 ServiceConnection private ServiceConnection mServiceConnection = new ServiceConnection() {
    /** * onServiceConnected(ComponentName name, IBinder service): IBinder是从 MusicService中的 onBind传递过来的, * 而 MusicService中的 onBind则是内部类 LocalBinder中创建的。LocalBinder中的方法用于获取当前的 Service。 * 所以这个 LocalBinder是一个桥梁,是沟通 Service 以及这个 Activity的桥梁,在这里可以存储一些数据,所以这时候 * 就可以把 MusicService得到。 * * @param name * @param service */ @Override public void onServiceConnected(ComponentName name, IBinder service) {
    MusicService.LocalBinder localBinder = (MusicService.LocalBinder) service; musicService = localBinder.getService(); } @Override public void onServiceDisconnected(ComponentName name) {
    } }; @Override protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); setContentView(R.layout.activity_music); mStartButton = findViewById(R.id.startBtn); mStopButton = findViewById(R.id.stopBtn); mStartButton.setOnClickListener(this); mStopButton.setOnClickListener(this); } @Override public void onClick(View v) {
    switch (v.getId()) {
    case R.id.startBtn: // 绑定服务 bindService(new Intent(MusicButtonActivity.this, MusicService.class), mServiceConnection, BIND_AUTO_CREATE); break; case R.id.stopBtn: // 解绑服务 unbindService(mServiceConnection); break; } }}
  • 7.3.2 通过 LocalBinder获取 MusicService

    public class MusicService extends Service {
    private static final String TAG = MusicService.class.getSimpleName(); private MediaPlayer mediaPlayer; // 创建 MediaPlayer对象 private IBinder mBinder = new LocalBinder(); @Override public void onCreate() {
    super.onCreate(); Log.i(TAG, "onCreate"); // 初始化 MediaPlayer mediaPlayer = MediaPlayer.create(this,R.raw.tianhou); } @Override public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i(TAG, "onStartCommand"); mediaPlayer.start(); return START_NOT_STICKY; } @Override public void onDestroy() {
    Log.i(TAG, "onDestroy"); mediaPlayer.stop(); super.onDestroy(); } @Nullable @Override public IBinder onBind(Intent intent) {
    return mBinder; } public class LocalBinder extends Binder {
    MusicService getService() {
    return MusicService.this; } } // 模拟数据 public int getMusicPlayProgress() {
    return 18; }}

image-20200312193324815.png

First, go to bindService and pass in the connection, which will be passed to the service in LocalBinder.getService(), and then the service will pass back the mBinder of onBind(), that is to say, the connection just passed in will be passed in The IBinder in onServiceConnected() can receive the LocalBinder, and a getService() method is constructed in LocalBinder to return the current service. Through this method, we can get the service. Some public methods can be created in MusicService, the data can be obtained through the methods in MusicService object. At this time, MusicButtonActivity and MusicService can communicate.


什么场景下使用IntentService和Service

  • 当你需要把你的任务按队列来分配时就用IntentService;只需要后台服务的用service

转载地址:http://kkiwi.baihongyu.com/

你可能感兴趣的文章
使用Maven Archetype生成工程报错的解决
查看>>
System.getProperty()系统参数
查看>>
Linux系统下批量删除.svn目录
查看>>
大数据行业应用趋势
查看>>
Mac + Rails3 + MongoDB的Demo工程搭建
查看>>
隐藏于Python内的设计之禅彩蛋
查看>>
VSCode配置C/C++环境
查看>>
OTB测试之Visual Tracker Benchmark v1.0全过程配置流程
查看>>
缓存在Springboot应用中的使用
查看>>
Linux(一)- 认识Linux
查看>>
Linux(二)- Linux常用命令
查看>>
Linux(三)- Java开发环境搭建
查看>>
Linux(四)- Ubuntu安装Mysql
查看>>
Ubuntu安装开发环境
查看>>
Deepin开发环境安装
查看>>
Spring入门
查看>>
网址大全
查看>>
Git的使用
查看>>
Linux域名IP映射
查看>>
Java的反射机制
查看>>