Android中Context

Context字面意思上下文,位于framework package的android.content.Context中,其实该类为LONG型,类似Win32中的Handle句柄,很多方法需要通过 Context才能识别调用者的实例,比如说Toast的第一个参数就是Context,一般在Activity中我们直接用this代替,代表调用者的 实例为Activity,而到了一个button的onClick(View view)等方法时,我们用this时就会报错,所以我们可能使用ActivityName.this来解决,主要原因是因为实现Context的类主要有Android特有的几个模型,Activity、Service以及BroadcastReceiver。

Context提供了关于应用环境全局信息的接口。它是一个抽象类,它的执行被Android系统所提供。它允许获取以应用为特征的资源和类型。同时启动应用级的操作,如启动Activity,broadcasting和接收intents。

 
两种类型的Context
在android中context可以作很多操作,但是最主要的功能是加载和访问资源。在android中有两种context,一种是 application context,一种是activity context,通常我们在各种类和方法间传递的是activity context。比如一个activity的onCreate
protected void onCreate(Bundle state) {
super.onCreate(state);
 
TextView label = new TextView(this);
//传递context给view control

label.setText("Leaks are bad");
 
setContentView(label);
}
把activity context传递给view,意味着view拥有一个指向activity的引用,进而引用activity占有的资源:view hierachy, resource等。
 
内存泄露
这样如果context发生内存泄露的话,就会泄露很多内存。这里泄露的意思是gc没有办法回收activity的内存。
 
注释:为什么GC没有办法回收相应的内存,个人感觉是因为传递Context会增加对象指针的引用计数,所以基于智能指针技术的GC无法释放相应的内存。
 
当屏幕旋转的时候,系统会销毁当前的activity,保存状态信息,再创建一个新的。比如我们写了一个应用程序,它需要加载一个很大的图片,我们不希望每次旋转屏幕的时候都销毁这个图片,重新加载。实现这个要求的简单想法就是定义一个静态的Drawable,这样Activity 类创建销毁它始终保存在内存中。实现类似:

 

public class myactivity extends Activity {
private static Drawable sBackground;
protected void onCreate(Bundle state) {
super.onCreate(state);
 
TextView label = new TextView(this);
label.setText("Leaks are bad");
 
if (sBackground == null) {
sBackground = getDrawable(R.drawable.large_bitmap);
}
label.setBackgroundDrawable(sBackground);//drawable attached to a view

setContentView(label);
}
}

 

这段程序看起来很简单,但是却问题很大。当屏幕旋转的时候会有leak(即gc没法销毁activity)。我们刚才说过,屏幕旋转的时候系统会销毁当前的activity。但是当drawable和view关联后,drawable保存了view的 reference,即sBackground保存了label的引用,而label保存了activity的引用。既然drawable不能销毁,它所引用和间接引用的都不能销毁,这样系统就没有办法销毁当前的activity,于是造成了内存泄露。gc对这种类型的内存泄露是无能为力的。避免这种内存泄露的方法是避免activity中的任何对象的生命周期长过activity,避免由于对象对 activity的引用导致activity不能正常被销毁。
 
为了防止内存泄露,我们应该注意以下几点:
  1. 不要让生命周期长的对象引用activity context,即保证引用activity的对象要与activity本身生命周期是一样的
  2. 对于生命周期长的对象,可以使用application context
  3. 避免非静态的内部类,尽量使用静态类,避免生命周期问题,注意内部类对外部对象引用导致的生命周期变化
application context
我们可以使用application context。application context伴随application的一生,与activity的生命周期无关。application context可以通过Context.getApplicationContext或者Activity.getApplication方法获取。
而制造Application context的方法在这里可以找到
 
Java里面通常是用一个static的变量(例如singleton之类的)来同步activity之间(程序里面类之间)的状态。在android里面比较靠谱的做法是用application context来关联这些状态。
每个activity都是context,里面包含了运行时的状态。同样application也有一个context,android会保证这个context是唯一的实例。
做一个你自己的application context需要继承android.app.Application,然后在app的manifest里面说明这个类。android会自动帮你创建你这个类的实例,接着你用Context.getApplicationContext()方法就能在各个activity里
面获得这个application context了。

 class MyApp extends Application {

  private String myState;

  public String getState(){
    return myState;
  }
  public void setState(String s){
    myState = s;
  }
}

