轉自:http://www.kaixin001.com/repaste/779081_7412034925.html
大家好,今天給大家分享一下Android裏的Context的一些用法,以前經常有人在羣裏問我比如我在一個工具類裏的某個方法,或者View裏需要調用Context.但是工具類還有View裏沒有這個上下文怎麼辦?爲了解決大家的疑問,爲了解決大家的疑問,我今天寫一個簡單的Demo.讓大家如何學好自如的用Context.想什麼時候有Context,什麼時候就有Context.
這裏大致可以分爲兩種:一是傳遞Context參數,二是調用全局的Context.
其實我們應用啓動的時候會啓動Application這個類,這個類是在AndroidManifest.xml文件裏其實是默認的
[java]view plaincopyprint?
- <application
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- >
- <activity
- android:name=".ApplicationDemoActivity"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN"/>
- <category android:name="android.intent.category.LAUNCHER"/>
- </intent-filter>
- </activity>
- </application>
這個Application類是單例的,也就是說我們可以自己寫個Application(比如名爲:MainApplication)類,來代替默認的Applicaiton,這個類可以保存應用的全局變量,我們可以定義一個全局的Context.供外部調用.用法如下:
[java]view plaincopyprint?
- packagecom.tutor.application;
- importandroid.app.Application;
- importandroid.content.Context;
- publicclassMainApplicationextendsApplication {
- /**
- * 全局的上下文.
- */
- privatestaticContext mContext;
- @Override
- publicvoidonCreate() {
- super.onCreate();
- mContext = getApplicationContext();
- }
- /**獲取Context.
- * @return
- */
- publicstaticContext getContext(){
- returnmContext;
- }
- @Override
- publicvoidonLowMemory() {
- super.onLowMemory();
- }
- }
我們需要在AndroidMainifest.xml把MainApplication註冊進去(第10行代碼):
[java]view plaincopyprint?
- <?xml version="1.0"encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.tutor.application"
- android:versionCode="1"
- android:versionName="1.0">
- <application
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:name=".MainApplication">
- <activity
- android:name=".ApplicationDemoActivity"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN"/>
- <category android:name="android.intent.category.LAUNCHER"/>
- </intent-filter>
- </activity>
- </application>
- </manifest>
爲了讓大家更容易理解,寫了一個簡單的Demo.步驟如下:【成都安卓培訓】
第一步:新建一個Android工程ApplicationDemo,目錄結構如下:
第二步:新建MainApplication.java,代碼和上面一樣我就不貼了.
第三步:新建一個工具類ToolsUtil.java,代碼如下
[java]view plaincopyprint?
- packagecom.tutor.application;
- importandroid.content.Context;
- importandroid.widget.Toast;
- /**
- * @author frankiewei.
- * 應用的一些工具類.
- */
- publicclassToolUtils {
- /**
- * 參數帶Context.
- * @param context
- * @param msg
- */
- publicstaticvoidshowToast(Context context,String msg){
- Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
- }
- /**
- * 調用全局的Context.
- * @param msg
- */
- publicstaticvoidshowToast(String msg){
- Toast.makeText(MainApplication.getContext(), msg, Toast.LENGTH_SHORT).show();
- }
- }
第四步:新建一個View命名爲MainView.java就是我們Activity現實的View.代碼如下:
[java]view plaincopyprint?
- packagecom.tutor.application;
- importandroid.app.Activity;
- importandroid.content.Context;
- importandroid.util.AttributeSet;
- importandroid.view.LayoutInflater;
- importandroid.view.View;
- importandroid.widget.Button;
- importandroid.widget.FrameLayout;
- /**
- * @author frankiewei.
- * 自定義的MainView.
- */
- publicclassMainViewextendsFrameLayoutimplementsView.OnClickListener{
- privateContext mContext;
- privateActivity mActivity;
- /**
- * 參數Button.
- */
- privateButton mArgButton;
- /**
- * 全局Button.
- */
- privateButton mGlobleButton;
- /**
- * 退出Button.
- */
- privateButton mExitButton;
- publicMainView(Context context){
- super(context);
- setupViews();
- }
- publicMainView(Context context, AttributeSet attrs) {
- super(context, attrs);
- setupViews();
- }
- privatevoidsetupViews(){
- //獲取View的上下文.
- mContext = getContext();
- //這裏將Context轉換爲Activity.
- mActivity = (Activity)mContext;
- LayoutInflater inflater = LayoutInflater.from(mContext);
- View v = inflater.inflate(R.layout.main,null);
- addView(v);
- mArgButton = (Button)v.findViewById(R.id.arg_button);
- mGlobleButton = (Button)v.findViewById(R.id.glo_button);
- mExitButton = (Button)v.findViewById(R.id.exit_button);
- mArgButton.setOnClickListener(this);
- mGlobleButton.setOnClickListener(this);
- mExitButton.setOnClickListener(this);
- }
- publicvoidonClick(View v) {
- if(v == mArgButton){
- ToolUtils.showToast(mContext,"我是通過傳遞Context參數顯示的!");
- }elseif(v == mGlobleButton){
- ToolUtils.showToast("我是通過全局Context顯示的!");
- }else{
- mActivity.finish();
- }
- }
- }
這裏MainView.java使用的佈局main.xml代碼如下:
[html]view plaincopyprint?
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical">
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Welcome to frankie wei's blog."
- />
- <Button
- android:id="@+id/arg_button"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="傳遞Context參數"
- />
- <Button
- android:id="@+id/glo_button"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="全局的Context"
- />
- <Button
- android:id="@+id/exit_button"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="退出App"
- />
- </LinearLayout>
第五步:修改ApplicationDemoActivity.java,代碼如下:
[java]view plaincopyprint?
- packagecom.tutor.application;
- importandroid.app.Activity;
- importandroid.os.Bundle;
- publicclassApplicationDemoActivityextendsActivity {
- @Override
- publicvoidonCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- MainView mMainView =newMainView(this);
- setContentView(mMainView);
- }
- }
第六步:運行上述工程效果如下:
運行效果1 運行效果2---- 點擊第一個按鈕
好了今天就講到這裏