安卓四大组件———Service

1.什么是Service(服务)

Service是一种后台运行的应用组件。

2.Service有哪些应用场景

下载任务,qq消息缓存,音乐播放器的播放

3.startService方式启动Service怎么做()

首先新建一个类继承Service ,实现它的onBind方法,其中可以用onCreate(),onStartCommand()和onDestroy()方法。例如

package com.example.demo3;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.Nullable;

public class Demo extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
//创建时服务时调用,只执行一次
    @Override
    public void onCreate() {
        super.onCreate();
    }
//服务需要做的事再里面实现。一旦执行此方法,服务即会启动并可在后台无限期运行。启动子线程会并列进行    
@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        System.out.println("执行onStartCommane方法***");
        return super.onStartCommand(intent, flags, startId);
    }

//Servicce销毁时调用
    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}

所以用onStartCommand()Service的生命周期为
onCreate()到startInstrumentation()再到onDestroy()
启动时需要在mianfest里面注册

<service android:name=".DemoService"/>

在主线程中启动

Intent intent=new Intent(this,DemoService.class);
startService(intent);

4.bindService方式启动Service怎么做

bind时绑定的意思,绑定完想要结束进程需要解绑 unbindService(得到的ServiceConnection 对象);。第一次执行bindService时,onCreate和onBind方法会被调用,但是多次执行bindService时,onCreate和onBind方法并不会被多次调用,即并不会多次创建服务和绑定服务。
先新建一个MyBinder继承Binder,新建Mybind对象,在onbind方法中返回Mybind对象,
在Mybind里面获得当前service对象

package com.example.demo3;

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

public class HomeService extends Service {
    private int count=0;
    private MyBinder binder=new MyBinder();
    private boolean stopFlag;

    public class MyBinder extends Binder {

        public Service getBinder(){
            return HomeService.this;
        }
    }
    public IBinder onBind(Intent intent){
        System.out.println("执行onBind方法***");
        return binder;
    }

    public boolean onUnbind(Intent intent){
        System.out.println("执行onUnBind方法***");
        return true;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        System.out.println("执行onCreate方法***");
           }
    public void ff(){
        new Thread(new MyThread()).start();

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        System.out.println("执行onStartCommane方法***");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onRebind(Intent intent) {
        super.onRebind(intent);
        System.out.println("执行onRebind方法***");
    }
    class MyThread extends Thread{
        @Override
        public void run() {
            while (!stopFlag){
                try {
                    Thread.sleep(1000);
                    System.out.println(Thread.currentThread().getName()+"---"+Thread.currentThread().getName());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                count++;
                System.out.println(count+"");
            }
        }
    }
}

在主线程中新建ServiceConnection 对象,实现他的onServiceConnected和onServiceDisconnected方法,用onServiceConnected得到的第二给参数调用Mybinder方法得到当前的服务,然后直接调用Service中的方法

package com.example.demo3;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Binder;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Main2Activity extends AppCompatActivity {
    private Button button;
    private Button button2;
    private Intent intent;
    private ServiceConnection connection=new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            System.out.println("onServiceConnected");
            HomeService homeService= (HomeService) ((HomeService.MyBinder) service).getBinder();
            homeService.ff();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            System.out.println("onServiceConnected");
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        button=findViewById(R.id.start);
        button2=findViewById(R.id.stop);
        intent=new Intent(this,HomeService.class);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bindService(intent,connection,Service.BIND_AUTO_CREATE) ;}
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                unbindService(connection);
            }
        });
    }
}

用bindService方式启动Service的上面周期为
onCreate()到onBind到onUnbind到onDestroy;

5.IntentSevice和Service的区别

1.只能通过startService方式启动
2.子线程排队进行
3.注册时必须写上无参的构造方法,super必须传递一个String类型的变量,String最好为当前类的名字
4.继承于IntentSevice
一般用来购买火车票,外卖等排队操作。
其他和Service一样。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值