class Blah extends Activity {

  @Override
  public void onCreate(Bundle b){
    ...
    MyApp appState = ((MyApp)getApplicationContext());
    String state = appState.getState();
    ...
  }
} 

下面介绍Context的一些get方法,通过这些get方法可以获取应用环境全局信息:

1.public abstract Context getApplicationContext ()

Return the context of the single, global Application object of the current process.

2.public abstract ApplicationInfo getApplicationInfo ()

Return the full application info for this context's package.

3.public abstract ContentResolver getContentResolver ()

Return a ContentResolver instance for your application's package.

4.public abstract PackageManager getPackageManager ()

Return PackageManager instance to find global package information.

5.public abstract String getPackageName ()

Return the name of this application's package.

6.public abstract Resources getResources ()

Return a Resources instance for your application's package.

7.public abstract SharedPreferences getSharedPreferences (String name, int mode)

Retrieve and hold the contents of the preferences file 'name', returning a SharedPreferences through which you can retrieve and modify its values. Only one instance of the SharedPreferences object is returned to any callers for the same name, meaning they will see each other's edits as soon as they are made.

8.public final String getString (int resId)

Return a localized string from the application's package's default string table.

9.public abstract Object getSystemService (String name)

Return the handle to a system-level service by name. The class of the returned object varies by the requested name. Currently available names are:

参考博客:http://blog.chinaunix.net/space.php?uid=17102734&do=blog&id=2830227

              http://blog.youkuaiyun.com/zhangqijie001/article/details/5891682

浅析Android的Context 欢迎访问我的个人网站:https://coderyuan.com Context是Android App中用的非常多的一种概念,常被翻译成上下文,这种概念在其他的技术中也有所使用,无意间点了Context的源码,那么就来分析分析Context在Android中到底是什么东西? 先贴段代码 /** * Interface to global information about an ... 阅读详情

相关推荐

【规则引擎连载3】 WebSphere ILOG JRules 开发——新建一个规则项目

规则项目依赖于bom,bom来自于一个Java项目,所以在建规则项目之前,需要先建立一个Java项目,也就是建模。 说明:原则上是一个规则项目,一个bom。如果规则项目很多,并且每个规则项目的bom都是一样的,那么可以将bom提取出来,单独作为一个项目,这样减少不必要的重复操作,减少维护成本,降低出错几率。这里逐步讲解两种方法来创建规则项目,推荐第二种。 本文主要建立两种规则项目与bom之间的关系,编写相关的规则内容,如操作规则、规则流、决策表等,在后续文章中详细讲解 。

manman的分享 2331

Android中的Context

Android中的Context (2011-03-01 12:39) 分类: Android   最近在看程序的时候,看到好多地方都用到了Context,下面来学习一下: 参考:ACOD、http://www.igniu.com/?p=215、 ACOD上关于Context的解释: Interface to global information about an a...

daixj110的专栏 260

【脚本篇】---spyglass lint脚本

【代码】【脚本篇】---spyglass lint脚本。

冬瓜 6359

【Android】【UI】创建View时的Context该用Application还是Activity

安卓的View在创建时要指定Context,只要是Context,不管Application或者是Activity都可以,但是二者是有细微区别的 Activity包含了工程中设置的Theme,而Application是不包含Theme属性的,使用Application创建View会丢失Theme样式 布局中的控件,安卓系统默认会使用Activity作为Context 通过new View(Context context)或LayoutInfalter.from(Context context)创建的

天天向上 Up Up Up (*^▽^*) 2399

防火墙学习5---虚拟系统和跟系统通过路由表互访

虚拟系统与跟系统互访可分为虚拟系统访问根系统和跟系统访问虚拟系统。

weixin_48030849的博客 1032

mysql笔记:23. 在Mac上安装与卸载MySQL

