森林海
下面是我发现的关于使用context:1) .在Activity本身,使用this对于膨胀的布局和菜单,注册上下文菜单,实例化小部件,启动其他活动,创建新的Intent在Activity类中可用的首选项或其他方法。Activity.充气布局:View mView = this.getLayoutInflater().inflate(R.layout.myLayout, myViewGroup);充气菜单:@Overridepublic boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
this.getMenuInflater().inflate(R.menu.mymenu, menu);
return true;}注册上下文菜单:this.registerForContextMenu(myView);实例化小部件:TextView myTextView = (TextView) this.findViewById(R.id.myTextView);开始Activity:Intent mIntent = new Intent(this, MyActivity.class);this.startActivity(mIntent);实例化首选项:SharedPreferences mSharedPreferences = this.getPreferenceManager().getSharedPreferences();2) .对于应用程序范围的类,请使用getApplicationContext()由于此上下文存在于应用程序的生存期内。检索当前Android包的名称:public class MyApplication extends Application {
public static String getPackageName() {
String packageName = null;
try {
PackageInfo mPackageInfo = getApplicationContext().getPackageManager().getPackageInfo(getApplicationContext().
getPackageName(), 0);
packageName = mPackageInfo.packageName;
} catch (NameNotFoundException e) {
// Log error here.
}
return packageName;
}}绑定应用程序范围的类:Intent mIntent = new Intent(this, MyPersistent.class);MyServiceConnection mServiceConnection = new MyServiceConnection();
if (mServiceConnection != null) {
getApplicationContext().bindService(mIntent, mServiceConnection, Context.BIND_AUTO_CREATE);}3) .对于侦听器和其他类型的Android类(例如Content观察者),使用上下文替换如下:mContext = this; // Example 1mContext = context; // Example 2哪里this或context类(活动等)的上下文。Activity语境替代:public class MyActivity extends Activity {
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
}}侦听器上下文替换:public class MyLocationListener implements LocationListener {
private Context mContext;
public MyLocationListener(Context context) {
mContext = context;
}}ContentObserver语境替代:public class MyContentObserver extends ContentObserver {
private Context mContext;
public MyContentObserver(Handler handler, Context context) {
super(handler);
mContext = context;
}}4) .为BroadcastReceiver(包括内联/嵌入式接收器),使用接收器自己的上下文。外部BroadcastReceiver:public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (action.equals(Intent.ACTION_SCREEN_OFF)) {
sendReceiverAction(context, true);
}
private static void sendReceiverAction(Context context, boolean state) {
Intent mIntent = new Intent(context.getClass().getName() + "." + context.getString(R.string.receiver_action));
mIntent.putExtra("extra", state);
context.sendBroadcast(mIntent, null);
}
}}内联/嵌入BroadcastReceiver:public class MyActivity extends Activity {
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final boolean connected = intent.getBooleanExtra(context.getString(R.string.connected), false);
if (connected) {
// Do something.
}
}
};}5) .对于服务,使用服务自己的上下文。public class MyService extends Service {
private BroadcastReceiver mBroadcastReceiver;
@Override
public void onCreate() {
super.onCreate();
registerReceiver();
}
private void registerReceiver() {
IntentFilter mIntentFilter = new IntentFilter();
mIntentFilter.addAction(Intent.ACTION_SCREEN_OFF);
this.mBroadcastReceiver = new MyBroadcastReceiver();
this.registerReceiver(this.mBroadcastReceiver, mIntentFilter);
} }6) .对于烤面包,一般使用getApplicationContext(),但在可能的情况下,使用活动、服务等传递的上下文。使用应用程序的上下文:Toast mToast = Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG);mToast.show();使用从源传递的上下文:public static void showLongToast(Context context, String message) {
if (context != null && message != null) {
Toast mToast = Toast.makeText(context, message, Toast.LENGTH_LONG);
mToast.show();
}}最后,不要用getBaseContext()正如Android的框架开发人员所建议的那样。最新情况:添加示例Context使用。