谈谈Android⾥的Context的使⽤实例
⼤家好,今天给⼤家分享⼀下Android⾥的Context的⼀些⽤法,以前经常有⼈在⾥问我⽐如我在⼀个⼯具类⾥的某个⽅法,或者View⾥需要调⽤Context.但是⼯具类还有View⾥没有这个上下⽂怎么办?为了解决⼤家的疑问,为了解决⼤家的疑问,我今天写⼀个简单的Demo.让⼤家如何学好⾃如的⽤Context.想什么时候有Context,什么时候就有Context.
这⾥⼤致可以分为两种:⼀是传递Context参数,⼆是调⽤全局的Context.
其实我们应⽤启动的时候会启动Application这个类,这个类是在l⽂件⾥其实是默认的
<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="androidintentactionMAIN" />
<category android:name="androidintentcategoryLAUNCHER" />
</intent-filter>
</activity>
</application>
这个Application类是单例的,也就是说我们可以⾃⼰写个Application(⽐如名为:MainApplication)类,来代替默认的Applicaiton,这个类可以保存应⽤的全局变量,我们可以定义⼀个全局的Context.供外部调⽤.⽤法如下:
package com.tutor.application;
import androidappApplication;
import androidcontentContext;
public class MainApplication extends Application {
/**
* 全局的上下⽂
*/
private static Context mContext;
@Override
public void onCreate() {
superonCreate();
mContext = getApplicationContext();
}
/**获取Context
* @return
*/
public static Context getContext(){
return mContext;
}
@Override
public void onLowMemory() {
superonLowMemory();
}
}
我们需要在l把MainApplication注册进去(第10⾏代码):
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="schemasandroidcom/apk/res/android"
package="comtutorapplication"
android:versionCode="1"
android:versionName="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="androidintentactionMAIN" />
<category android:name="androidintentcategoryLAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
为了让⼤家更容易理解,写了⼀个简单的Demo.步骤如下:
第⼀步:新建⼀个Android⼯程ApplicationDemo,⽬录结构如下:
第⼆步:新建MainApplication.Java,代码和上⾯⼀样我就不贴了.
第三步:新建⼀个⼯具类ToolsUtil.java,代码如下
package com.tutor.application;
import androidcontentContext;
import androidwidgetToast;
/**
* @author frankiewei
* 应⽤的⼀些⼯具类
*/
public class ToolUtils {
/
**
* 参数带Context
* @param context
* @param msg
*/
public static void showToast(Context context,String msg){
ToastmakeText(context, msg, ToastLENGTH_SHORT)show();
}
/**
* 调⽤全局的Context
* @param msg
*/
public static void showToast(String msg){
ToastmakeText(MainApplicationgetContext(), msg, ToastLENGTH_SHORT)show();
}
}
第四步:新建⼀个View命名为MainView.java就是我们Activity现实的View.代码如下:package com.tutor.application;
import androidappActivity;
import androidcontentContext;
import androidutilAttributeSet;
import androidviewLayoutInflater;
import androidviewView;
import androidwidgetButton;
import androidwidgetFrameLayout;
/**
* @author frankiewei
* ⾃定义的MainView
*/
public class MainView extends FrameLayout implements ViewOnClickListener{
private Context mContext;
private Activity mActivity;
/**
* 参数Button
*/
private Button mArgButton;
/**
* 全局Button
*/
private Button mGlobleButton;
/**
* 退出Button
*/
private Button mExitButton;
public MainView(Context context){
super(context);
setupViews();
}
public MainView(Context context, AttributeSet attrs) {
super(context, attrs);
setupViews();
}
private void setupViews(){
//获取View的上下⽂
mContext = getContext();
//这⾥将Context转换为Activity
mActivity = (Activity)mContext;
LayoutInflater inflater = LayoutInflaterfrom(mContext);
View v = inflaterinflate(Rlayoutmain, null);
addView(v);
mArgButton = (Button)vfindViewById(Ridarg_button);
mGlobleButton = (Button)vfindViewById(Ridglo_button);
mExitButton = (Button)vfindViewById(Ridexit_button);
mArgButtonsetOnClickListener(this);
mGlobleButtonsetOnClickListener(this);
mExitButtonsetOnClickListener(this);
}
public void onClick(View v) {
if(v == mArgButton){
ToolUtilsshowToast(mContext, "我是通过传递Context参数显⽰的!");    }else if(v == mGlobleButton){
ToolUtilsshowToast("我是通过全局Context显⽰的!");
}else{
mActivityfinish();
}
}
}
这⾥MainView.java使⽤的布局l代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="schemasandroidcom/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"
安卓intent用法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,代码如下:
package com.tutor.application;
import androidappActivity;
import androidosBundle;
public class ApplicationDemoActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
superonCreate(savedInstanceState);
MainView mMainView = new MainView(this);
setContentView(mMainView);
}
}
第六步:运⾏上述⼯程效果如下:
运⾏效果1
运⾏效果2---- 点击第⼀个按钮
运⾏效果3---- 点击第⼆个按钮
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。