Android四大组件-Service

服务Service

概念
  • 实现程序后台运行的解决方案,适合执行不需要和用户交互的长期运行的任务;
  • 服务并不运行在单独的进程中,依赖于创建服务时所在的进程;
  • 服务不会自动开启线程,任务默认运行在主线程。
定义一个服务
  • 继承Service;
  • 重写onCreate,onStartCommand,onDestroy方法;
  • 清单文件注册。
启动和关闭服务
  • 启动

    Intent startIntent = new Intent(this, MyService.class);
    startSestarvice(startIntent);
    
  • 关闭

    Intent stopIntent = new Intent(this, MyService.class);
    stopService(stopIntent);
    
  • Ps:现实情况是一旦服务开启就会和Activity失去联系的。

服务和界面通信
  • 在Service中继承Binder创建子类,子类中写和界面交互的方法;
  • onBind中返回Binder子类的实例;
  • 在界面中创建ServiceConnection,重写onServiceConnectedonServiceDisconnected方法,onServiceConnected中的IBinder向下转型为在Service中重写的Binder类实例,此实例负责通信;
  • 调用bindService绑定界面和服务;
  • 调用unbindService解绑服务和界面。
生命周期
  • onCreate:服务创建时调用,只要服务已经存在,即便再次startSestarvice也不会创建新的实例,此方法也不会再次被回调;
  • onStartCommand:每次服务启动都会执行,即startSestarvice每执行一次,此方法会被回调一次,可以执行任务;
  • onDestroystopService调用时,此方法会被回调,无论startSestarvice执行几次,只需要一次stopService即可关闭,用来回收资源;
  • onBind:调用bindService时,此方法会被调用,onBind也只会被回调一次;
  • onServiceDisconnected:此方法在正常关闭stop或者unbind服务时不会被回调,只有被系统杀死时才会回调;
思考
startServicebindServiceunbindServicestopServiceonCreate->onStartCommand->onBind->onDestroy
bindServicestartServiceunbindServicestopServiceonCreate->onBind->onStartCommand->onDestroy
startServicebindServicestopServiceunbindServiceonCreate->onStartCommand->onBind->onDestroy
bindServicestartServicestopServiceunbindServiceonCreate->onBind->onStartCommand->onDestroy
前台服务优点
  • 提升优先级;
使用
  • 声明权限

    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    
  • 在服务的onCreate方法中添加

            Intent intent = new Intent(this, MainActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
            NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "channel_id");
    
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                NotificationChannel channel = new NotificationChannel("channel_id", "channel_name", NotificationManager.IMPORTANCE_HIGH);
                notificationManager.createNotificationChannel(channel);
            }
    
            Notification notification = builder.setContentTitle("this is content title")
                    .setContentText("this is content text")
                    .setWhen(System.currentTimeMillis())
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                    .setContentIntent(pendingIntent)
                    .build();
    
            startForeground(1, notification);
    
IntentService的优点
  • 简单的创建一个异步的,执行完任务会自动停止的服务。
使用
  • 必须提供一个无参构造方法,在方法内部调用有参构造方法;
  • onHandleIntent处理任务,已经运行在子线程,不会ANR;
  • 不用主动关闭,任务执行完之后会自动销毁。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值