zoukankan      html  css  js  c++  java
  • Android中Context简介

    Context字面意思是上下文,位于framework package的android.content.Context中,其实该类为LONG型。很多方法需要通过 Context才能识别调用者的实例:比如说Toast的第一个参数就是Context,一般在Activity中我们直接用this代替,代表调用者的实例为Activity,而到了一个button的onClick(View view)等方法时,我们用this时就会报错,所以我们可能使用ActivityName.this来解决,到了一个button的onClick(View view)等方法时,我们用this时就会报错是因为: 你是在一个匿名类中调用,this指的是这么匿名类的对象 当然会报错。

    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:

    其他的:

    public abstract File getCacheDir(); 或许缓存目录,

    public abstract Drawable getWallpaper();获取壁纸,

    public abstract void sendBroadcast(Intent intent);发送广播,

    public abstract Intent registerReceiver(BroadcastReceiver receiver,IntentFilter filter);注册广播接收器,

    public abstract ComponentName startService(Intent service);开启关闭Service服务,绑定服务等,

    public abstract ApplicationInfo getApplicationInfo();获取ApplicationInfo 相关信息等

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

  • 相关阅读:
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    开源数据库在平安的应用实践
    从Oracle到PostgreSQL:Storage Index 特性 vs BRIN 索引
    Cosmos 白皮书
    基于支付场景下的微服务改造与性能优化
    MySQL数据库备份之主从同步配置
    Maven Gradle 区别
    荐书:《PostgreSQL指南:内幕探索》| 留言送书
    SQL、NoSQL、NewSQL,论开源之路谁主沉浮
  • 原文地址:https://www.cnblogs.com/mumue/p/2451090.html
Copyright © 2011-2022 走看看