是什么区别getContext(),getApplicationContext(),getBaseContext(),和" this"?
虽然这是一个简单的问题,但我无法理解它们之间的基本区别.如果可能,请举出一些简单的例子.
我是新来的Android和我想明白之间的差别getApplication(),getApplicationContext()getBaseContext(),getContext()以及someClass.this特别是当使用这些方法在下面的代码行:
当我发起祝酒时,这些和我使用它们之间有什么区别?
Toast.makeText(LoginActivity.this, "LogIn successful", Toast.LENGTH_SHORT).show();
Toast.makeText(getApplication(), "LogIn successful", Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "LogIn successful", Toast.LENGTH_SHORT).show();
Toast.makeText(getBaseContext(), "LogIn successful", Toast.LENGTH_SHORT).show();
Run Code Online (Sandbox Code Playgroud)
与意图相同:
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
Intent intent = new Intent(MenuPagina., LoginActivity.class);
Intent intent = new Intent(getBaseContext(), LoginActivity.class);
Intent intent = new Intent(getApplication(), LoginActivity.class);
Run Code Online (Sandbox Code Playgroud) 有什么区别:
getApplicationContext()getBasecontext()getApplication()getParent()你能用一个简单的例子详细说明吗?
我最近向市场发布了一个应用程序,我现在收到一些用户的错误,该应用程序可能会在启动时崩溃.不幸的是我无法直接与他联系,该应用程序在模拟器以及我的手机(以及一些朋友的手机)上工作正常.
编辑:我想这会发生在多个用户身上,因为我在市场上收到的评论就像"开始时崩溃"或"不起作用".我只收到了这一个堆栈跟踪,但没有关于配置,设备,Android版本等的信息.
该应用程序是一个简单的音板,所以没有任何魔术,但我无法理解为什么它在某些手机上失败.这是我得到的堆栈跟踪,我希望有人可以帮助我:
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.my.app/com.my.app.SoundMachine}: java.lang.ClassNotFoundException: com.my.app.SoundMachine in loader dalvik.system.PathClassLoader[/mnt/asec/com.my.app-1/pkg.apk]
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:876)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:634)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: com.my.app.SoundMachine in loader dalvik.system.PathClassLoader[/mnt/asec/com.my.app-1/pkg.apk]
at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
... 11 more
Run Code Online (Sandbox Code Playgroud)
这些是我活动的前几行:
public class SoundMachine extends Activity {
private SoundManager mSoundManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Run Code Online (Sandbox Code Playgroud)
编辑:这是(几乎)完成onCreate:
@Override …Run Code Online (Sandbox Code Playgroud) 我GOOGLE了这个问题很多,已经发现,当使用许多不同的建议getBaseContext,getApplicationContext或活动本身的这个指针.
经常出现并且似乎很有意义的三条规则是 -
假设这些是正确的,getBaseContext有什么用?
我见过许多使用以下方法创建新意图的例子 -
Intent intent = new Intent(getBaseContext(), myClass.class);
Run Code Online (Sandbox Code Playgroud)
与 - 相反 -
Intent intent = new Intent(this, myClass.class);
Run Code Online (Sandbox Code Playgroud)
哪个是正确的或推荐的方法,为什么?
我有一个长时间运行的异步任务,它将一些数据发送到我的服务器,然后停止.整个过程可能涉及一些请求和响应.我必须从数据库中读取数据,发送它并处理响应并相应地更新我的数据库.我正在使用内容提供程序从数据库中读取和更新数据.
现在要使用Content Provider,我必须getContentResolver()在上下文中调用该方法.所以我想知道我是否必须使用getApplicationContext或只是传递Activity.this给我的方法.
我看了几个帖子是这样解释两个和大部分之间的区别,他们劝告我们不要使用getApplicationContext,如果可能的.虽然我不希望我AsyncTask失去从被破坏或方向改变Activity.this时的背景Activity.所以我想知道我是否可以使用getApplicationContext我的情况或使用Activity.this符合我的要求.
this经常参考当前的背景.但是,在某些情况下,为什么我们必须使用getBaseContext()而不是this.(这意味着使用时this会发现错误).
这是我的例子:
Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?>arg0, View arg1, int arg2, long arg3){
Toast.makeText(getBaseContext(),"SELECTED", Toast.LENGTH_SHORT).show(); //this line
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,当我更改getBaseContext()为this将收到错误.
请问谁可以帮我解释一下.
有点混淆Widget Context和Application Context之间的区别:关于无法通过实现Android Widget的源代码注册新的BroadcastReceiver的问题(Ref.1)
出于可读性的原因,我将我的答案复制如下:★ 亨利的问题(参考文献1):
我正在制作一个需要广播接收器的小部件,就像com.example.android.apis.appwidget.ExampleBroadcastReceiver中的那个.但是,该示例在清单中定义了Intent.ACTION_TIMEZONE_CHANGED,但有一些不允许这样做
例如,Intent.ACTION_TIME_TICK说"你不能通过在清单中声明的组件来接收它,只能通过用Context.registerReceiver()明确地注册它."
所以我删除了清单声明并尝试使用如下调用替换示例中的AppWidgetProvider.onEnabled函数:context.registerReceiver(myReceiver,new IntentFilter(Intent.ACTION_TIME_TICK));
(其中"myReceiver"是我想要的接收器的实例.)但是,当我尝试运行代码时,我收到以下错误:
无法启动接收器... android.content.ReceiverCallNotAllowedException:不允许IntentReceiver组件注册接收意图
★我们分析了这个问题并解决了这个问题(参考文献1)
这是调查此问题后的结果,我是处理程序成功此问题.所以我收集报告与android开发者分享.希望它有所帮助
结果如下:
❶ISSUE:*关于Widget的限制,当尝试通过显式源代码注册BroadcastReceiver时:(通过Manifest.xml注册BroadcastReceiver时无效)
❷示例:*BroadcastReceiver:ACTION_TIME_TICK消息就是一个例子:来自Android的文档指出:"你不能通过清单中声明的组件来接收它,只能通过用Context.registerReceiver()明确地注册它." (参考文献1)
❸PREVIOUSSOLUTION:*Code Snippet:context.registerReceiver(this,intentName); (1)
❹ERROR使用3★解决方案*当实现跟随(1)时,它虽然异常:android.content.ReceiverCallNotAllowedException:IntentReceiver组件不允许注册接收意图
★任何需要在Widget中注册BroadcastReceiver的人都有好消息:)我们可以注册成功的BroadcastReceiver
❺您的解决方案:*但是,我们可以通过使用的应用程序上下文而不是Widget上下文(*)代码片段来修复它:context.getApplicationContext.registerReceiver(this,intentName);
❻参考:*http://developer.android.com/reference/android/content/Intent.html#AC ...关于
❼目标环境:*SDK 2.3,在模拟器和NexusOne 2.3上,如果有任何成功的解决方案请更新我们的报告
❽注意*可能是小部件和应用程序的Context对象之间的区别,但我仍然不知道这个问题的确切原因.
如果您有更好的解决方案或更清楚地解释,请告诉我
我也解决了这个问题,但我仍然不知道这个问题的确切原因.
如果您有更好的解决方案或更清楚地解释,请告诉我
★结论:
● NG:使用Widget上下文注册BroadcastReceiver时
context.registerReceiver(this, intentName);
Run Code Online (Sandbox Code Playgroud)
- >它以为异常:
Unable to start receiver...android.content.ReceiverCallNotAllowedException: IntentReceiver components are not allowed to register to receive intents
Run Code Online (Sandbox Code Playgroud)
● OK:使用Application上下文时一切正常:
ontext.getApplicationContext.registerReceiver(this, intentName);
Run Code Online (Sandbox Code Playgroud)
★问: 还有我们的解决方案可以求解的问题:"无法注册新的广播消息通过实现Android小工具的源代码".
但我仍然关注两个问题:
问题 ❶:Widget …
例如
Intent intent = new Intent(this, SecondActivity.class);
Run Code Online (Sandbox Code Playgroud)
eclipse错误:Intent类型中的方法setClass(Context,Class)不适用于参数(FirstActivity.ClickEvent,Class)
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
Run Code Online (Sandbox Code Playgroud)
但这是正确的.谁能解释这两者之间的区别?谢谢.
我正在开发一个向用户显示通知的应用程序.通知的目的是使用户在另一个活动中轻松返回活动.我在我的应用程序中使用此代码来创建和显示通知.
notification = new Notification(R.drawable.icon,
"Notify",
System.currentTimeMillis());
notification.setLatestEventInfo(this, "App name",
"App message",
PendingIntent.getActivity(
this, 0,
new Intent(this, Main.class),
PendingIntent.FLAG_CANCEL_CURRENT));
notification.flags |= Notification.FLAG_ONGOING_EVENT;
nManager.notify(0, notification);
Run Code Online (Sandbox Code Playgroud)
但是当用户点击通知时,会启动相同活动的新实例,而不是之前用户使用的实例.
我认为这与PendingIntent有关,但是我找不到如何使Intent恢复以前暂停的活动实例而不是创建新实例.
谢谢.
notifications android android-activity android-pendingintent