phpAndroid開発 虎の巻

暗黙的インテント

カテゴリ:アプリケーションコンポーネント

暗黙的インテントでの起動

例:ブラウザの起動

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.yahoo.co.jp/"));
startActivity(intent);

具体的なアプリは指定せず、URLを指定。対応するアプリが自動的に起動される。
対応するアプリが複数ある場合、選択する画面が出る。

ACTION_DIALの例:ダイヤラーの起動

	Intent intent = new Intent(Intent.ACTION_DIAL,Uri.parse("tel:0123456789"));
	startActivity(intent);

ACTION_CALLの例:電話をかける

	Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:0123456789"));
	startActivity(intent);
注:パーミッションを設定する。 <uses-permission android:name="android.permission.CALL_PHONE"/>

ACTION_GET_CONTENTの例:ギャラリーから画像を指定

	Intent intent = new Intent(Intent.ACTION_GET_CONTENT);

	intent.setType("image/*");
	startActivityForResult(intent, 1);

※データを取得しImageViewに表示

※onActivityResult内

	if( resultCode == RESULT_OK ){
		Uri uri= data.getData();
		ImageView imageView1 = (ImageView)findViewById(R.id.imageView1);
		imageView1.setImageURI(uri);
	}

カメラを起動し画像を取得

Uri PictureUri; を宣言しておく。

    String filename = System.currentTimeMillis() + ".jpg";  
    ContentValues values = new ContentValues();  
    values.put(MediaStore.Images.Media.TITLE, filename);  
    values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");  
    PictureUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);  
  
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
    intent.putExtra(MediaStore.EXTRA_OUTPUT, PictureUri);  
    startActivityForResult(intent, 1);

※データを取得しImageViewに表示

※onActivityResult内

	if( resultCode == RESULT_OK ){
		ImageView imageView = (ImageView)findViewById(R.id.imageView1); 
		imageView.setImageURI(PictureUri);
	}

■intent-filter

暗黙的インテントに対し、起動される側のアプリを作るには intent-filter を AndroidManifest.xml に指定する。
intent-filterとは受け取るインテントをフィルタリングするものと言える。

intent-filterの例

<activity android:name="com.example.viewtest.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter>
action対応するアクション
category対応するカテゴリ(Intentオブジェクトに addCategoryで指定)
data対応するデータ

例:特定サイトのURLに対応し起動

<intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="http" android:host="www.dicre.com" android:pathPrefix="/android/" /> </intent-filter>

その場合のURL取得

String url = getIntent().getDataString();

カテゴリ:アプリケーションコンポーネントの記事