Android 中Context的使用方法详解

 更新时间:2017年09月25日 10:58:01   投稿:lqh  
这篇文章主要介绍了Android 中Context的使用方法详解的相关资料,希望通过本文大家能够理解掌握context的使用方法,需要的朋友可以参考下

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。

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

下面介绍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:

 还有很多有用的方法,具体不一一列举。详情请参考文档,反正Context很有用。

如有疑问请留言或到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

相关文章

  • Android实现SwipeRefreshLayout首次进入自动刷新

    Android实现SwipeRefreshLayout首次进入自动刷新

    这篇文章主要为大家详细介绍了Android实现SwipeRefreshLayout首次进入自动刷新,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-01-01
  • Android布局技巧之使用ViewStub

    Android布局技巧之使用ViewStub

    这篇文章主要为大家详细介绍了Android布局技巧之使用ViewStub,具有一定的实用性,感兴趣的小伙伴们可以参考一下
    2016-06-06
  • Android启动优化之延时加载的步骤详解

    Android启动优化之延时加载的步骤详解

    这篇文章主要给大家介绍了关于Android启动优化之延时加载的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-02-02
  • Studio 编译报错:compileSdkVersion ''android-24'' requires JDK 1.8 or later to compile.的解决办法

    Studio 编译报错:compileSdkVersion ''android-24'' requires JDK 1.

    今天小编就为大家分享一篇关于Studio编译报错:compileSdkVersion 'android-24' requires JDK 1.8 or later to compile.的解决办法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-10-10
  • Android实现机房座位预约系统

    Android实现机房座位预约系统

    这篇文章主要为大家详细介绍了Android实现机房座位预约系统,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-04-04
  • Android仿微信底部按钮滑动变色

    Android仿微信底部按钮滑动变色

    这篇文章主要介绍了Android仿微信底部按钮滑动变色,使用Fragment为Tab页的滑动操作,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-03-03
  • 从源码分析Android的Glide库的图片加载流程及特点

    从源码分析Android的Glide库的图片加载流程及特点

    这篇文章主要介绍了从源码分析Android的Glide库的图片加载流程及特点,Glide库是Android下一款人气很高的多媒体资源管理库,特别是在处理gif加载方面受到众多开发者青睐,需要的朋友可以参考下
    2016-04-04
  • Android.bp语法和使用方法讲解

    Android.bp语法和使用方法讲解

    Android.bp是用来替换Android.mk的配置文件,下面这篇文章主要给大家介绍了关于Android.bp语法和使用的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2023-02-02
  • 如何玩转Android矢量图VectorDrawable

    如何玩转Android矢量图VectorDrawable

    这篇文章主要教大家如何玩转Android矢量图VectorDrawable,对矢量图感兴趣的小伙伴们可以参考一下
    2016-05-05
  • Android Activity生命周期调用的理解

    Android Activity生命周期调用的理解

    大家好。本篇文章主要讲的是Android Activity生命周期调用的理解,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下,方便下次浏览
    2021-12-12

最新评论