Android-Service-服务的基本用法

什么是Service


Service是Android中实现程序后台运行的解决方案,它非常适合执行那些不需要和用户交互而
且还要求长期运行的任务。Service的运行不依赖于任何用户界面,即使程序被切换到后台,或
者用户打开了另外一个应用程序,Service仍然能够保持正常运行。
注意,Service并不是运行在一个独立的进程当中的,而是依赖于创建Service时所在的应用程序进程。当某个应用程序进程被杀掉时,所有依赖于该进程的Service也会停止运行。另外,也不要被Service的后台概念所迷惑,实际上Service并不会自动开启线程,所有的代码都是默认运行在主线程当中的。也就是说,我们需要在Service的内部手动创建子线程,并在这里执行具体的任务,否则就有可能出现主线程被阻塞的情况。

定义一个Service


新建一个ServiceTest项目,新建一个Service,com.example.servicetest -> New -> Service -> Service,继承自Service,命名为MyService,内容如下:

MyService.java

package com.example.servicetest;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;


public class MyService extends Service {
    private static final String TAG = "MyService";

    public MyService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "onCreate: MyService created successfully");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "onStartCommand: MyService started");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy: MyService was destroyed");
    }
}
  • onBinder():是Service中唯一一个抽象方法,必须在子类实现。
  • onCreate(): 服务创建的时候调用
  • onStartCommand(): 服务启动的时候调用
  • onDestroy():服务销毁的时候调用

此外还需要在 AndroidManifest.xml 注册:

以上,一个简单的服务已经定义好了。

启动和停止服务


启动和停止服务都需要借助Intent对象来实现。

修改 activity_main.xml 中的代码,添加两个按钮,分别用于启动和停止服务:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/start_service"
        android:text="Start Service"
        android:textAllCaps="false"
        android:layout_marginLeft="40dp"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/stop_service"
        android:text="Stop Service"
        android:textAllCaps="false"
        android:layout_marginLeft="40dp"
        />

</LinearLayout>

修改 MainActivity.java : 

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button buttonStartService = findViewById(R.id.start_service);
        Button buttonStopService = findViewById(R.id.stop_service);
        buttonStartService.setOnClickListener(this);
        buttonStopService.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(this, MyService.class);
        switch (v.getId()) {
            case R.id.start_service:
                startService(intent);
                break;
            case R.id.stop_service:
                stopService(intent);
                break;
            default:
                break;
        }
    }
}

分别给按钮注册了点击事件。Start Service的点击事件中,构建了一个Intent对象,并调用startService()方法来启动MyService这个服务。Stop Service中同样构建了一个Intent对象并调用stopService()方法停止服务。startService以及stopService方法都是定义在Context类中,因此可以在Activity中直接调用。如果没有点击StopService, 服务会一直处于运行状态。

点击按钮,观察打印如下:

onCreate只有在第一次创建服务时会调用,onStartCommand会在每次启动服务时都调用。

活动和服务之间的通信


如何让活动和服务之间的关系更为紧密?

在Service里面提供一个下载功能,在活动中可以决定何时开始下载,以及查看下载进度。实现该功能的思路是建立一个专门的Binder对象来进行管理。

修改 MyService 中的代码:

package com.example.servicetest;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;


public class MyService extends Service {
    private static final String TAG = "MyService";

    private DownloadBinder downloadBinder = new DownloadBinder();

    //DownloadBinder类继承Binder,提供下载方法和查看进度的方法
    class DownloadBinder extends Binder {
        public void startDownload() {
            Log.d(TAG, "startDownload: executed");
        }
        
        public int getProcess() {
            Log.d(TAG, "getProcess: executed");
            return 0; 
        }
    }

    public MyService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        return downloadBinder;
    }

    ...

}

新建了DownloadBinder类继承Binder,然后在它的内部提供了下载方法和查看进度的方法;然后创建了DownloadBinder对象,并在onBind()方法中返回了该对象,到这列MyService的工作全部完成了。

