一分钟教你学会-Android Service、Intent Service

本文深入解析了Android中的服务组件,详细介绍了服务的定义、类型、生命周期及其在后台执行长时间操作的机制。涵盖了启动服务、绑定服务的特性,以及如何正确处理耗时操作和服务生命周期的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

服务详解

Android的四大组件之一,服务是长期运行在后台的应用程序组件。服务不是进程,也不是线程,它和应用程序在同一个进程中,服务中不能做耗时操作,运行在主线程中。主要应用与后台播放音乐,定位服务,每隔一定时间和服务器进行交互。注意服务需要在注册列表中注册!

 

一,定义

安卓的四大组件之一,是一个可以在后台执行长时间运行操作而不使用用户界面的应用组件。服务能够被其他组件启动,绑定,交互,通信。

二,基本类型

1,发起者(启动)

通过startService()方法启动的服务。通常,开始的服务执行单一的操作并且不会向调用者返回结果。

2,结合(绑定)

通过bindService()方法绑定的Service.bound服务提供了一个客户端/服务器接口,允许组件与服务进行交互,发送请求,获取结果,甚至可以利用进程间通信(IPC)跨进程执行这些操作绑。定服务的生存期和被绑定的应用程序组件一致。多个组件可以同时与一个服务绑定,不过所有的组件解除绑定后,服务也就会被销毁。

注:服务运行于宿主进程的主线程中,耗时操作需要新建线程。

子线程中使用会崩溃,例如,在的onclick中,加入一个线程线程实现setTextView(“串”),这种方式会导致任务程序崩溃。

 

三,服务的生命周期 

服务有两种启动方式,生命周期不一样:

  1. 的onCreate() - > onStartCommand() - > onDestory()
  1. 的onCreate() - > onBind() - > onUnbind() - > onDestory()

1,的的onCreate()注意一个activityA跳转到活动后,A中的服务终止

首次创建服务时,系统将调用此方法来执行一次性设置程序,在调用onStartCommand()或onBind()之前。如果服务已在运行,则不会调用此方法。

2,onStartCommand()

通过调用startService()请求启动服务时,系统将调用此方法。一旦执行此方法,服务即会启动并可在后台无限期运行。通过调用stopSelf()或stopService()来停止服务。

3,onBind()

通过调用bindService()与服务绑定(例如执行RPC)时,系统将调用此方法。

4,onUnbind()

当服务上绑定的所有客户端都断开连接时调用此方法。

5,的的onDestroy()

当服务不再使用且将被销毁时,系统将调用此方法。服务应该实现此方法来清理所有资源,如线程,注册的侦听器,接收器等。这是服务接收的最后一个调用。

 

注意事项:

1.服务不是一个单独的进程,它和应用程序在同一个进程中。

2.Service不是一个线程,所以我们应该避免在服务里面进行耗时的操作

 

注意服务是被动在acitivty中启动的

public class MainActivity extends AppCompatActivity {
    public static final String TAG = "MainActivity";
    private Button startServiceBtn;
    private Button stopServideBtn;
    private Button goBtn;
    private Button unBindServiveBtn;
    private Intent serviceIntent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        startServiceBtn = (Button) findViewById(R.id.start_service);
        stopServideBtn = (Button) findViewById(R.id.stop_service);
        goBtn = (Button) findViewById(R.id.go);
        unBindServiveBtn = (Button)findViewById(R.id.unbind) ;

 
        startServiceBtn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                serviceIntent = new Intent(MainActivity.this, HelloService.class); // HelloService
                startService(serviceIntent);  //启动服务
            }
        });
        stopServideBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stopService(serviceIntent);
            }
        });
        goBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, BActivity.class);
                startActivity(intent);
            }
        });
}


 

HelloService中的代码:

public class HelloService extends Service{

 
    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        Log.i("onCreate", "首次创建服务");
        super.onCreate();
    }

 
    @Override
    public IBinder onBind(Intent intent) {
        Log.i("onBind", "服务绑定");
        return onBind(intent);
    }

 
   // 已经废弃
//    @Override
//    @Deprecated
//    public void onStart(Intent intent, int startId) {
//        super.onStart(intent, startId);
//    }

 
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        Log.i("onStartCommand", "服务即会启动并可在后台无限期运行");
        return super.onStartCommand(intent, flags, startId);
    }

 
    @Override
    public boolean onUnbind(Intent intent) {
        // TODO Auto-generated method stub
        Log.i("onUnbind", " 当Service上绑定的所有客户端都断开连接时调用此方法。");
        return super.onUnbind(intent);
    }

 
    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        Log.i("onDestroy", "当服务不再使用且将被销毁时,系统将调用此方法");
        super.onDestroy();
    }
 
}

意向服务简介

这是服务的子类,它使用工作线程逐一处理所有启动请求。如果您不要求服务同时处理多个请求,这是最好的选择。您只需实现onHandleIntent()方法即可,该方法会接收每个启动请求的意图,使您能够执行后台工作有以下特点:

1,IntentService会创建单独的工作者线程处理所有的意图请求;

2,IntentService会创建单独的工作者线程来处理onHandleIntent()方法实现的代码,因此无需处理多线程问题;

3,所有请求处理完成后,IntentService自动停止无需调用stopSelf()方法停止该服务。

如图4所示,服务的onBind()方法提供了默认实现,返回NULL;

5,服务的onStartCommand()方法提供了默认实现,该实现会将请求意图添加到队列中。

使用方法:扩展IntentService重写onHandlerIntent()方法。

 

举例如下:

公共类MyIntentService扩展IntentService {

 

  public MyIntentService(){

        超级( “MyIntentService”); //调用父类的有参构造函数

    }



    @覆盖

    protected void onHandleIntent(Intent intent){

        //打印当前线程的id

        Log.d(“MyIntentService”,“Thread id is”+ Thread.currentThread()。getId());

    }



    @覆盖

    public void onDestroy(){

        super.onDestroy();

        Log.d(“MyIntentService”,“onDestroy execution”);

    }

}

 

注意:Intent service方法也是需要在activity中引用的,但是可以在点击事件中引用该类型服务

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.start_service:
            Intent startIntent = new Intent(this, MyService.class);
            startService(startIntent); // 启动服务
            break;
        case R.id.stop_service:
            Intent stopIntent = new Intent(this, MyService.class);
            stopService(stopIntent); // 停止服务
            break;
        case R.id.bind_service:
            Intent bindIntent = new Intent(this, MyService.class);
            bindService(bindIntent, connection, BIND_AUTO_CREATE); // 绑定服务
            break;
        case R.id.unbind_service:
            unbindService(connection); // 解绑服务
            break;

 
    /** 这里实现的intentService服务 **/
   case R.id.start_intent_service:
    // 打印主线程的id
    Log.d("MainActivity", "Thread id is " + Thread.currentThread(). getId());
    Intent intentService = new Intent(this, MyIntentService.class);
    startService(intentService);
    break;

 
        default:
            break;
    }
}

参考学习:

https://www.jianshu.com/p/476d3ed50db1

https://www.jb51.net/article/136964.htm

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值