Android中的重要类学习之——Context

Android中的重要类学习之——Context

如其名字一样,这个类定义 的是一个上下文(应用程序)环境接口。此类的直接子类是ContextWrapper,而ContextThemeWrapper和Application都是ContextWrapper的直接子类,Acitvity是ContextThemeWrapper的直接子类,因此Activity也是一种Context。

 

重要的方法:

   public abstractAssetManager getAssets();

 

   public abstractResources getResources();——可以通过此方法获取此应用程序的资源描述实例,然后通过此实例便可访问项目res文件夹下的资源(assets应该不可以)

 

public abstractPackageManager getPackageManager();

 

   public abstractContentResolver getContentResolver();

 

public abstractLooper getMainLooper();——返回当前进程的主线程的Looper对象,此主线程用来分发对应用程序组件的调用。

 

public abstractContext getApplicationContext();——我在一个简单的Activity中测试此方法返回的是一个Application类型的实例。

 

public finalCharSequence getText(int resId)

public finalString getString(int resId)       这两个方法提供了快捷获取字符串资源的方法

 

public abstractString getPackageName();——返回此应用程序的包的名称

 

public abstractApplicationInfo getApplicationInfo();——Returnthe full application info for this context's package.   我们可以从ApplicationInfo这个类的实例中获取此应用程序的各种信息,具体内容可查看ApplicationInfo的文档。

 

以下方法是关于数据存储的

public abstractSharedPreferences getSharedPreferences(String name,

            intmode);——If apreferences file by this name

     * does not exist, it will be created whenyou retrieve an

     * editor (SharedPreferences.edit()) andthen commit changes (Editor.commit()).

     * @param modeOperating mode.  Use 0 or {@link#MODE_PRIVATE} for the

     * default operation, {@link#MODE_WORLD_READABLE}

     * and {@link#MODE_WORLD_WRITEABLE} to control permissions.

 

public abstractFileInputStream openFileInput(String name)

        throws FileNotFoundException;

 

public abstractFileOutputStream openFileOutput(String name, intmode)

        throwsFileNotFoundException;

 

public abstract booleandeleteFile(String name);

 

public abstract FilegetFileStreamPath(String name);

 

 注意,以上四个方法的name(文件名)中不能含有路径分隔符(seperator)  ,这些文件存储在/data/data/程序包名/files目录下

 

public abstract FilegetFilesDir();——openFileOutput()方法所创建的文件所在的目录,一般为/data/data/程序包名/files

 

public abstract FilegetExternalFilesDir(String type);——获取外部存储的目录,如果传入null则返回外部存储的根目录

 

public abstract FilegetCacheDir();

 

public abstract FilegetExternalCacheDir();

   

public abstractString[] fileList();

 

public abstract FilegetDir(String name, int mode);

 

public abstractSQLiteDatabase openOrCreateDatabase(String name,

            intmode, CursorFactory factory);

 

public abstract booleandeleteDatabase(String name);

 

public abstract FilegetDatabasePath(String name);

 

public abstractString[] databaseList();

 

注:以上关于数据库操作的方法中的name属性是数据库名,关于数据存储的方法就是这些,下面我们看一些组件间调用与通信用到的方法

 

public abstract voidstartActivity(Intent intent);——这个就不用说了吧。最常用的方法。

 

public abstract voidstartIntentSender(IntentSenderintent,

            Intent fillInIntent, intflagsMask, int flagsValues, intextraFlags)

            throws IntentSender.SendIntentException;——不太懂,大家可以研究下。。

 

public abstract voidsendBroadcast(Intent intent);——这个也是很常用的,发送广播,已注册的所有可以匹配这个intent的BradcastReceiver的onReceive()方法会被调用。这个方法是一个异步调用的方法,且能立即返回(在onReceive方法被调用前)。

 

public abstract voidsendBroadcast(Intent intent,

            String receiverPermission);——同上面的sendBroadcast(Intent),只不过能够匹配intent的BroadcastReceiver必须同时拥有receiverPermission权限。

 

public abstract voidsendOrderedBroadcast(Intentintent,

            String receiverPermission);——Broadcast the given intent to all interestedBroadcastReceivers, delivering them one at a time toallow more preferred receivers to consume the broadcastbefore it is delivered to less preferred receivers.也就是说这个会先递交给优先级高的receiver去处理,这个优先级的高低通过IntentFilter的Priority属性设置。

 

public abstractIntent registerReceiver(BroadcastReceiver receiver,

                                           IntentFilter filter);——我们也可以通过Manifest来注册Receiver,不过如果Recevier定义为内部类的时候我们就必须通过这种方式来注册它了。以种方式注册的Receiver的生命周期与注册它的组件的生命周期紧紧相关。还有,此方法不能在BroadcastReceiver组件中调用。

 

public abstract voidunregisterReceiver(BroadcastReceiver receiver);——移除注册

 

public abstractComponentName startService(Intent service);——启动本地服务

 

public abstract booleanstopService(Intent service);——结束一个本地服务

 

*/

    public abstract booleanbindService(Intent service, ServiceConnection conn,

            intflags);——绑定到一个本地或远程服务。

 

public abstract voidunbindService(ServiceConnection conn);

 

public abstractObject getSystemService(String name);——获取系统服务,一般都会返回一些Manager。name可以是以下这些值(还列出了相应返回的对象类型):

* @see #WINDOW_SERVICE

     * @seeandroid.view.WindowManager

     * @see#LAYOUT_INFLATER_SERVICE

     * @seeandroid.view.LayoutInflater

     * @see#ACTIVITY_SERVICE

     * @seeandroid.app.ActivityManager

     * @see#POWER_SERVICE

     * @seeandroid.os.PowerManager

     * @see#ALARM_SERVICE

     * @seeandroid.app.AlarmManager

     * @see#NOTIFICATION_SERVICE

     * @seeandroid.app.NotificationManager

     * @see#KEYGUARD_SERVICE

     * @seeandroid.app.KeyguardManager

     * @see#LOCATION_SERVICE

     * @seeandroid.location.LocationManager

     * @see#SEARCH_SERVICE

     * @seeandroid.app.SearchManager

     * @see#SENSOR_SERVICE

     * @seeandroid.hardware.SensorManager

     * @see#STORAGE_SERVICE

     * @seeandroid.os.storage.StorageManager

     * @see#VIBRATOR_SERVICE

     * @seeandroid.os.Vibrator

     * @see#CONNECTIVITY_SERVICE

     * @seeandroid.net.ConnectivityManager

     * @see#WIFI_SERVICE

     * @seeandroid.net.wifi.WifiManager

     * @see#AUDIO_SERVICE

     * @seeandroid.media.AudioManager

     * @see#TELEPHONY_SERVICE

     * @seeandroid.telephony.TelephonyManager

     * @see#INPUT_METHOD_SERVICE

     * @seeandroid.view.inputmethod.InputMethodManager

     * @see#UI_MODE_SERVICE

     * @seeandroid.app.UiModeManager

     */

 

上面就是我从Context的源文件中总结的我认为比较重要的方法,其实就是把那些英文的注释翻译了一下而已,希望能直到抛砖引玉的作用,大家一起进步。

PS:csdn上这个显示效果不好,如果有兴趣的同学可以在下面回复中留下你的邮箱,我把docx格式的给你发过去。

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值