是什么区别getContext(),getApplicationContext(),getBaseContext(),和" this"?
虽然这是一个简单的问题,但我无法理解它们之间的基本区别.如果可能,请举出一些简单的例子.
我用Google搜索并阅读了Java文档,但我有点困惑.有人Context可以用简单的英语解释一下是什么吗?
原谅我,这让我疯了,我会尝试通过我的愤怒发布一些易读的东西.
我在这里看到了几个关于如何检查屏幕是否被锁定的帖子,但没有一个对我有效.它都会检测实际屏幕是否关闭(如果它已锁定).
我有一个音乐播放的游戏.按下锁定按钮后,它将继续播放.我最初在OnStop上停止了音乐,但应用程序会在锁定后重新启动,因此音乐最终会再次启动.
然后,我在清单中添加了KeyboardHidden | orientation.这使得它不会重新启动应用程序,但OnStop似乎不再被调用.
我已经尝试使用PowerManager查看屏幕是否打开/关闭,这有效,但没有帮助.(我可以让音乐停在那里,但只要再次按下锁定按钮,音乐就会立即启动)
请有人向我解释一下:
Intent intent = new Intent(Context, AlarmReceiver.class);
Run Code Online (Sandbox Code Playgroud)
我从来没有理解过,我认真地认为如果有人不试图向我解释这个问题,我永远不会理解.整个上下文的事情让我很困惑.有时它的工作方式如下:
Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
Run Code Online (Sandbox Code Playgroud)
有时它不会像那样工作,但它只接受:
Intent intent = new Intent(context, AlarmReceiver.class);
Run Code Online (Sandbox Code Playgroud)
有时它:
Intent intent = new Intent(this, AlarmReceiver.class);
Run Code Online (Sandbox Code Playgroud)
等等等等.
我理解上下文的基础但有多少?为什么eclipse会一次又一次地给我一个错误呢?为什么我们有时需要声明上下文?:
Context context;
Run Code Online (Sandbox Code Playgroud)
我无法为所有情况找到合适的背景如何在每种情况下知道什么是正确的?
我对所有这些的用法非常困惑,我们应该在哪里使用它们.
有人可以向我解释Android中的活动,背景和意图是什么吗?
我阅读了Android文档,但我无法理解这些概念.
我是Android开发的新手,在改变活动时遇到了一些问题.我试图从一个方法中改变活动,但我收到错误,cannot resolve method startActivity并在参数结束时出错Cannot resolve constructor 'Intent (...)'.我在这里发现了一个同样问题的问题,并试图将他们的回复实施到我的程序中但没有快乐.
这是代码:
public void open301(View view) {
startActivity(new Intent(CustomAdapter.this, ThreeZeroOne.class));
}
Run Code Online (Sandbox Code Playgroud)
在查看上面链接的问题的回复之前,代码看起来像这样,具有相同的错误:
public void open301(View view) {
Intent openThree = new Intent(this,ThreeZeroOne.class);
startActivity(openThree);
}
Run Code Online (Sandbox Code Playgroud)
完整代码:
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.content.Intent;
public class CustomAdapter extends BaseAdapter {
String[] result;
Context context;
int[] imageId;
private static LayoutInflater inflater = null;
public CustomAdapter(selectGame …Run Code Online (Sandbox Code Playgroud) 使用下面的代码,我试图访问存储在asset/raw文件夹中的文件,但获取null和
E/ERR: file:/android_asset/raw/default_book.txt (No such file or directory)
Run Code Online (Sandbox Code Playgroud)
错误,我的代码是:
private void implementingDefaultBook() {
String filePath = Uri.parse("file:///android_asset/raw/default_book.txt").toString();
File file = new File(filePath);
try {
FileInputStream stream = new FileInputStream(file);
} catch (Exception e) {
e.printStackTrace();
Log.e("ERR ", e.getMessage());
} catch (OutOfMemoryError e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
根据这个答案或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)
考虑到内存泄漏,速度,一般性能,所有这些可能性之间的更好方法是什么?
所以我一直在官方网站上进行Android Developer培训,他们希望我们最终实例化我们的数据库.
所以他们告诉我们使用这段代码:
FeedReaderDbHelper mDbHelper = new FeedReaderDbHelper(getContext());
Run Code Online (Sandbox Code Playgroud)
但是,我收到了该getContext()方法的错误.它声明它找不到该方法的符号.
所以我搜索了源代码,并且找不到View类中的那个方法.这是一种弃用的方法吗?如果这不是一个选项,还有其他方法我们可以抓住视图的上下文吗?
谢谢!