プログラミング系のネタをまとめていきます。

Androidアプリ4大要素

  • Activity
  • Intent
  • Service
  • ContentProvider

アクティビティ

参考サイト


Androidアプリの4大要素 Activity
http://www.sakc.jp/blog/archives/24364

アクティビティとタスク


●アクティビティ
Activityのこと。

●ルートアクティビティ
タスクを開始するアクティビティのこと。
通常だと、AndroidManifest.xml の タグのなかに、以下のフィルターを持つ。

<intent-filter>
  <action android:name="android.intent.action.MAIN" />  
  <category android:name="android.intent.category.LAUNCHER" />  
</intent-filter>

●スタック
アクティビティの一連の起動を管理している。
ルートアクティビティを起点に、同タスク内で生成されるアクティビティは上に積まれていく。
一番上のアクティビティが全面に出て、動作中のアクティビティとなる。
  • スタック内の一番下のアクティビティ : root activity
  • スタック内の一番上のアクティビティ : running activity

●タスク
アプリケーションの起動単位。

ActivityのlaunchMode


AndroidManifest.xmlのactivity要素で設定できる属性。
launchMode複数起動備考
standardありインテントが発行された際、常に新しいActivityインスタンスが生成される(onCreate)
singleTopありスタックのトップにそのActivityが存在する場合はインスタンスを生成しないで使いまわす(onNewIntent)
トップにない場合はインスタンスを生成する(onreate)。
singleTask無しスタック内にそのActivityが存在する場合は、それより上のActivityを全て終了させる。指定のActivityはonNewIntentが呼ばれる。
singleInstance無し1タスク内に、1個のActivityを持つモード。別のActivityを呼び出す際、必ずタスクも別のものになる。

参考サイト

タスクについて図解されていてわかりやすいです。
http://techblog.qoncept.jp/?p=102

※親和性(Affinity)、タスクの起動モード等、細かい説明はこちら。
http://www.techdoctranslator.com/android/guide/act...


http://www.techdoctranslator.com/android/guide/man...

インテント

明示的インテント


アクティビティの名前を明示的に指定する。
通常、同一アプリケーション内のアクティビティ間で利用される。

Intent intent = new Intent(this, MyActivity.class);
startActivity(intent);

暗黙的インテント


Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.google.com/"));
startActivity(intent);

スキーマが"http"、ホストが"www.google.com"のデータを表示できるアプリをシステムに問い合わせて、
システム側でアクティビティを選択して起動する。

参考サイト


明示的インテントと暗黙的インテント
http://android.keicode.com/basics/intent-type.php

BroadcastReceiver

ブロードキャストを受け取る

Activityから送信
public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// :
		
		Button btn = (Button) findViewById(R.id.buttonBroadcast);
		btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) 
			{
				Intent intent = new Intent("TEST_DATA");	// 独自のアクション
				intent.addCategory(Intent.CATEGORY_DEFAULT);
				intent.putExtra("data", "test string");
				sendBroadcast(intent);						// ブロードキャスト送信
			}
		});
	}

受け取ったインテントのアクションとカテゴリを表示。
public class TestBroadcastReceiver extends BroadcastReceiver {
	@Override
	public void onReceive(Context arg0, Intent arg1)
	{
		Bundle bundle = arg1.getExtras();
		{
			String str = arg1.getAction() + "\n";
			if(arg1.getCategories() != null)
			{
				for (String strData : arg1.getCategories())
				{
					str += strData + "\n";
				}
			}
			str += bundle.getString("data") + "\n";
			Toast.makeText(arg0, str, Toast.LENGTH_SHORT).show();
		}
	}
}


ブロードキャストが呼ばれるための設定は以下のどちらかの設定を行う。
呼び出し登録 1 : AndroidManifestで指定

<receiver android:name=".TestBroadcastReceiver">
    <intent-filter>
        <action android:name="TEST_DATA"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
</receiver>

呼び出し登録 2 : プログラム上で指定

public class MainActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// :
		
		TestBroadcastReceiver receiver = new TestBroadcastReceiver();
		IntentFilter filter = new IntentFilter();
		filter.addAction("TEST_DATA");
		filter.addCategory(Intent.CATEGORY_DEFAULT);
		registerReceiver(receiver, filter);
		
		// :
	}


参考サイト


Androidアプリの4大要素とインテント 明示的なIntent
http://www.sakc.jp/blog/archives/24514

Androidアプリの4大要素とインテント 暗黙的なIntent
http://www.sakc.jp/blog/archives/24833

Androidアプリの4大要素 BroadcastReceiver
http://www.sakc.jp/blog/archives/24996



サービス


バックグラウンドで処理を行うときに利用する。

package com.Test.TestIntent;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class BackgroundService extends Service {

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

}

AndroidManifest.xmlへ登録


作成したサービスをアプリケーションで実行する場合には、
"service"タグを使用してマニフェストファイルに登録する。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.sample.service"
    android:versionCode="1"
    android:versionName="1.0">
 
    <application android:icon="@drawable/icon" android:label="@string/app_name">
    
    	: // メインのアクティビティ等

        <!-- Serviceは "service"タグを利用してマニフェストファイルに登録する -->
        <service android:name=".BackgroundService" />
    </application>
</manifest> 

端末起動時にサービスを起動する


フローは以下の様になります。
  1. ブロードキャストレシーバーで、端末起動完了インテント(BOOT_COMPLETED)を受け取る。
  2. サービスを起動するインテントを送信する。
  3. インテントを受け取り、サービス起動。

起動時のブロードキャストを受信するパーミッション

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<receiver
    android:name=".BootReceiver"
    android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</receiver>

起動時ブロードキャストのテスト

以下のコマンドでのテスト出来る。

adb shell am broadcast -a android.intent.action.BOOT_COMPLETED

BOOT_COMPLETEDの注意点

android.intent.action.BOOT_COMPLETED は 外部SDが端末にマウントされる前にブロードキャストされるので、
外部SDにインストールされている場合、Broadcast Intent を受け取ることができません。
また、アプリが外部SDにインストールされている場合、外部SDがアンマウントされると
Service が kill され、マウントしても再開されません。

●内部ストレージのみ保存の設定

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    android:installLocation="internalOnly"
    :
    >

参考サイト


[Android]ブロードキャストインテントを受け取る
http://mousouprogrammer.blogspot.jp/2013/03/androi...

Broadcast Intent 一覧
http://d.hatena.ne.jp/Raspberry-Farad/20090920/125...

端末起動時(ブート時)にサービスを起動する方法
http://team-hiroq.com/blog/android/broadcastreceiv...

Menu

メインコンテンツ

プログラミング

機器

Macツール

各種情報

Wiki内検索

おまかせリンク

Androidアプリ

AdSense

技術書


管理人/副管理人のみ編集できます