android无context的建立方法

本文介绍如何在Android应用中通过自定义Application类实现全局Context的获取与利用,以便于工具类等场景下便捷地使用Context。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

注:精简了原文。 完整精彩原文地址:http://blog.youkuaiyun.com/android_tutor/article/details/8026018

有时我们需要在一个工具类中用到上下文context。当然有一个做法就是使用传递context的方法,当我们需要用全局context的时候,该怎么办呢?

其实我们应用启动的时候会启动Application这个类,这个类是在AndroidManifest.xml文件里其实是默认的

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <application  
  2.        android:icon="@drawable/ic_launcher"  
  3.        android:label="@string/app_name"  
  4.        >  
  5.        <activity  
  6.            android:name=".ApplicationDemoActivity"  
  7.            android:label="@string/app_name" >  
  8.            <intent-filter>  
  9.                <action android:name="android.intent.action.MAIN" />  
  10.                <category android:name="android.intent.category.LAUNCHER" />  
  11.            </intent-filter>  
  12.        </activity>  
  13.    </application>  
这个Application类是单例的,也就是说我们可以自己写个Application(比如名为:MainApplication)类,来代替默认的Applicaiton,这个类可以保存应用的全局变量, (1)我们可以定义一个全局的Context.供外部调用 .用法如下:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.example.contextandroid;  
  2.   
  3. import android.app.Application;  
  4. import android.content.Context;  
  5.   
  6. public class MainApplication extends Application{  
  7.     /** 
  8.      * 全局的上下文 
  9.      */  
  10.     private static Context mContext;  
  11.       
  12.     @Override  
  13.     public void onCreate() {  
  14.         super.onCreate();  
  15.         mContext = getApplicationContext();    
  16.     }  
  17.       
  18.     /** 
  19.      * 获取context 
  20.      * @return 
  21.      */  
  22.     public static Context getContext(){  
  23.         return mContext;  
  24.     }  
  25.       
  26.     @Override  
  27.     public void onLowMemory() {  
  28.         super.onLowMemory();  
  29.     }  
  30.   
  31. }  

我们需要在(2) AndroidMainifest.xml把MainApplication注册 进去(第10行代码):

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.tutor.application"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.       
  7.     <application  
  8.         android:icon="@drawable/ic_launcher"  
  9.         android:label="@string/app_name"  
  10.         android:name=".MainApplication" >  
  11.         <activity  
  12.             android:name=".ApplicationDemoActivity"  
  13.             android:label="@string/app_name" >  
  14.             <intent-filter>  
  15.                 <action android:name="android.intent.action.MAIN" />  
  16.                 <category android:name="android.intent.category.LAUNCHER" />  
  17.             </intent-filter>  
  18.         </activity>  
  19.     </application>  
  20.       
  21. </manifest>  

那么,我们在一个.java文件的工具类中。可以这样调用全局的context:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /**  
  2.      * 调用全局的Context.  
  3.      * @param msg  
  4.      */    
  5.     public static void showToast(String msg){    
  6.         Toast.makeText(MainApplication.getContext(), msg, Toast.LENGTH_SHORT).show();    
  7.     }    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值