四大组件|Service的初级使用

2019-01-12  本文已影响5人  chipsss

为什么要使用服务

service是Android中实现程序后台运行的解决方案,它非常适合执行那些不需要和用户交互而且还要求长期运行的任务。

本篇对Service的基本使用进行探讨


Service注意点:

基本用法:

定义一个服务:
创建服务.png

Exported属性表示是否允许除了当前程序之外的其他程序访问这个服务。Enable属性表示是否启用这个服务。

public class MyService extends Service {
    public MyService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        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();
    }
}
生命周期:

onCreate->onStartCommand->onDestroy**onCreate**:在服务创建时调用,可完成初始化工作。
onStartCommand:在每次服务启动时调用。**onDestroy**:在服务销毁时调用,用来回收不再使用的资源。
注意点:

启动和停止服务:
  1. 无绑定启动:这种启动模式使activity和service没有关系,活动不知道服务做了什么事情,只知道启动和调用。
    //activity中调用
    Intent startIntent = new Intent(context, ServiceName.class);
    startService(startIntent);
    
    Intent stopIntent = new Intent(context, ServiceName.class);
    stopService(stopIntent);
    
    //Service中调用该方法也可实现停止服务
    stopSelf();
  1. 绑定启动:为了让activity和service的关系更紧密一些,例如activity指挥service去干什么。`
    (1) 在Service类定义个内部类继承Bider,并新建一个实例,在onBind中返回
public class MyService extends Service {
    private static String TAG = "MyService";
    private DownloadBinder binder = new DownloadBinder();
    
    public class DownloadBinder extends Binder{
        public void startDownload(){
            Log.d(TAG, "startDownload");
        }

        public void getDownloadProgress(){
            Log.d(TAG, "progress");
        }

        public void cancelDownload(){
            Log.d(TAG, "cancelDownload");
        }
    }
    
    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }

(2) 在activity中生命service的内部Binder,并新建一个Connection,在onServiceConnected中下转为自定义的Binder,并调用自己定义的方法。

MyService.DownloadBinder binder;

    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            binder = (MyService.DownloadBinder) iBinder;
            binder.startDownload();
            binder.getDownloadProgress();
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    };

(3) 绑定启动,解绑停止

    //activity中,绑定启动
    Intent bindIntent = new Intent(this, MyService.class);
    bindService(bindIntent, connection, BIND_AUTO_CREATE);
    
    //解绑停止
    unbindService(connection);

bindService()第三个参数为标志位, BIND_AUTO_CREATE表示活动和服务进行绑定后自动创建服务。

更多用法探讨:

1. 前台服务

为什么使用前台服务?后台服务的优先级是比较低的,如果系统出现内存不足的情况,就可能会回收后台运行的服务。如果你希望服务一直运行着,就可以考虑使用前台服务。
前台服务会有一个正在运行的图标在系统状态栏显示,方便传递信息给用户。比如下载进度。

public class MyService extends Service {
    @Override
    public void onCreate() {
        super.onCreate();

        //创建前台服务
        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        Notification notification = new NotificationCompat.Builder(this)
                .setContentText("content text")
                .setContentTitle("content title")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
                .setContentIntent(pendingIntent)
                .build();
        startForeground(1, notification);
    }
}

与创建通知的方法类似,通过启动服务即可启动实现前台服务。

2. IntentService

为什么使用IntentService?`
由上面我们知道,服务中的代码是直接运行在主线程中的,若处理一些很耗时的工作,就很容易引起线程阻塞,进而导致ARN(Application Not Responding)。我们可以通过创建子线程解决这个问题,但很多时候我们会选择偷懒,IntentService就可以做到这一点,可以创建一个异步且支持自动停止的服务。

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

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        //处理具体业务逻辑
        //处理完Service的具体业务逻辑后会自动调用Destory()销毁服务
    }
}

这里需要提供一个无参构造函数,必须实现父类的有参构造函数。在子类onHandleIntent()中处理一些具体逻辑,因为这个方法已经是在子线程中运行的了。

参考`
《第一行代码》

上一篇下一篇

猜你喜欢

热点阅读