上下文Context详细介绍

1.先看看它的继承结构,下图可以看出Context首先是一个抽象类,继承了Object,Activity,Service,Application都继承了它

2.API中对它的描述:

@1Context是关于应用程序环境的全局(全部)信息的接口,它的实现由android系统提供

@2上下文可以允许你访问应用程序特别的资源和类

@3它上调应用程序级操作,如启动Activity,发送广播,接受意图等等。

3.属性摘要

@1包含一些应用中资源操作模式如public static final int MODE_PRIVATE = 0x0000;

@2包含bindService的标记flag:如public static final int BIND_VISIBLE = 0x10000000;

@3包含获取getSystemService系统各项管理的字段,如public static final String POWER_SERVICE = "power";是电源管理字段

4.方法摘要(这些方法大多是抽象方法,需要子类实现,调用也是调用子类的实现的方法,所以传对象时传子类即可)

@1资产文件管理:public abstract AssetManager getAssets();返回对应程序包下的资产

@2资源管理:public abstract Resources getResources();返回对应程序包下的资源对象

@3应用程序包信息:public abstract PackageManager getPackageManager();

@4共享应用程序数据获取者:public abstract ContentResolver getContentResolver();

@5当前进程的主线程的循环:public abstract Looper getMainLooper();The main looper.

@6返回单个当前进程的全局应用程序对象的上下文:public abstract Context getApplicationContext();

@7根据资源id获取数据:getString,getText,getColor

@8返回Drawable对象:getDrawable():Returns a drawable object associated with a particular resource ID and* styled for the current them:

@9颜色列表:getColorStateList

@10获取类加载器:getClassLoader

@11获取应用包名:getPackageName

@12获取应用信息:getApplicationInfo

@13获取SP:getSharedPreferences,getSharedPreferencesPath

@14程序包下的files目录下的文件操作:openFileInput,openFileOutput,deleteFile,getFileStreamPath,getFilesDir

@15外部设备目录:getExternalFilesDir

@16缓存目录getCacheDir

@17应用程序数据库操作:openOrCreateDatabase,deleteDatabase,getDatabasePath,databaseList

@18壁纸管理:setWallpaper,clearWallpaper

@19启动活动:startActivity

@20发送广播:sendBroadcast

@21注册接受者,取消接受者:registerReceiver,unregisterReceiver

@22服务Service管理:startService,stopService,bindService,unbindService

@23获取系统各种管理着:getSystemService

5.总结:可见这个Context在android中起着至关重要的作用,就像一个总管。

需要程序源码的可以加我微信x241602私聊。
原文地址:https://www.cnblogs.com/huipengbo/p/7014850.html