Jetpack 之 LifeCycle 组件使用详解,最新BAT大厂面试者整理的Android面试题目模板

Jetpack 之 LifeCycle 组件使用详解 一、LifeCycle 简介 LifeCycle 是一个可以感知宿主生命周期变化的组件。常见的宿主包括 Activity/Fragment、Service 和 Application。LifeCycle 会持有宿主的生命周期状态的信息,当宿主生命周期发生变化时,会通知监听宿主的观察者。 LifeCycle 的出现主要是为了解决: 系统组件的生命周期与普通组件之间的耦合性。 系统组件指:Activity/Fragment、Service 和 Application。 普通组件指:将代码按照功能或者作用封装成 阅读详情

(2)解耦 Fragment

在新版的 SDK 中,Fragment 同样也默认实现了 LifecycleOwner 接口,因此,以上的例子同样适合于 Fragment。

(二)使用场景2:使用 LifecycleService 解耦 Service 与组件


(1)LifecycleService 基本介绍

Android 中拥有生命周期的组件除了 Activity/Fragment ,还有一个非常重要的组件就是 Service。LifecycleService 就是用来监听和解耦 Service 组件的。

public class LifecycleService extends Service implements LifecycleOwner {

private final ServiceLifecycleDispatcher mDispatcher = new ServiceLifecycleDispatcher(this);

…

@Override

@NonNull

public Lifecycle getLifecycle() {

return mDispatcher.getLifecycle();

}

}

(2)具体使用方法

第一步:添加相关依赖

implementation “androidx.lifecycle:lifecycle-service:2.2.0”

第二步:创建 MyServiceObserver 类,实现 LifecycleObserver 接口。使用 @OnLifecycleEvent 标记希望在 Server 生命周期发生变化时得到同步调用的方法。

public class MyServiceObserver implements LifecycleObserver {

private static String TAG = “MyServiceObserver”;

@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)

private void initVideo(){

Log.d(TAG,“initVideo”);

}

@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)

private void pausePlay(){

Log.d(TAG,“stopPlay”);

}

}

第三步:创建一个 MyService 的类,继承 LifecycleServi
Android_Jetpack:LifeCycle在Activity/Fragment、Service以及Application中的应用 Android_Jetpack:LifeCycle在Activity/Fragment、Service以及Application中的应用一、使用LifeCycle解耦Activity/Fragment与组件二、使用LifeCycleService解耦Service与组件三、使用ProcessLifecycleOwner监听应用程序的生命周期 最近重新捡起了Android开发,对Jetpack很感兴趣,就专门研究了下。这篇主要讲的就是系统组件与普通组件解耦的LifeCycle。LifeCycle可以帮助开发者 阅读详情

相关推荐

Android架构灵魂组件Lifecycle的生命周期机制详解

在早期 Andorid 架构中,生命周期的事件监听和状态查询,我们需要自定义一套提供侦测功能的 Activity/Fragment 基类及回调接口,在 Activity/Fragment 的生命周期方法中实现依赖组件的操作,在外部进行事件监听和状态查询。这种模式会导致代码条理性很差而且会扩散错误。在 Lifecycle 引入后,可以避免在 Activity/Fragment 生命周期方法内写太多的业务逻辑处理代码,这样可以使我们的业务逻辑更加解耦。

maniuT的博客 567

android LifeCycle解耦service 组件

前言: 各位同学大家好,相信各位同学在开发安卓的时候都有用过四大组件之一的service吧 今天我们就通过一个获取gps 定位的小案例来讲一下用 LifeCycle解耦service 组件 需要用到的三方库 implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0' 具体实现: 创建我们的 MylocationObserver 继承 LifecycleObserver package com.example.lifecycle

xq610928的博客 1615

ServiceLifecycle:Android Service生命周期

项目视频介绍 # 标题 视频地址 1 服务的生命周期视频1 2 服务的生命周期视频2 3 Android Service全面认识三

反激电源RCD尖峰吸收电路的原理以及调试

反激电源RCD尖峰吸收电路的原理以及调试

mnmmnmn的博客 9257

海康威视摄像头CVE-2017-7921漏洞复现:从零到一的实战指南(含解密工具下载)

本文详细解析了海康威视摄像头CVE-2017-7921漏洞的技术原理与复现方法,提供从环境搭建到实战利用的完整指南。通过认证绕过漏洞,攻击者可获取敏感信息并执行高权限操作,文章还包含解密工具下载与防御建议,助力安全研究人员深入理解物联网设备安全风险。

weixin_29325515的博客 817

告别环境依赖!用PyInstaller把PaddleOCR项目打包成独立EXE(附完整spec文件配置)

本文详细介绍了如何使用PyInstaller将PaddleOCR项目打包成独立EXE文件,彻底解决Python环境依赖问题。通过完整的spec文件配置和实战技巧,开发者可以轻松创建开箱即用的OCR工具,特别适合需要交付给非技术用户的场景。文章涵盖环境配置、模型打包、性能优化等关键环节,并提供了解决常见问题的方案。

weixin_33712987的博客 578

论文阅读------Exposure Fusion: A Simple and Practical Alternative to High Dynamic Range Photography

