- 浏览: 1067245 次
- 性别:
- 来自: 南昌
文章分类
- 全部博客 (276)
- 生活 (1)
- 代码之美 (22)
- Media (7)
- Android Widget (3)
- Android Intent (1)
- Android Activity (4)
- UI event handle--UI事件处理机制 (2)
- Java基础知识 (12)
- android Databases (5)
- Android 系统知识 (70)
- 平常遇到的问题与解决方法 (38)
- Android TextView/EditView (2)
- Thinking Java (1)
- android webkit (6)
- JSON (1)
- XML (4)
- HTTP (1)
- Google Weather API (1)
- android 2.3 NFC (10)
- android app (20)
- android framework (7)
- C++ (2)
- android System (5)
- Pthread (1)
- Wifi (8)
- Unix/Linux C (8)
- Android 4.0 (1)
- Mail (1)
- Smack 源码学习 (4)
- iOS (4)
- Android (1)
- git (1)
- Gallery3d (2)
- React-Natice (1)
最新评论
-
dd18349182956:
你是用的smack哪个版本?我用的smack4.1.3和sma ...
关于socket长连接的心跳包 -
xukaiyin:
全英文
getApplicationContext()与this,getBaseContext() -
裂风矢:
...
<category android:name="android.intent.category.DEFAULT" /> 惹的祸 -
xanthodont:
mark一下
XMPP——Smack -
Evilover3:
mark一下,学习了
XMPP——Smack
转自:http://stackoverflow.com/questions/1026973/android-whats-the-difference-between-the-various-methods-to-get-a-context
In various bits of Android code I've seen:
public class MyActivity extends Activity {
public void method() {
mContext = this; // since Activity extends Context
mContext = getApplicationContext();
mContext = getBaseContext();
}
}
However I can't find any decent explanation of which is preferable, and under what circumstances which should be used.
Pointers to documentation on this, and guidance about what might break if the wrong one is chosen, would be much appreciated.
I agree that documentation is sparse when it comes to Contexts in Android, but you can piece together a few facts from various sources.
This blog post on the official Google Android developers blog was written mostly to help address memory leaks, but provides some good information about contexts as well:
In a regular Android application, you usually have two kinds of Context, Activity and Application.
Reading the article a little bit further tells about the difference between to the two and why you might want to consider using the application Context (Activity.getApplicaitonContext()) rather than using the Activity context ("this"). Basically the Application context is associated with the Applicaiton and will always be the same throughout the life cycle of you app, where as the Activity context is associated with the activity and could possible be destroyed many times as the activity is destroyed during screen orientation changes and such.
I couldn't find really anything about when to use getBaseContext() other than a for a one liner from Dianne Hackborn, one of the Google engineers working on the Android SDK:
Don't use getBaseContext(), just use the Context you have.
That was from a post on the android-developers newsgroup, you may want to consider asking your question there as well because a handful of the people working on Android actual monitor that newsgroup and answer questions.
So overall it seems preferable to use the global application context when possible.
I got bad window token errors when using application context to Progress Dialog. So i used activity context instead.
In various bits of Android code I've seen:
public class MyActivity extends Activity {
public void method() {
mContext = this; // since Activity extends Context
mContext = getApplicationContext();
mContext = getBaseContext();
}
}
However I can't find any decent explanation of which is preferable, and under what circumstances which should be used.
Pointers to documentation on this, and guidance about what might break if the wrong one is chosen, would be much appreciated.
I agree that documentation is sparse when it comes to Contexts in Android, but you can piece together a few facts from various sources.
This blog post on the official Google Android developers blog was written mostly to help address memory leaks, but provides some good information about contexts as well:
In a regular Android application, you usually have two kinds of Context, Activity and Application.
Reading the article a little bit further tells about the difference between to the two and why you might want to consider using the application Context (Activity.getApplicaitonContext()) rather than using the Activity context ("this"). Basically the Application context is associated with the Applicaiton and will always be the same throughout the life cycle of you app, where as the Activity context is associated with the activity and could possible be destroyed many times as the activity is destroyed during screen orientation changes and such.
I couldn't find really anything about when to use getBaseContext() other than a for a one liner from Dianne Hackborn, one of the Google engineers working on the Android SDK:
Don't use getBaseContext(), just use the Context you have.
That was from a post on the android-developers newsgroup, you may want to consider asking your question there as well because a handful of the people working on Android actual monitor that newsgroup and answer questions.
So overall it seems preferable to use the global application context when possible.
I got bad window token errors when using application context to Progress Dialog. So i used activity context instead.
发表评论
-
打印调用堆栈
2019-11-15 15:48 496平常我们遇到不清楚代码逻辑的,可以通过打印调用堆栈来理清楚,如 ... -
你知道Log.isLoggable
2018-11-23 14:15 968我们可以通过Log.isLoggable来动态开关log的输出 ... -
android:allowUndo
2018-04-25 16:51 791Android 在Android 23增加了UndoManag ... -
mipmap-xxx
2015-12-10 11:35 1114最近在看AOSP,发现mipmaps, 百度 了一下,发现有各 ... -
《Android.Programming.Pushing.the.Limits].Erik.Hellman》记录1
2015-10-29 10:56 582最近在看《Android.Programming.Pushin ... -
System.currentTimeMillis() uptimeMillis elapsedRealtime 区别
2015-10-28 20:02 1326转自http://blog.csdn.net/wutianyi ... -
GPS的开关设置
2015-09-29 18:36 2046//modify by hyxu 2015-9-30 to s ... -
DialogFragment
2015-09-25 13:56 1059public class YesNoDialog extend ... -
ANDROID L——RecyclerView,CardView导入和使用
2015-07-23 09:51 964转自http://blog.csdn.net/a3969019 ... -
IntentService 和ResultReceiver
2015-07-22 20:00 817转自[url] http://javatechig.com/a ... -
Android media媒体库分析之:分类别统计媒体文件大小
2015-07-21 20:07 556转自http://www.linuxidc.com/Linux ... -
java.lang.IllegalArgumentException: Service Intent must be explicit
2015-07-21 20:03 1310转自:http://www.2cto.com/kf/20150 ... -
Context 和Application Context
2015-02-11 15:14 887http://possiblemobile.com/2013/ ... -
ContentProviderOperation.Builder 中withValue和withValueBackReference的区别
2015-02-10 14:01 2206关于ContentProviderOperation.Buil ... -
AndroidManifest.xml的Service元素 android:process设置
2013-05-30 17:02 11495转自:http://galin.blog.sohu ... -
android中打包含有Activity以及资源文件的jar包在工程中调用
2013-05-28 15:00 1324转自:http://www.cnblogs.com/vaiya ... -
Android杂谈--内存泄露(1)--contentView缓存使用与ListView优化
2012-11-01 09:29 2843转自:http://www.cnblogs.com/louli ... -
Handler+ExecutorService(线程池)+MessageQueue模式+缓存模式
2012-10-31 14:32 1898转自:http://www.eoeandroid.com/th ... -
Animation
2012-10-30 13:41 1139转自:http://hi.baidu.com/wendaoer ... -
Android onTouchEvent和onInterceptTouchEvent
2012-10-24 15:05 1293ViewGroup里的onInterceptTouchEven ...
相关推荐
【Android Context 与 GetApplicationContext 的区别及使用】 在Android开发中,Context是一个至关重要的概念,它扮演着应用程序组件与系统服务交互的关键角色。Context可以理解为应用运行时的上下文环境,提供了对...
RT,在android开发中,如果在使用context的地方全部用getApplicationContext()会不会避免某些内存泄漏问题? 首先,Activity的Context和Application的Context肯定不是一个东西,一个是当前活动的 Context,它的生命...
.setContext(getApplicationContext()) .setVideoEncoder(SessionBuilder.VIDEO_H264) .setVideoQuality(new VideoQuality(1280, 720)); this.startService(new Intent(this,RtspServer.class));
在Application的onCreate()中进行初始化{ CrashLogUtil.init(getBaseContext(), getApplicationContext().getExternalFilesDir("crash_log").getPath());}通过File logDir = new File(CrashLogUtil.getInstance()....
然后,通过getApplicationContext方法,可以返回这个已经加载好的ApplicationContext实例。 使用这个工具类非常简单。例如,如果你需要获取一个名为"userService"的Bean实例,可以这样做: ```java public static ...
安卓基本语句有public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here.... myIntent=new Intent(getApplicationContext(), FrmMain.class); } //2监听器
PermissionHelper.requestPermission(this, new RequestPermissionListener() { @Override public void onSuccess() { Toast.makeText(getApplicationContext(),"权限成功获取",Toast.LENGTH_SHORT).show(); } ...
PermissionHelper.requestPermission(this, new RequestPermissionListener() { @Override public void onSuccess() { Toast.makeText(getApplicationContext(),"权限成功获取",Toast.LENGTH_SHORT).show(); } ...
Button button = (Button) this.findViewById(R.id.button); button.setOnClickListener(new ButtonClickListener()); } private final class ButtonClickListener implements View.OnClickListener{ ...
为避免这种情况,尽量使用`getApplicationContext()`或`getBaseContext()`创建`Toast`,而不是使用`Activity`的`this`。如果在`Activity`中使用`this`,确保在`Activity`的`onDestroy()`方法中调用`toast.cancel()`...
getBaseContext() 或这个(在活动课程中) 上下文使用示例: 创建新对象:创建新视图,适配器,侦听器 TextView tv = new TextView (getContext()); ListAdapter adapter = new SimpleCursorAdapter ...
Toast.makeText(YLC_APPLICATION.getInstance().getApplicationContext(), "您的网络出错啦!", Toast.LENGTH_LONG).show(); }else if (isError == MKEvent.ERROR_NETWORK_DATA) { Toast.makeText(YLC_...
ManagerInitializer.i.init(getApplicationContext()); } } Then call CameraActivity to use camera: Intent intent = new Intent(this, CameraActivity.class); intent.putExtra(CameraActivity.PAT
PendingIntent sendPI = PendingIntent.getBroadcast(getApplicationContext(), 0, intentSend, 0); smsManager.sendTextMessage(strNumber, null, strBody, sendPI, null);//发送短信 send.setEnabled...
一般通过`this`或`getContext()`获取,适合执行与当前Activity相关的操作。 三、`Context`的常用方法 1. **获取资源**:如`getString()`, `getColor()`, `getDrawable()`等,用于获取字符串、颜色、图片等资源。 2....
根据提供的信息来看,这段代码似乎涉及到了一个应用中与日历功能不相关的部分,而是在实现一个具有地理位置选择功能的应用程序。尽管如此,我们依然可以从这段代码中提取出一些关于日历应用程序可能涉及的技术点。 ...
第二个参数是否保存编辑后的照片与你指定的路径.默认为false. 2.实现ImageEditeStateListener接口 fragment.setImageEditeStateListener(new ImageEditeStateListener() { @Override public void ...
在与Android系统的对接过程中,我们可以实现更高效的数据管理和业务流程自动化。 标题"Zebra斑马PDA对接Android"涉及到的关键知识点主要包括以下几个方面: 1. **硬件接口**:Zebra斑马PDA通常配备有多种接口,如...
在Android开发中,`AppInfoBean.zip`这个文件名暗示我们可能在处理一个与应用程序信息相关的Java类。从标签"android"我们可以推断,这涉及到Android应用开发中的一个特定组件或者数据模型。在这个场景下,`...