这就是我关于使用context
发现:
1)。在一个Activity
本身,使用this
用于充气布局和菜单,注册上下文菜单,实例化部件,启动其他活动,一个Activity
中创建新Intent
,在Activity
可用实例的偏好,或其他方法。
膨胀布局:
View mView = this.getLayoutInflater().inflate(R.layout.myLayout, myViewGroup);
充气菜单:
@Override
public 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类(例如ContentObserver)的,使用上下文取代,如:
mContext = this; // Example 1
mContext = 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
用法的示例。
此链接可能会对您有所帮助。通过[this](http://droidpassion.blogspot.in/2012/03/all-about-context.html).. – Aju 2012-05-28 15:30:44