Service

Service定义
  • Service是可以长时间运行在后台的,不可见的,没有界面的组件;
  • Service运行在主线程中;
  • Service可以跨进程调用。
为什么使用Service
  • service可以放在独立的进程中,更安全。
  • 使用service可以依赖现有的binder机制不需要在应用层面上处理线程同步的繁杂工作。系统可以重新启动异常死去的service。
  • intentservice使用队列的方式将请求的队列加入到intent队列中,然后开启一个 thread 线程,对于异步的startservice请求,intentservice会处理完第一个之后处理第二个,每一个请求都会在单独的一个work thread 中处理。不会阻塞应用程序的主线程,这就给我们提供了一个思路,每一个耗时的操作都可以使用intentservice中运行
Service基本启动方式:StartService;bindService
StartService特点
  • onCreate只会执行一次,只要调用startService,onStartCommand一定会执行
  • Service运行在main线程中,做耗时操作需要例外开子线程
  • 通过Intent进行传参,在onStartCommand接收参数
  • 无法获得Service对象,直接操作Service中的属性和方法
  • 调用stopService后,Service会执行onDestory后停止
StartService优缺点

优点:使用简单,和Activity一样,只需要几行代码即可启动Service
缺点:使用startService的方式,无法获得Service对象,不能直接操作Service中的属性和方法,只能通过Intent进行传递不同参数,重复调用startService,触发onStartCommand。

StartService使用步骤
  1. 新建类继承Service
  2. 重写onCreate方法(在创建时调用的方法,可以用来做数据初始化,Service在没有停止之前,只会执行一次)
  3. 实现onBind抽象方法
  4. 重写onStartCommand(创建后会调用onStartCommand,此方法中可以接收调用者传递过来的参数,并且可以编写需要的逻辑代码,当重复调用Service时,onStartCommand会重复执行)
  5. 重写onDestroy方法 (在退出时调用,此方法中可以编写释放资源的操作)
  6. 在AndroidManifest文件中注册Service(使用标签注册)
  7. 使用startService方法启动Service
  8. 使用stopService方法停止Service
StartService代码展示

xml文件
首先要在布局文件中设置两个按钮,用来启动和停止Service

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.administrator.sharedapplication.Main3Activity">

    <Button
        android:id="@+id/start_btn"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="启动Service"/>
    <Button
        android:id="@+id/stop_btn"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="停止Service"/>

</LinearLayout>

然后在Activity文件中对按钮进行定义、绑定ID、设置监听和点击事件

package com.example.administrator.sharedapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Main3Activity extends AppCompatActivity implements View.OnClickListener{
    private Button startBtn;
    private Button stopBtn;
    private Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);

        bindID();
    }

    private void bindID() {
        startBtn=findViewById(R.id.start_btn);
        stopBtn=findViewById(R.id.stop_btn);
        startBtn.setOnClickListener(this);
        stopBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.start_btn:
                intent = new Intent(this,MyService.class);
                startService(intent);//启动service
                break;
            case R.id.stop_btn:
                stopService(intent);//停止service
                break;
                default:
                break;
        }
    }
}

最后在新建的类中写入MyService方式启动Service的步骤

package com.example.administrator.sharedapplication;

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

public class MyService extends Service{

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e("Service"+Thread.currentThread().getName(),"onCreate...");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e("Service","onStartCommand...");

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i=0;i<10;i++){
                    Log.e("Service"+Thread.currentThread().getName(),i+"***");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e("Service"+Thread.currentThread().getName(),"onDestroy...");

    }
}
bindService特点
  1. onCreate,onBind只会执行一次
  2. Service运行在main线程中,做耗时操作需另开子线程
  3. 可以直接操作Service中的属性和方法
  4. 调用unbindService后,Service会执行onUnbind,onDestroy方法后停止
bindService优缺点

优点:可以得到Service对象,灵活控制Service内部的属性和方法。
缺点:使用较为复杂

