Android-Broadcastの詳細と要約
6579 ワード
一、放送の実現
1、メールの放送を傍受する
2、Activity内にネストされたブロードキャストが実現され、Activityを更新するUIインタフェースで多く使われる.
二、登録放送受信
1、静態登録
2、動的登録
uiReceiver=new UIReceiver(); IntentFilter filter=new IntentFilter(); filter.addAction("android.janmac.location"); registerReceiver(uiReceiver, filter);
三、放送を送る
まず、情報の送信が必要な場所で、送信する情報とフィルタリングのための情報(Action、Categoryなど)を1つのIntentオブジェクトにロードし、Contextを呼び出す.sendBroadcast()、sendOrderBroadcast()またはsendStickyBroadcast()メソッドは、Intentオブジェクトをブロードキャストで送信します.
Intentが送信されると、登録されているすべてのBroadcastReceiverは、登録時のIntentFilterが送信されたIntentと一致するかどうかをチェックし、一致するとBroadcastReceiverのonReceive()メソッドを呼び出します.したがって、BroadcastReceiverを定義するには、onReceive()メソッドを実装する必要があります.
Intent uiIntent=new Intent(); Bundle bundle=new Bundle(); bundle.putString("longitude", longitude); bundle.putString("latitude", latitude); uiIntent.putExtras(bundle); uiIntent.setAction("android.janmac.location"); Log.v("bundle", uiIntent.getExtras().getString("longitude")); context.sendBroadcast(uiIntent);
四、BroadcastReceiverはどうやって使いますか.
ブロードキャストが来るたびにBroadcastReceiverオブジェクトが再作成され、onReceive()メソッドが呼び出され、実行が完了すると破棄されます.onReceive()メソッドが10秒以内に実行されない場合、Androidはプログラムに応答していないと判断します.そのためBroadcastReceiverでは時間のかかる操作はできませんが、ANR(Application NoResponse)のダイアログボックスが表示されます.
時間のかかる作業を完了する必要がある場合は、Intentをサービスに送信し、サービスによって完了する必要があります.ここではサブスレッドを使用して解決することはできません.BroadcastReceiverのライフサイクルが短いため、サブスレッドはまだBroadcastReceiverが終了していない可能性があります.
BroadcastReceiverが終了すると、BroadcastReceiverのプロセスは、システムがメモリを必要とする場合に優先的に殺されやすくなります.これは、空のプロセス(アクティブなコンポーネントのないプロセス)に属するためです.ホストプロセスが殺されると、作業中のサブスレッドも殺されます.したがって,サブスレッドを用いて解決することは信頼できない.
五、その他
サービスブロードキャストBroadcastによるActivity UIの更新
http://www.eoeandroid.com/thread-68005-1-1.html
秩序放送など他の面について-AndroidのBroadCastReceiver
http://yangguangfu.iteye.com/blog/1063732
1、メールの放送を傍受する
public class SmsBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Object[] pdus=(Object[])intent.getExtras().get("pdus");
for(Object pdu:pdus)
{
byte[] date=(byte[])pdu;
SmsMessage message=SmsMessage.createFromPdu(date);
String sender=message.getOriginatingAddress();
String body=message.getMessageBody();
if(sender.equals(AppUtil.herPhone)&&body.regionMatches(0, AppUtil.herSmsText, 0, 18))
{
//Toast.makeText(context, body, Toast.LENGTH_LONG).show();
String [] bodyArray=body.split(" ");
String longitude=bodyArray[3];
String latitude=bodyArray[4];
Intent uiIntent=new Intent();
Bundle bundle=new Bundle();
bundle.putString("longitude", longitude);
bundle.putString("latitude", latitude);
uiIntent.putExtras(bundle);
uiIntent.setAction("android.janmac.location");
Log.v("bundle", uiIntent.getExtras().getString("longitude"));
context.sendBroadcast(uiIntent);
abortBroadcast();
}
}
}
}
2、Activity内にネストされたブロードキャストが実現され、Activityを更新するUIインタフェースで多く使われる.
private class UIReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {
Log.v("location","uireceiver !");
Bundle bundle=new Bundle();
bundle=intent.getExtras();
herLongitude=Double.valueOf(bundle.getString("longitude"));
herLatitude=Double.valueOf(bundle.getString("latitude"));
HerLocationInit();
button.setText(" !");
}
}
二、登録放送受信
1、静態登録
2、動的登録
uiReceiver=new UIReceiver(); IntentFilter filter=new IntentFilter(); filter.addAction("android.janmac.location"); registerReceiver(uiReceiver, filter);
三、放送を送る
まず、情報の送信が必要な場所で、送信する情報とフィルタリングのための情報(Action、Categoryなど)を1つのIntentオブジェクトにロードし、Contextを呼び出す.sendBroadcast()、sendOrderBroadcast()またはsendStickyBroadcast()メソッドは、Intentオブジェクトをブロードキャストで送信します.
Intentが送信されると、登録されているすべてのBroadcastReceiverは、登録時のIntentFilterが送信されたIntentと一致するかどうかをチェックし、一致するとBroadcastReceiverのonReceive()メソッドを呼び出します.したがって、BroadcastReceiverを定義するには、onReceive()メソッドを実装する必要があります.
Intent uiIntent=new Intent(); Bundle bundle=new Bundle(); bundle.putString("longitude", longitude); bundle.putString("latitude", latitude); uiIntent.putExtras(bundle); uiIntent.setAction("android.janmac.location"); Log.v("bundle", uiIntent.getExtras().getString("longitude")); context.sendBroadcast(uiIntent);
四、BroadcastReceiverはどうやって使いますか.
ブロードキャストが来るたびにBroadcastReceiverオブジェクトが再作成され、onReceive()メソッドが呼び出され、実行が完了すると破棄されます.onReceive()メソッドが10秒以内に実行されない場合、Androidはプログラムに応答していないと判断します.そのためBroadcastReceiverでは時間のかかる操作はできませんが、ANR(Application NoResponse)のダイアログボックスが表示されます.
時間のかかる作業を完了する必要がある場合は、Intentをサービスに送信し、サービスによって完了する必要があります.ここではサブスレッドを使用して解決することはできません.BroadcastReceiverのライフサイクルが短いため、サブスレッドはまだBroadcastReceiverが終了していない可能性があります.
BroadcastReceiverが終了すると、BroadcastReceiverのプロセスは、システムがメモリを必要とする場合に優先的に殺されやすくなります.これは、空のプロセス(アクティブなコンポーネントのないプロセス)に属するためです.ホストプロセスが殺されると、作業中のサブスレッドも殺されます.したがって,サブスレッドを用いて解決することは信頼できない.
五、その他
サービスブロードキャストBroadcastによるActivity UIの更新
http://www.eoeandroid.com/thread-68005-1-1.html
秩序放送など他の面について-AndroidのBroadCastReceiver
http://yangguangfu.iteye.com/blog/1063732