Service的简介(本人目前理解的)

1.Service简单概述

  Service(服务)是一个一种可以在后台执行长时间运行操作而没有用户界面的应用组件。服务可由其他应用组件启动(如Activity),服务一旦被启动将在后台一直运行,即使启动服务的组件(Activity)已销毁也不受影响。 此外,组件可以绑定到服务,以与之进行交互,甚至是执行进程间通信 (IPC)。 例如,服务可以处理网络事务、播放音乐,执行文件 I/O 或与内容提供程序交互,而所有这一切均可在后台进行。
  
二.service有2种基本的启动方式:
startService():使用这种方式,来进行单一的任务,不需要返回结果给调用者
bindService():与上面的相反。

startService的优点就是使用简单,只需几行代码就能启动Service。其缺点就是使用startService无法获得Service对象,不能直接操作Service中的属性和方法,只能通过intent传递不同参数,重复调用startService,触发onStartCommand。

bindService的优点和Service的缺点有关,Service不能获得Service对象,不能直接操作Service中的属性和方法,而 bindService解决了这个问题。其缺点是操作步骤复杂些。
三.startService代码演示在我上一个博客已介绍
四.bindService的代码演示:
Myservice中代码:

package com.example.administrator.myapplicat323;

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/023.
 */

public class Myservice extends Service {

    private Downloadbinder mbinder=new Downloadbinder();
    class Downloadbinder extends Binder{
public void startdownload(){
    Log.e("MyService","hahahhahaahahah");
};
public int progress(){
    Log.e("MyService","intintitnintitnintintitnititnitnitn");
    return 0;
};

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

        return mbinder;
    }

}

MainActivity中代码:

package com.example.administrator.myapplicat323;

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

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Myservice.Downloadbinder downloadbinder;
    private Button bind;
    private Button unbind;
    private Button tiao;
    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            downloadbinder = (Myservice.Downloadbinder) iBinder;
            downloadbinder.startdownload();
            downloadbinder.progress();

        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bind = findViewById(R.id.bind);
        unbind = findViewById(R.id.unbind);
        tiao = findViewById(R.id.tiao);
        bind.setOnClickListener(this);
        unbind.setOnClickListener(this);
        tiao.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
switch (view.getId()){
    case R.id.bind:
        Intent bindintent=new Intent(this,Myservice.class);
        //绑定Service和activity
        bindService(bindintent,connection,BIND_AUTO_CREATE);
//        Intent intent=new Intent(this,MyIntentService.class);
//        startService(intent);
        break;

        case R.id.unbind:
            unbindService(connection);//解除绑定
        break;
    case R.id.tiao:
       Intent intent1=new Intent(this,Main2Activity.class);
       startActivity(intent1);
        break;
        default:
            break;
}
    }
}

五.intentService的介绍:
intentService是Service的子类,根据需要处理异步请求(以intent表示)。客户端通过调用startService(Intent) 发送请求,该Service根据需要启动,使用工作线程处理依次每个Intent,并在停止工作时停止自身。
代码演示:
MyintentService中代码:

package com.example.administrator.myapplicat323;

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

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

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

Activity中代码:
在Button的点击事件中加入:

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

六.intentservice和service的区别:
Android中的Service是用于后台服务的,当应用程序被挂到后台的时候,问了保证应用某些组件仍然可以工作而引入了Service这个概念,那么这里面要强调的是Service不是独立的进程,也不是独立的线程,它是依赖于应用程序的主线程的,也就是说,在更多时候不建议在Service中编写耗时的逻辑和操作,否则会引起ANR。

那么我们当我们编写的耗时逻辑,不得不被service来管理的时候,就需要引入IntentService,IntentService是继承Service的,那么它包含了Service的全部特性,当然也包含service的生命周期,那么与service不同的是,IntentService在执行onCreate操作的时候,内部开了一个线程,去你执行你的耗时操作。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值