Đây là những gì tôi đã tìm thấy liên quan đến việc sử dụng các context
:
1). Trong phạm vi Activity
, sử dụng this
để tăng bố cục và menu, đăng ký menu ngữ cảnh, khởi chạy tiện ích, bắt đầu các hoạt động khác, tạo Intent
mới trong một Activity
, tùy chọn instantiating hoặc các phương pháp khác có sẵn trong Activity
.
Inflate bố trí:
View mView = this.getLayoutInflater().inflate(R.layout.myLayout, myViewGroup);
Bơm menu:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
this.getMenuInflater().inflate(R.menu.mymenu, menu);
return true;
}
Register menu ngữ cảnh:
this.registerForContextMenu(myView);
nhanh chóng phụ tùng:
TextView myTextView = (TextView) this.findViewById(R.id.myTextView);
Bắt đầu một Activity
:
Intent mIntent = new Intent(this, MyActivity.class);
this.startActivity(mIntent);
sở thích nhanh chóng:
SharedPreferences mSharedPreferences = this.getPreferenceManager().getSharedPreferences();
2). Đối với lớp ứng dụng rộng, sử dụng getApplicationContext()
vì ngữ cảnh này tồn tại trong thời gian tồn tại của ứng dụng.
Lấy tên của gói Android hiện tại:
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;
}
}
Bind một lớp ứng dụng rộng:
Intent mIntent = new Intent(this, MyPersistent.class);
MyServiceConnection mServiceConnection = new MyServiceConnection();
if (mServiceConnection != null) {
getApplicationContext().bindService(mIntent, mServiceConnection, Context.BIND_AUTO_CREATE);
}
3). Đối với Thính giả và các loại khác của các lớp học Android (ví dụ ContentObserver), sử dụng một bối cảnh thay như:
mContext = this; // Example 1
mContext = context; // Example 2
nơi this
hoặc context
là bối cảnh của một lớp (Hoạt động, vv). thay
Activity
bối cảnh:
public class MyActivity extends Activity {
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
}
}
Listener bối cảnh thay:
public class MyLocationListener implements LocationListener {
private Context mContext;
public MyLocationListener(Context context) {
mContext = context;
}
}
ContentObserver
bối cảnh thay:
public class MyContentObserver extends ContentObserver {
private Context mContext;
public MyContentObserver(Handler handler, Context context) {
super(handler);
mContext = context;
}
}
4). Đối với BroadcastReceiver
(bao gồm cả máy thu được nhúng/được nhúng), hãy sử dụng ngữ cảnh của người nhận.
ngoài 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);
}
}
}
inlined/Embedded 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). Đối với Dịch vụ, sử dụng ngữ cảnh riêng của dịch vụ.
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). Đối với bánh mì nướng, thường sử dụng getApplicationContext()
, nhưng nếu có thể, sử dụng bối cảnh truyền từ một hoạt động, dịch vụ vv
Sử dụng bối cảnh của ứng dụng:
Toast mToast = Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG);
mToast.show();
Sử dụng bối cảnh truyền từ một nguồn :
public static void showLongToast(Context context, String message) {
if (context != null && message != null) {
Toast mToast = Toast.makeText(context, message, Toast.LENGTH_LONG);
mToast.show();
}
}
Và cuối cùng, không sử dụng getBaseContext()
như tư vấn bởi các nhà phát triển khuôn khổ của Android.
CẬP NHẬT: Thêm ví dụ về việc sử dụng Context
.
Liên kết này có thể giúp bạn. Đi qua [this] (http://droidpassion.blogspot.in/2012/03/all-about-context.html) .. – Aju