最終更新:
bokkuri_orz 2014年05月31日(土) 20:53:32履歴
●アクティビティ
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
●タスク
アプリケーションの起動単位。
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"のデータを表示できるアプリをシステムに問い合わせて、
システム側でアクティビティを選択して起動する。
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();
}
}
}
ブロードキャストが呼ばれるための設定は以下のどちらかの設定を行う。
<receiver android:name=".TestBroadcastReceiver">
<intent-filter>
<action android:name="TEST_DATA"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
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;
}
}
作成したサービスをアプリケーションで実行する場合には、
"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>
フローは以下の様になります。
- ブロードキャストレシーバーで、端末起動完了インテント(BOOT_COMPLETED)を受け取る。
- サービスを起動するインテントを送信する。
- インテントを受け取り、サービス起動。
<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>
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...


最新コメント