getBaseContext(),getApplicationContext()和ActivityName.this获取的活动上下文之间的实际区别是什么?
要启动一个Activity,你需要一个Intent,比如:
Intent i = new Intent(context, class)
Run Code Online (Sandbox Code Playgroud)
因此,要填写上下文参数,可以使用以下几个选项:
MyActivity.this或只是thisgetApplicationContext()getBaseContext()我相信还有一两个选择.这些选项都出现在某种教程中,一个使用第一个,另一个使用第三个选项.
那么我应该使用哪一个?它甚至重要吗?不同的情况有什么不同吗?
根据这个答案或android的文档,有几种方法可以在应用程序中获取Context并将其传递给其他类/方法/ anyuneed.
假设我在Foo活动中并且需要将上下文传递给Bar的构造函数.
Bar bar = new Bar(Foo.this);
Bar bar2 = new Bar(this); //same as first i guess
Bar bar3 = new Bar(getApplicationContext());
Bar bar4 = new Bar(getBaseContext());
Bar bar5 = new Bar(MyApp.getContext); // get context statically
Run Code Online (Sandbox Code Playgroud)
考虑到内存泄漏,速度,一般性能,所有这些可能性之间的更好方法是什么?
任何人都可以告诉我有关Context的内容吗?我知道我们需要使用上下文进行UI修改.
有人可以向我简要介绍一下它的含义吗?我看过像getApplicationContext(),getBaseContext()?他们之间有什么区别?
目标:让ProgressDialog显示"正在加载...",直到下一个活动完全加载并显示在屏幕上.
尝试将ProgressDialog上下文和活动设置为原始活动.还尝试使用getApplicationContext()和getParentContext().最后两种方法的例外情况.需要这样做因为目标活动由于非简单的布局文件而呈现缓慢.(由于组织问题,现在无法解决这个问题.)结果目标活动需要1-2秒才能进入OnCreate,然后屏幕会变黑并持续5秒以上,然后进行绘制.渲染速度很慢.使用Hierarchy Viewer进行了审核,看到了很多红球,但现在无法修复.
阅读一些相关但尚未找到解决方法.例如,获取Context的各种方法之间有什么区别?
例如,这两次崩溃.使用源"活动"的"this"也不起作用.
// Context parentContext = this.getParent().getBaseContext();
Context parentContext = this.getApplicationContext();
ProgressDialogMenuable theProgressDialog = new ProgressDialogMenuable(parentContext,this);
theProgressDialog.setTitle("yeeha");
theProgressDialog.setMessage("weewah");
theProgressDialog.setIndeterminate(true);
theProgressDialog.setCancelable(true);
theProgressDialog.show();
Run Code Online (Sandbox Code Playgroud)
另外,奇怪的是,当我这样做时没有任何反应:theProgressDialog.show(); ActivityHelper.changeActivity(this,v,InsMyHoldingsActivity.class,extraMap,-1,-1); 用户单击按钮以显示下一个活动,但ProgressDialog与活动启动冲突,除按钮变为黄色触控外,实际上没有任何其他操作.下面的按钮有效.删除ProgressDialog创建,它的工作原理.没有记录控制台消息.肯定会对开发人员有点遗憾.
我注意到在我的类中扩展View,为了获得屏幕大小,我必须使用 getContext
DisplayMetrics dispM = getContext.getResources().getDisplayMetrics();
int width = dispM.WidthPixels;
int height = dispM.HeightPixels;
Run Code Online (Sandbox Code Playgroud)
如果我想在我的活动中做同样的事情,我必须getContext用getBaseContext. 为什么会这样?
我对Android中的术语"上下文"有疑问.我看到上下文提供了有关应用程序运行环境的信息,但是应用程序上下文和活动上下文之间有什么区别?
为什么我这样做:
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
Run Code Online (Sandbox Code Playgroud)
为什么我将上下文传递给构造函数?任何人都可以提供请帮助我理解上下文是什么,以及上下文对象是什么?
我不想复制/粘贴来自Android参考,因为我已经读过它.....太多次没有理解.
对于某些Android应用程序,我想集成以下功能:用户可以定义他想要被提醒的时间.当时间到了,应用程序应该在通知栏中创建通知,即使此时用户没有使用该应用程序.
为此,需要查看AlarmManager,NotificationManager和Notification.Builder类,对吧?
那么我该如何提前创建定时通知呢?我的代码(到目前为止)是这样的:
将其添加到AndroidManifest下以注册广播接收器:
<receiver android:name="AlarmNotificationReceiver"></receiver>
Run Code Online (Sandbox Code Playgroud)
创建一个新的类文件来处理它收到的警报:
public class AlarmNotificationReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
String additionalData = extras.getString("displayText");
// show the notification now
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification mNotification = new Notification(R.drawable.ic_launcher, context.getString(R.string.app_name), System.currentTimeMillis());
PendingIntent pi = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0); // open MainActivity if the user selects this notification
mNotification.setLatestEventInfo(context, context.getString(R.string.app_name), additionalData, pi);
mNotification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.DEFAULT_SOUND;
mNotificationManager.notify(1, mNotification);
} …Run Code Online (Sandbox Code Playgroud) 我想尝试使用以下代码从我的设置中获取一些值:
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.content.*;
public class TCPdumpHandler {
public void getPreference() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
Boolean checkboxPreference = prefs.getBoolean("checkboxPref", true);
}
}
Run Code Online (Sandbox Code Playgroud)
但错误是: The method getBaseContext() is undefined for the type TCPdumpHandler
你能告诉我原因吗?
我按下按钮时尝试启动一个新活动,但没有任何反应,我没有错误.这是我的代码:
主要活动
public class CSLearn_Python_AppActivity extends Activity {
String tag = "Events";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
//get content from main.xml
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button login = (Button) findViewById(R.id.loginBtn);
login.setOnClickListener(new OnClickListener(){
public void onClick(View v){
// Intent intent = new Intent("com.Main.Verification");
// startActivity(intent);
Intent myIntent = new Intent(getBaseContext(), Verification.class);
startActivity(myIntent);
}
});
}
Run Code Online (Sandbox Code Playgroud)
新的活动
import android.app.Activity;
import android.os.Bundle;
public class Verification extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.verification); …Run Code Online (Sandbox Code Playgroud) 我正在使用对话框.我希望如果我单击按钮,则会调用其他活动.但它给出了以下错误.//构造函数Intent(new DialogInterface.OnClickListener(){},Class)未定义
这是代码
builder1.setNegativeButton("secondact", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Intent i=new Intent(this, FbsampleActivity.class)
}
});
Run Code Online (Sandbox Code Playgroud)