Contextクラス

android.contentContextクラス

Contextとは?

 状態:-  閲覧数:1,883  投稿日:2013-11-07  更新日:2014-03-23  
Android OS の環境やリソースへのアクセス窓口 となるコンポーネント
・アプリケーション環境に関するグローバルな情報を受け渡すために使用
・具体的には、データベースやファイル、アプリで利用する画像データなどへのアクセスの窓口となるほか、他のアプリとの連携や、アプリ情報へのアクセスの窓口ともなる
・つまり、Contextクラスを通して、アプリケーションのリリース、クラス、アプリケーションレベルの オペレーション(アクティビティの起動、ブロードキャスト、インテントの受信など)にアクセスする ことが可能
・簡単に言えば、アプリ全体の情報を管理し、何から起動されたか、現在の状態、何にアクセスしょうとしているか等の情報を受け渡すために 使用される

・Context はライフサイクルを持っている
・端末の様々な 状態遷移 の管理も、Context の仕事
・Context にはいくつかの種類が存在する
・アプリを作る際には、 どの種類の Context かを意識する必要がある

Contextの種類

 閲覧数:484 投稿日:2013-11-08 更新日:2014-03-23 

3種類


Context型オブジェクトは、3種類存在
・1.Application Context
・2.Activity Context
・3.Service


1.Application


Application Context
・Context の一種で、Android アプリ全体を統括する
・アプリケーションが起動してから終了するまでのライフサイクルを管理
・Applicationと運命共同体
・Activity クラス内にて、getApplicationContext() で「Applicationのインスタンスオブジェクト」を取得可能


2.Activity


Activity Context
・Context の一種で、1つの画面を統括
・MVC モデルの、コントローラに相当する役割を担う
・1つの画面が構成され、バックグラウンドに移るか、終了するまでのライフサイクルを管理
・Activityと運命共同体
・Activity クラスが Context クラスを継承している
・引数に this を入れるだけで使用可能


3.Service


・Context の一種で、画面を持たず、バックグラウンドで動作
・バックグラウンドで常に動き続ける常駐型の Service と、都度起動と終了をする Service の2種類がある
・Service が作られてから終了するまでのライフサイクルを管理

クラス構成

 閲覧数:363 投稿日:2013-11-08 更新日:2014-03-23 

親クラス



java.lang.Object
  ↳ android.content.Context



コード例

 閲覧数:495 投稿日:2014-03-23 更新日:2014-03-23 

例1


ファイル入出力
・ファイルの書き込みと読み込み、そして読み取った内容を画面に表示

▼ /Context/src/android/style/FileReadActivity.java
package android.style;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

public class FileReadActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

       // ベースのレイアウト取得
LinearLayout parent = (LinearLayout) findViewById(R.id.parent);

String f = "sample.txt";
createTable(f); // ファイル作成

try {
BufferedReader br = new BufferedReader(new InputStreamReader(// InputStreamReaderとBufferedReaderクラスを使って、1行ずつ処理
openFileInput(f)));// openFileInputメソッドで入力ストリームを取得
try {
String line;
while ((line = br.readLine()) != null) {
TextView tv = new TextView(this);
tv.setTypeface(Typeface.MONOSPACE); // フォントを等幅にするために、setTypefaceメソッドで、フォントファミリーにTypeface.MONOSPACEの指定を
tv.setText(line);// 取得した1行は、それぞれテキストビューに設定
parent.addView(tv); // レイアウト(ビュー)に追加
}
} finally {
br.close();
}
} catch (IOException e) {
}
}

// ファイルに九九表を出力する
public void createTable(String f) {
try {
PrintWriter pw = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(openFileOutput(f,
Context.MODE_WORLD_READABLE))));
try {
for (int i = 1; i < 10; i++) {
for (int j = 1; j < 10; j++) {
pw.printf("%3d ", i * j);
}
pw.printf("\n");
}
} finally {
pw.close();
}
} catch (IOException e) {
}
}
}


・xml … レイアウト
▼/res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:id="@+id/parent"
   >
</LinearLayout>



コメント投稿(ログインが必要)



類似度ページランキング
順位 ページタイトル抜粋
1 Contextクラス 90
2 Dateクラス 59
3 TextViewクラス 57
4 EditTextクラス 57
5 Calendarクラス 57
6 Fragmentクラス 57
7 LinearLayoutクラス 56
8 Toastクラス 56
9 Bundleクラス 53
10 Canvasクラス 53
11 TableLayoutクラス 50
12 FrameLayoutクラス 50
13 AlertDialogクラス 50
14 Logクラス 50
15 TableRowクラス 48
16 ListViewクラス 48
17 CardViewクラス 48
18 Viewクラス 47
19 Timeクラス 47
20 ViewGroupクラス 45
2024/9/21 20:46 更新