修改activity_main.xml 中的代码,新增了两个按钮分别用于绑定以及取消绑定服务,如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    ...
    ...

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/bind_service"
        android:text="Bind Service"
        android:textAllCaps="false"
        android:layout_marginLeft="40dp"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/unbind_service"
        android:text="Unbind Service"
        android:textAllCaps="false"
        android:layout_marginLeft="40dp"
        />

</LinearLayout>

修改MainActivity.java,

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private MyService.DownloadBinder downloadBinder;
    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            downloadBinder = (MyService.DownloadBinder) service;
            downloadBinder.startDownload();
            downloadBinder.getProcess();
        }

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


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button buttonStartService = findViewById(R.id.start_service);
        Button buttonStopService = findViewById(R.id.stop_service);
        Button buttonBindService = findViewById(R.id.bind_service);
        Button buttonUnBindService = findViewById(R.id.unbind_service);
        buttonStartService.setOnClickListener(this);
        buttonStopService.setOnClickListener(this);
        buttonBindService.setOnClickListener(this);
        buttonUnBindService.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(this, MyService.class);
        switch (v.getId()) {
            case R.id.start_service:
                startService(intent);
                break;
            case R.id.stop_service:
                stopService(intent);
                break;
            case R.id.bind_service:
                bindService(intent, connection, BIND_AUTO_CREATE);
                break;
            case R.id.unbind_service:
                unbindService(connection);
                break;
            default:
                break;
        }
    }
}

先创建了一个ServiceConnection的匿名对象,在里面重写了onServiceConnected()方法以及onServiceDisconnected()方法,它们分别会在活动与服务绑定或者断开的时候调用。在onServiceConnected()中通过向下转型得到DownloadBinder的实例,通过该实例使得活动和服务间的关系变得更加紧密,在活动中现在可以调用DownloadBinder中的任何public方法。

bindService()方法接收三个参数,第一个接收intent对象,第二个接收一个ServiceConnection对象,第三个是一个标志位,这里传入BIND_AUTO_CREATE,表示在服务和活动绑定后会自动创建服务。这使得MyService中的onCreate()方法执行而onStartCommand()方法不会执行。unbindService()接收一个ServiceConnection实例,该方法用于解除活动和服务之间的绑定。

点击Bind Service和Unbind Service按钮,查看日志打印如下:

onCreate()方法得到执行,然后startDownload()和getProcess()方法都得到执行,说明确实在活动里成功调用了服务里提供的方法。

此外需要注意,任何一个Service在整个Application里面都是通用的,既不仅可以和MainActivity活动绑定,也可以和任何一个其他的任何活动进行绑定,而且绑定之后他们都可以获取到相同的DownloadBind实例。

服务的生命周期

在项目的任何位置调用了Context的startService()方法,相应的服务就会启动,并回调onStartCommand()方法。如果该服务还未创建,onCreate()方法会先于onStartCommand()方法执行。服务启动之后会一直运行,直到stopService()或stopSelf()方法被调用,或者被系统回收。

每调用一次startService()方法,onStartCommand()就会执行一次,但实际上每个Service只会存在
一个实例。所以不管你调用了多少次startService()方法,只需调用一次stopService()或stopSelf()方法,Service就会停止。

调用Context的bindService()来获取一个Service的持久连接,这时就会回调Service中的onBind()方法。类似地,如果这个Service之前还没有创建过,onCreate()方法会先于onBind()方法执行。之后,调用方可以获取到onBind()方法里返回的IBinder对象的实例,这样就能自由地和Service进行通信了。只要调用方和Service之间的连接没有断开,Service就会一直保持运行状态,直到被系统回收。

当调用了startService()方法后,再去调用stopService()方法,这时Service中的onDestroy()方法就会执行,表示Service已经销毁。当调用了bindService()方法,再去调用unbindService()法,onDestroy()方法也会执行。但是完全有可能对一个Service既调用了startService()方法,又调用了bindService()方法的这种情况下要同时调用stopService()和unbindService()方法,onDestroy()方法才会执行。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值