LoginSignup
11
9

More than 5 years have passed since last update.

Android開発〜画面遷移(インテント)〜

Posted at

はじめに

こんにちは。某学校でプログラミング等の勉強中のサーバーサイドのプログラマーのワタタクです。:relaxed:
今回もAndroid開発していきましょう。
今回のテーマは「インテント」です。

対象者

  • Javaがなんとなく書ける人。
  • Android開発が曖昧だけど多少できる人。 

2種類のインテント

  • 明示的インテント
    → 起動するアクティビティクラスを指定する。

  • 暗黙的インテント
    → どんな種類のアクティビティを起動したいのかを表すURIとアクション(後述)を指定する。
    Android OS が URI とアクションをもとに該当するアクティビティを探し出して起動する。
    起動先 が複数ある場合はアプリチューザが表示される。

では次にどのように処理すれば良いか見ていきます。

明示的インテント

明示的インテントで画面遷移させるには以下の通り。
1)AndroidManifest.xmlにアクティビティを登録する

<?xml version="1.0" encoding="utf-8"?>
<manifest ...... >

    <application ........ >
        <activity>

                〜メインアクティビティ〜

        </activity>

        <!--追加-->
        <activity android:name="パッケージ名+クラス名">

    </application>

</manifest>

*アクティビティクラスがルートパッケージ直下なら「.クラス名」でOK!

*上記手順は「File>New>Activity>Empty Activity」から作ると勝手にやってくれるので便利。

2)画面起動
○今の画面から別画面を起動する方法。
①Intentオブジェクト生成。

~~~~~~~~ = new Intent(コンテキスト, 起動先アクティビティ)

②①を引数にstartActivity()メソッドを実行する。

3)データ渡し
○起動先アクティビティにデータを渡す方法はputExtra("名前", 値)を使う。

4)データ受け取り
○起動先アクティビティでデータを受け取る方法は以下の通り。
①Intentオブジェクト取得

Intent intent = getIntent();

②Bundleオブジェクト取得

Bundle extra = intent.getExtra();

③Bundleのgetデータ型メソッドを使ってデータ取得

ForResult

○起動先アクティビティ終了後に元アクティビティで処理を行うとき
1)アクティビティの起動には以下のメソッド

startActivityForResult(インテントオブジェクト, リクエストコード)

2)起動先アクティビティのfinish()の直前に以下のメソッド

setResult(リザルトコード, インテントオブジェクト)

*リザルトコード

  • RESULT_OK
  • RESULT_CANSELED

3)1)と2)を実行し元アクティビティで以下のメソッドを実行する。

onActivityResult()

引数は3つ

  • int requestCode:1)の第二引数で指定した整数値
  • int resultCode:2)の第一引数で指定した定数値
  • Intent data

サンプルコード

ForResultSampleActivity.java(起動元)
Intent intent = new Intent(ForResultSampleActivity.this,RatingEvaluateActivity.class);
inten1.putExtra("name", name);
startActivityForResult(intent, RATING_EVALUATE);
RatingEvaluateActivity.java(起動先)
Intent intent = getIntent();
String name = intent.getStringExtra("name");

暗黙的インテント

暗黙的インテントで他のアプリを起動する手順は、以下の通り。
1) URI オブジェクトを生成する。

Uri uri = Uri.porse(URI文字列(後述));

2) Intent オブジェクトを生成する。

Intent intent = new Intent(アクションを表す定数。(後述), uri);

3) アクティビティを起動する。

startActivity(intent) ;

URI

Android OS 標準のアプリの URI は以下の通り。

・ブラウザ → http:// ..... , https:// .....
・地図 → geo: 緯度 , 経度
geo: o , o?g = 検索文字列

(注)
日本語の検索キーワードは、
URLEncoder.encode([ キーワード ], [ エンコード形式 (URF-8 等 )]) で エンコードする。

・電話 → tel: 電話番号

アクション

Intent クラスの定数フィールドを使う。

  • ACTION_VIEW → 画面表示
  • ACTION_CALL → データをもとに電話をかける。
  • ACTION_DIAL → 電話をかける画面を表示
  • ACTION_SEND → メール /SMS 送信

サンプルコード

①地図アプリに遷移する

try {
     TextView etKeyword = findViewById(R.id.etKeyword);
     String keyword = etKeyword.getText().toString();
     keyword = URLEncoder.encode(keyword, "utf-8");
     Uri uri = Uri.parse("geo:0,0?q=" + keyword);
     Intent intent = new Intent(Intent.ACTION_VIEW, uri);
     startActivity(intent);
} catch(UnsupportedEncodingException ex) {
     Log.e("MapSearchActivity", "keyword変換失敗", ex);
}

②ブラウザに遷移する

String url = "http:://www.~~~.~~";
Uri uri = Uri.parse(url);

Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);

以上。
もし何か間違っている等のご指摘があればご連絡ください。
最後まで読んで頂きありがとうございました。

11
9
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
9