の使用に関して私が見つけたものは次のcontext
とおりです。
1) . 内で、レイアウトとメニューの拡張、コンテキスト メニューの登録、ウィジェットのインスタンス化、他のアクティビティの開始、内での新規作成、環境設定のインスタンス化、または で使用可能なその他のメソッドに使用しActivity
ます。this
Intent
Activity
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) . Listener およびその他のタイプの Android クラス (ContentObserver など) には、次のようなコンテキスト置換を使用します。
mContext = this; // Example 1
mContext = context; // Example 2
this
orはcontext
クラスのコンテキスト (Activity など) です。
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) . Toasts の場合、通常は を使用getApplicationContext()
しますが、可能であれば、Activity、Service などから渡されたコンテキストを使用します。
アプリケーションの使用コンテキスト:
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
ます。