Android开发Android技术知识Android开发经验谈

AndroidAZ系列: 四大组件之Service(All,Fa

2019-11-21  本文已影响0人  道阻且长_行则将至

四大组件之Service

Git仓库地址

配套四维导图地址

AndroidAZ系列有以下目的:

  1. Android程序猿的面试(初级,中级,高级,资深),拿到满意的offer。
  2. Android程序猿学习进阶。

标记说明:因为笔者是列出所有的Android知识点,因此面试不需要看那么多内容,如果是面试的知识点。笔者会加上标记Face,而如果不是面试的知识点,笔者会加上No标记,它是要学的东西;然后笔者将Android面试者或者面试者分为4个等级,初级A1,中级A2,高级A3,资深A4,如果这个知识点是所有等级的范围,那么笔者将会以all标记上。因此进阶路线就是A1->A2->A3->A4。也是面试者挑选的复习范围,假如你是中级程序员,那么你面试要看的内容就是包含A2&Face的标记。

1.Service 是什么

Android Service是Android四大组件之一,它主要用来执行一些不与用户交互的长期运行的任务. 服务的运行不依赖于任何用户界面,即使程序被切换到后台,或者用户打开了另一个应用程序,服务仍然能够保持正常运行. 另外Service并不是运行在单独线程中,而是主线程中。所以尽量要避免耗时操作。

2.Service和线程的区别

2.1 线程的用法

2.2 在子线程中更新UI

2.3 服务的用法

  1. 定义一个服务
public class MyService extends Service{
    public MyService(){
    }

    @Override
    public IBinder onBind(Intent intent){
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate(){
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent,int flags,int startId){
        return super.onStartCommand(intent,flags,startId);
    }

    @Override
    public void onDestroy(){
        super.onDestroy();
    }
}
  1. 在AndroidManifest.xml中进行注册
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:lable="@string/app_name"
    android:suppertsRtl="true"
    android:theme="@style/AppTheme">
    ...
    <service
        android:name=".MyService"
        android:enable="true"
        android:exported="true">
    </service>
</application>
  1. 启动服务
Intent startIntent = new Intent(mContext,MyService.class);
mContext.startService(startIntent);
  1. 停止服务
Intent stopIntent = new Intent(mContext,MyService.class);
mContext.stopService(startIntent);

Service和Thread的区别

3.Service的生命周期

Service生命周期图

3.1 startService

  1. 启动Service服务

    单次:startService() —> onCreate() —> onStartCommand()

    多次:startService() —> onCreate() —> onStartCommand() —> onStartCommand()

  2. 停止Service服务

    stopService() —> onDestroy()

3.2 bindService

  1. 绑定Service服务

    bindService() —> onCreate() —> onBind()

  2. 解绑Service服务

    unbindService() —> onUnbind() —> onDestroy()

  3. 启动绑定Service服务

    startService() —> onCreate() —> onStartCommand() —> bindService() —> onBind()

  4. 解绑停止Service服务

    unbindService() —> onUnbind() —> stopService() —> onDestroy()

  5. 解绑绑定Service服务

    unbindService() —> onUnbind(ture) —> bindService() —> onRebind()

3.3 onStartCommand()和onStart()区别

每调用一次startService()方法, onStartCommand()就会执行一次

onstart()方法是在android2.0一下的版本中使用。而在android2.0以上则使用onstartCommand()方法。它们两个方法放在一起使用时,不会产生冲突

4.Intent Service的使用

Intent Service时一个异步的 会自动停止的服务

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

    @Override
    protected void onHandleIntent(Intent intent){
        // 耗时操作
        Log.d("MyIntentService","This is is" + Thread.currentThread().getId());
    }

    @Override
    public void onDestroy(){
        super.onDestroy();
        Log.d("MyIntentService","onDestroy executed");
    }
}

这里首先要提供一个无参的构造函数,并且必须在其内部调用父类的构造函数.然后要在子类中去实现onHandleIntent()这个抽象方法,在这个方法里执行耗时逻辑,而不用担心ANR的问题,因为这个方法已经是在子线程中运行了.

5. Service使用场景

Service是一个应用程序中不可见的组件,用于处理运行时间长或者不需要用户交互的任务,比如一个股票应用,应用程序关掉了但是要给用户实时提供最新的数据,这时就可以使用Service

上一篇 下一篇

猜你喜欢

热点阅读