bindService使用步骤
  1. 新建类继承Service
  2. 实现onBind方法 (这里不同于startService中实现onBind方法那样简单。这里需要在此方法中返回一个Binder子类对象。)
  3. 重写onCreate方法
  4. 重写onUnBind方法 (在调用unbindService方法解除绑定后,触发此回调函数)
  5. 重写onDestroy方法
  6. 在AndroidManifest中注册Service
  7. 实现ServiceConnection接口 (这里是难点)
  8. 通过bindService绑定Service
  9. 通过unbindService解绑Service
bindService代码展示

在新建的类中写入TestService 方式启动Service的步骤

package com.example.administrator.serviceapplication;

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

/**
 * Created by Administrator on 2018/3/23.
 */

public class TestService extends Service {

    private String TAG="TestService";

    private Guanjia guanjia = new Guanjia();

    class Guanjia extends Binder{
        //作用:得到当前Service对象
        public TestService getServiceObject(){
            return TestService.this;
        }
    }
    public void fly(){
        Log.e(TAG,"开飞机");
    }
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.e(TAG,"onBind...");
        return guanjia;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.e(TAG,"onUnbind...");
        return super.onUnbind(intent);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e(TAG,"onCreate...");

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e(TAG,"onDestroy...");

    }
}
IntentService特点
  • IntentService是继承于Service并处理异步任务的一个类
  • 在IntentService内有一个工作线程来处理异步耗时操作
  • 此线程无需我们控制和维护
  • 当多次启动时,会以队列的形式,逐一执行
  • 当执行完耗时操作后,此Service会自动停止
IntentService优点

会创建独立的worker线程来处理所有的Intent请求;
会创建独立的worker线程来处理onHandleIntent()方法实现的代码,无需处理多线程问题;
所有请求处理完成后,IntentService会自动停止,无需调用stopSelf()方法停止Service;
多次调用时,按队列的顺序逐一执行,每次执行都是从创建到销毁的全部过程

IntentService和Service区别
  1. 首先IntentService是继承自Service;
  2. Service不是一个单独的进程,它和应用程序在同一个进程中;
  3. Service也不是一个线程,所以我们要避免在Service中进行耗时的操作;
  4. IntentService使用队列的方式将请求的Intent加入队列,然后开启了一个Worker Thread(工作线程)在处理队列中的Intent,对于异步的startService请求,
    IntentService会处理完成一个之后在处理第二个,每一个请求都会在一个单独的Worker Thread中处理,不会阻塞应用程序的主线程。
    因此,如果我们如果要在Service里面处理一个耗时的操作,我们可以用IntentService来代替使用。
  5. 使用IntentService 必须首先继承IntentService并实现onHandleIntent()方法,将耗时的任务放在这个方法执行,其他方面,IntentService和Service一样。
IntentService使用步骤
  • 新建类继承IntentService
  • 实现父类的构造方法
  • 重写onHandleIntent()方法
  • 重写onCreate方法
  • 重写onDesttory方法
  • 在AndroidManifest文件中注册Service(容易忘记或出错)
  • 在有Content的环境下启动Service
IntentService代码展示

xml文件
首先要在布局文件中设置两个按钮,用来开启和停止IntentService

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.administrator.serviceapplication.Main3Activity">

    <Button
        android:id="@+id/start_btn"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="开启IntentService"/>
    <Button
        android:id="@+id/stop_btn"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="停止IntentService"/>
</LinearLayout>

然后在Activity文件中对按钮进行定义、绑定ID、设置监听和点击事件

package com.example.administrator.serviceapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Main3Activity extends AppCompatActivity implements View.OnClickListener{
    private Button startBtn;
    private Button stopBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        bindID();
    }

    private void bindID() {
        startBtn=findViewById(R.id.start_btn);
        stopBtn=findViewById(R.id.stop_btn);
        startBtn.setOnClickListener(this);
        stopBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.start_btn:
                break;
            case R.id.stop_btn:
                break;
        }
    }
}

最后在新建的类中写入MyIntentService 方式启动Service的步骤

package com.example.administrator.serviceapplication;

import android.app.IntentService;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.util.Log;

/**
 * Created by Administrator on 2018/3/23.
 */

public class MyIntentService extends IntentService {
    public MyIntentService(String name) {
        super(name);
    }
    public MyIntentService(){
        super("");

    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {

        for (int i=0;i<10;i++){
            try {
                Log.e("MyIntentService",i+"***");
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值