Mertens T, Kautz J, Van Reeth F. Exposure fusion: A simple and practical alternative to high dynamic range photography[C]//Computer graphics forum. Oxford, UK: Blackwell Publishing Ltd, 2009, 28(1): 161-171. 摘要 本文提出了一种将包围曝光的序列融合到高质量图像中的技术,而无需先转换为高动态范围(HD.

qq_25283239的博客 3398

PLSQL Developer新手必看:如何避免安装过程中的常见坑

本文为PL/SQL Developer新手提供了详尽的安装与配置避坑指南。从版本选择、注册码使用、汉化包安装到Oracle客户端连接配置,逐一解析常见问题并提供解决方案,帮助开发者快速搭建稳定的PL/SQL开发环境,顺利开启Oracle数据库编程工作。

g9h0i1的博客 655

利用VBA宏实现LightTools仿真数据自动化导出至Excel

本文详细介绍了如何利用VBA宏实现LightTools仿真数据自动化导出至Excel的完整流程。通过调用LTAPI接口,工程师可以自动执行仿真、抓取照度与光强等数值数据、捕获并粘贴仿真图像,从而将重复性工作自动化,显著提升光学设计工作的效率和准确性。

weixin_28314575的博客 503

驾驶行为检测数据集准备与处理详解

本文详细介绍了构建"未系安全带/打电话"检测模型的数据集准备流程。首先分析公开数据集(如StateFarm、AICityChallenge)的优缺点,并提供格式转换脚本。重点推荐自行采集+半自动标注方案:1)视频关键帧提取;2)使用预训练模型生成伪标签;3)人工修正标注;4)数据增强(光照变化、运动模糊等)。文章还包含数据配置文件编写指南,并针对类别不平衡、小目标检测等常见问题提出解决方案,如过采样、切片训练和负样本挖掘。通过这套流程可构建高质量数据集,为模型训练奠定基础。

u010117029的博客 356

基于高频变压器的隔离式推挽栅极驱动电路

摘要:本文介绍了一种基于高频变压器的隔离式推挽栅极驱动电路,通过变压器实现电气隔离,为功率MOS管提供可靠驱动信号。电路包含输入侧PWM信号处理、变压器磁耦合隔离传输以及副边驱动逻辑等关键部分,利用电阻电容网络控制开关速度并防止误触发。该隔离驱动方案广泛应用于高压开关电源、新能源汽车电机控制器、光伏逆变器和工业变频器等需要高电压隔离及抗干扰能力的电力电子设备中,有效保障系统安全性和可靠性。

qq_37999850的博客 490

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

贝叶斯统计 [韦来生编]

贝叶斯(Bayes)统计是近几十年来迅速发展起来的数理统计的一个重要分支。贝叶斯方法与经典统计方法的主要不同之处是进行统计推断时除了利用样本信息外,还要利用参数的先验信息,因此可以提高统计推断或统计决策的效果。它在经济、金融、生物、医学、自然科学和社会科学等许多领域具有广泛的应用。贝叶斯方法的研究已渗透到了统计学的几乎所有领域。作者在给中国科学技术大学概率论与数理统计专业本科生开设的“数理统计”课程中有一章专门讲授“贝叶斯方法和统计决策理论”,近十年来也给中国科学技术大学概率统计专业研究生开设过几次“贝叶斯分析”课。本书是在对过去讲稿的内容作了适当的增减和调整的基础上完成的。  本书共分六章。第1章是绪论,介绍了贝叶斯统计的若干基本概念,同时对必要的数理统计的基础知识有重点地作了回顾。第二章介绍了确定先验分布的若干可供选择的方法。第三章介绍了几类常见统计模型参数的后验分布的主要结果和计算方法。第四章和第五章分别介绍了“贝叶斯统计推断”和“贝叶斯统计决策”的内容。第六章介绍了贝叶斯统计计算的若干方法,包括蒙特卡洛方法和马尔可夫链蒙特卡洛(MCMC)方法以及相关统计软件的简介。本书的每一章附右大量的例题和习题,书末附有常用的几个表格和部分习题解答供读者查用。  大约可在54学时内讲授本书的第1章至第六章的主要内容,教师可根据学时需要选讲本书第1章至第六章的部分内容。书中标“*”号的章节可略去不讲,留给读者作为阅读材料。第六章例题中的R代码和部分例题的数据文件等可从作者个人主页下载。  作者在编写本书的过程中参考了J.O.Berger教授的《统计决策论及贝叶斯分析》和茆诗松教授编写的《贝叶斯统计》等书中的一些内容,以及《贝叶斯分析》一书中由张伟平博士编写的有关贝叶斯统计计算的一些内容。在此表示衷心感谢。  本书编写准备过程中,中国科学技术大学统计与金融系的几个研究生帮助完成了本书部分中文的录入和排版,作者对他们的辛勤工作表示真诚的感谢。高等教育出版社为本书的出版给予了大力支持。在此一并致谢。

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

上一篇: Java队列Queue,系统压力暴增的情况下是如何一步步优化MySQL
下一篇: Maven远程仓库的配置和部署,程序员的中年危机
普通网友
博客等级 码龄5年 4697粉丝 610原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值