MySQL对Mac电脑的适配十分强大,再加上Mac本身就是Unix操作系统演化而来的,所以在Mac上安装MySQL的方式有多种。常见的方法有如下三种:DMG、brew、TAR。介绍这些方法前,我们先了解下MySQL安装文件的下载。

斌斌(Java) 2250

打脸现场!Android面试官:Context都说不明白也来面高工

相信大多数从事Android开发的朋友们越来越发现,找工作越来越难了,面试的要求越来越高了除了基础扎实的java知识,数据结构算法,设计模式还要求会底层源码,NDK技术,性能调优,还有会些小程序和跨平台,比如说flutter,以思维脑图的方式展示在下图;点击文档前往获取面试资料与视频教程;【阿里P7级别Android架构师技术脑图+全套视频】频教程;【阿里P7级别Android架构师技术脑图+全套视频】**[外链图片转存中…(img-iyhVo3sW-1710703473606)]

m0_62261166的博客 905

PCL读取点云数据报错:Failed to find match for field ‘x‘/ ‘y‘/ ‘z‘.

原因:使用matlab生成的pcd文件,点云数据是双精度类型,pcl处理的点云数据是单精度类型。解决方案:使用matlab生成的点云坐标单精度化,然后在生成点云加以存储。

little_white138的博客 871

Android开发中使用静态共享applicationContext

在Android开发中,许多方法的使用都需要传入context对象,而在亿些场景下无法像在Activity类中那样直接获取context、使用函数参数传入又显得有些许麻烦,此时便可以创建一个自定义的Application来方便我们获取applicationContext。...

qq_25851273的博客 890

【芯片设计- RTL 数字逻辑设计入门 番外篇6 -- 术语 Tile 介绍】

在系统级芯片(System on Chip, SoC)设计中,“tile” 是一个可以指代不同概念的术语,但通常它指的是芯片上的一个独立的功能单元或核心。一个 tile 可以包含一个或多个处理器核心、缓存、内存控制器、输入输出设备、外设接口或其他功能模块。在 SoC 上,多个 tile 可以被组合和连接起来,以形成一个完整的系统。

CodingCos的博客 3081

共享单车数据抓取与分析:使用Python爬虫抓取共享单车使用数据与地图信息

通过本篇博客,我们展示了如何使用Python爬虫抓取共享单车的使用数据和地图信息,并通过简单的数据分析得出有价值的见解。在实际应用中,数据的收集与分析不仅能帮助城市管理者更好地规划单车站点,还能为用户提供更精准的单车使用体验。未来,我们可以将此分析与机器学习模型相结合,进一步预测单车的使用趋势,优化调度算法,实现更加智能的共享单车管理。

2201_76125261的博客 2206

ApplicationContext对象的获取

实现ApplicationContextAware接口的方式获取ApplicationContext对象实例 创建一个实体类并实现ApplicationContextAware接口,重写接口内的setApplicationContext方法来完成获取ApplicationContext实例的方法,代码如下所示: @Component public class SpringContext implements ApplicationContextAware { /** .

xiaozhu0301的博客 1827

【Proteus仿真】用24C04与1602LCD设计的简易加密电子密码锁

【Proteus仿真】用24C04与1602LCD设计的简易加密电子密码锁

perseverance51博客 1122

安卓-Android什么是context?

原帖: link Android面试:来说一说Context吧,Android中的Context是什么?能干什么?怎么用呢?字节走动_Android关注42020.08.12 22:26:52字数 3,633阅读 813 Activity mActivity =new Activity() 作为Android开发者,不知道你有没有思考过这个问题,Activity可以new吗?Android的应用程序开发采用JAVA语言,Activity本质上也是一个对象,那上面的写法有什么问题呢?估计很多人说不

ruiruiddd的博客 279

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

深度学习工具包 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")

安卓应用有几个context

最近看源码看到Context这一块,发现很多网上的博客对“安卓应用有几个context”这一个问题统一给出的答案是: Activity数量+Service数量+1 我通过源码的分析发现根本不是这样,Context的数量应该是: 2*(Activity数量+Service数量+1) 要搞清楚这个问题就需要研究Activity和Application的生成了,Activity和Application的生成都位于ActivityThread的performLaunchActivity方法源代码: Contex

whoami_I的博客 632

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

android context 实例,安卓Android Context类实例详解

1.例如下面的代码片段,Toast类的第一个参数接受一个Context对象:@Overrideprotected Dialog onCreateDialog(int id) {switch (id) {case 0:Builder builder = new AlertDialog.Builder(this);builder.setIcon(R.drawable.ic_launcher);buil...

weixin_32126843的博客 356

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

安卓c语言获取context,认识一下Android里的Context

认识一下Android里的Context侯亮(本文以Android 7.0为准)1 什么是Context?在Android平台上,Context是一个基本的概念,它在逻辑上表示一个运行期的“上下文”。其实何止是Android平台,在其他平台上,一样有上下文的概念,比如一个进程,其实也是个上下文。我们在编写最简单的C语言程序时,凭什么写一句简单的malloc()就可以申请到内存,写一句简单的open...

weixin_42396760的博客 412

安卓c语言获取context,Android中Context详解 ---- 你所不知道的Context

二、 什么时候创建Context实例熟悉了Context的继承关系后,我们接下来分析应用程序在什么情况需要创建Context对象的?应用程序创建Context实例的情况有如下几种情况:1、创建Application 对象时, 而且整个App共一个Application对象2、创建Service对象时3、创建Activity对象时因此应用程序App共有的Context数目公式为:总Context实例...

weixin_26854475的博客 202

Android安卓 如何在Service获取参数Context 以及AlertDialog.Builder(Context)的Context

我的程序主程序名称: B000Activity 在Service中调用参数Context 参数添加:B000Activity.b000.getApplicationContext() 我的需求是需要在NetWorkutils中调用参数Context 结束

暴走的山交君的博客 1434

安卓c语言获取context,Android全局获取Context实例详解

Android全局获取Context实例详解在弹出Toast 启动活动 发送广播 操作数据库 使用通知等等时都需要Context如果操作在活动中进行是很简单的,因为活动本身就是一个Context对象但是当逻辑代码脱离了Activity类,此时使用Context就需要一些技巧了:我们可以定制一个自己的Application类,以便管理程序内一些全局状态信息,比如全局Context代码如下:publi...

weixin_33781072的博客 284

android的cantext对象,安卓Android Context类实例详解

1.例如下面的代码片段,Toast类的第一个参数接受一个Context对象:@Overrideprotected Dialog onCreateDialog(int id) {switch (id) {case 0:Builder builder = new AlertDialog.Builder(this);builder.setIcon(R.drawable.ic_launcher);buil...

weixin_34802266的博客 223

安卓Context上下文

安卓Context上下文

似霰的博客 1629

安卓中的context详解

大家好,  今天给大家介绍下我们在应用开发中最熟悉而陌生的朋友-----Context类 ,说它熟悉,是应为我们在开发中    时刻的在与它打交道,例如:Service、BroadcastReceiver、Activity等都会利用到Context的相关方法 ; 说它陌生,完全是    因为我们真正的不懂Context的原理、类结构关系。一个简单的问题是,一个应用程序App中存在多少个Cont

smile0528的博客 459

安卓中的Context

Context字面意思上下文,位于framework package的android.content.Context中,其实该类为LONG型,类似Win32中的Handle句柄,很多方法需要通过 Context才能识别调用者的实例,比如说Toast的第一个参数就是Context,一般在Activity中我们直接用this代替,代表调用者的 实例为Activity,而到了一个button的onCli

u011045817的专栏 1139

安卓中的Context是什么?

在安卓开发中,Context是一个非常重要的概念。它提供了对应用程序可用资源集的访问,使对象能够了解应用程序的环境和设备设置。许多安卓的API都需要Context作为参数,ActivityService等类都继承自Context。

qq_29715355的博客 676
上一篇: Android Service 服务(三)—— bindService与remoteService
下一篇: JAVA泛型简析
whatsyourname
博客等级 码龄23年 13粉丝 326原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值