Android階段を進む道-四大コンポーネントのBroadcaseReceiver
53401 ワード
周知のようにBroadcaseReceiverはAndroidの4つのコンポーネントの一つであり、ラジオ、ラッパとも呼ばれています.この記事はBroadcaseReceiverの詳細な使い方です.使い方についてはちょっと違っています.静的登録、動的登録、秩序ある放送、無秩序放送、カスタム放送、放送ブロックなどの機能の詳細な使い方基礎概念 放送分類 登録方式 使い方と注意点 使い方 MainActivity Costom BroadcaseReceiverのコード(カスタム放送) DynamicBroadcaseReceiverのコード(動的登録) Local BroadcaseReceiverのコード(ローカル放送) Static BroadcaseReceiverのコード(静的登録) QuickBroadcaseReceiver(優先度の放送受信者が傍受処理を行い、静的放送がブロックされ、優先度の放送しか受信できない)のコード activity.xml 明細書ファイル 基本概念
ラジオの分類秩序ある放送(ブロック可能) 無秩序放送(ブロック不可) 登録方式動的登録(コード登録) 静的登録(リスト登録) 使い方と注意点
1.放送を使用してまず一つのクラスを作成してBroadcaseReceiverを継承し、次にOneReceiver方法を書き換える(放送を受信する操作はここで実行する)
2.依頼されたActivityなどの寄生体の中で放送を送る(順序付けと無秩序、以下で紹介する)
3.Androidの4つのコンポーネントの一つとして、必要不可欠な自然は動的登録と静的なグループ測定(リスト登録)があります.
追加:Android 7.0に対して、動的登録を実現するのが一番いいです.
4.私はモニターシステムの放送を持っていません.電源を入れるとか、ネットの状態などではなく、自分で定義した放送を便利にするために、興味がある人は一挙にラジオ処理を行うことができます.
5.ラジオ登録が必要です.ログアウトも必要です.いいえ、OOMの隠れたリスクがあります.
使い方
1.MainActivityマスターコードと各受信者コード
2.MainActivityのXmlコード
3.リストファイル(主に登録と優先度を観察するための処理)
MainActivity
Costom BroadcaseReceiverのコード(カスタム放送)
ラジオの分類
1.放送を使用してまず一つのクラスを作成してBroadcaseReceiverを継承し、次にOneReceiver方法を書き換える(放送を受信する操作はここで実行する)
2.依頼されたActivityなどの寄生体の中で放送を送る(順序付けと無秩序、以下で紹介する)
3.Androidの4つのコンポーネントの一つとして、必要不可欠な自然は動的登録と静的なグループ測定(リスト登録)があります.
追加:Android 7.0に対して、動的登録を実現するのが一番いいです.
4.私はモニターシステムの放送を持っていません.電源を入れるとか、ネットの状態などではなく、自分で定義した放送を便利にするために、興味がある人は一挙にラジオ処理を行うことができます.
5.ラジオ登録が必要です.ログアウトも必要です.いいえ、OOMの隠れたリスクがあります.
使い方
1.MainActivityマスターコードと各受信者コード
2.MainActivityのXmlコード
3.リストファイル(主に登録と優先度を観察するための処理)
MainActivity
package com.example.broadcasereceiver;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private TextView mCostomBroad;
private TextView mLocalBroad;
private LocalBroadcastManager localBroadcast;
private TextView mDynamicBroad;
private TextView mStaticBroad;
private TextView mQuickBroad;
private IntentFilter dynamic_filter;
private DynamicBroadcaseReceiver dynamicBroadcaseReceiver;
private LocalBroadcaseReceiver localBroadcaseReceiver;
private TextView mQucly;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//
init();
//
initEvent();
/** , , , */
//
localBroadcast = LocalBroadcastManager.getInstance(this);
// -
IntentFilter localFilter = new IntentFilter();
localFilter.addAction("LocalBroadcaseReceiver");
localBroadcaseReceiver = new LocalBroadcaseReceiver();
localBroadcast.registerReceiver(localBroadcaseReceiver, localFilter);
}
/**
* find-Id
* */
private void init() {
mLocalBroad = (TextView) findViewById(R.id.Broadcase_local);
mCostomBroad = (TextView) findViewById(R.id.Broadcase_costom);
mDynamicBroad = (TextView) findViewById(R.id.Broadcase_dynamic);
mStaticBroad = (TextView) findViewById(R.id.Broadcase_static);
mQuickBroad = (TextView) findViewById(R.id.Broadcase_quick);
}
/**
* Click
* */
private void initEvent() {
mCostomBroad.setOnClickListener(this);
mLocalBroad.setOnClickListener(this);
mDynamicBroad.setOnClickListener(this);
mStaticBroad.setOnClickListener(this);
mQuickBroad.setOnClickListener(this);
}
/**
* Event
* */
@Override
public void onClick(View v) {
switch (v.getId()) {
//
case R.id.Broadcase_costom:
Intent intent = new Intent("CostomBroadcaseReceiver");
sendBroadcast(intent);
break;
//
case R.id.Broadcase_dynamic:
// ,
dynamic_filter = new IntentFilter();
// =
dynamic_filter.addAction("DynamicBroadcaseReceiver");
// -
dynamicBroadcaseReceiver = new DynamicBroadcaseReceiver();
registerReceiver(dynamicBroadcaseReceiver, dynamic_filter);
//Toast.makeText(MainActivity.this, " ", 0).show();
Intent DynamicIntent = new Intent("DynamicBroadcaseReceiver");
sendBroadcast(DynamicIntent);
break;
// -
case R.id.Broadcase_static:
Intent Staticintent = new Intent("StaticBroadcaseReceiver");
// ,
//sendBroadcast(Staticintent);
// ( : ), , ,
sendOrderedBroadcast(Staticintent, null);
break;
//
case R.id.Broadcase_local:
Intent localIntent = new Intent("LocalBroadcaseReceiver");
localBroadcast.sendBroadcast(localIntent);
break;
// - , - priority
// , Toask, Toask
case R.id.Broadcase_quick:
Intent quickintent = new Intent("StaticBroadcaseReceiver");
quickintent.setAction("QuickBroadcaseReceiver");
sendOrderedBroadcast(quickintent, null);
break;
default:
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
// - , , APP , , ,
//so if dynamicbRoadcaseReceiver ,we need shell all;(- -!)
unregisterReceiver(dynamicBroadcaseReceiver);
unregisterReceiver(localBroadcaseReceiver);
}
}
以下のRecevice方法はすべて簡単なToaskです.Costom BroadcaseReceiverのコード(カスタム放送)
package com.example.broadcasereceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
/**
*
* */
public class CostomBroadcaseReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent arg1) {
Toast.makeText(context, " ", 0).show();
}
}
DynamicBroadcaseReceiverのコード(動的登録)package com.example.broadcasereceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
/**
*
* */
public class DynamicBroadcaseReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent arg1) {
Toast.makeText(context, " ", 0).show();
}
}
Local BroadcaseReceiverのコード(ローカル放送)package com.example.broadcasereceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
/**
*
* */
public class LocalBroadcaseReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent arg1) {
Toast.makeText(context, " ", 0).show();
}
}
Static BroadcaseReceiverのコード(静的登録)package com.example.broadcasereceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
/**
*
* */
public class StaticBroadcaseReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent arg1) {
Toast.makeText(context, " ", 0).show();
}
}
QuickBroadcaseReceiver(優先度の放送受信者は、ブロック処理を行い、静的放送がブロックされ、優先度の放送しか受信できない)のコードpackage com.example.broadcasereceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class QuickBroadcaseReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, " ", 0).show();
abortBroadcast();
}
}
activity.xml<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:id="@+id/Broadcase_costom"
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center"
android:text="@string/CostomBroadcase" />
<TextView
android:id="@+id/Broadcase_dynamic"
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center"
android:text="@string/DynamicBroadcase" />
<TextView
android:id="@+id/Broadcase_static"
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center"
android:text="@string/StaticBroadcase" />
<TextView
android:id="@+id/Broadcase_local"
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center"
android:text="@string/LocalBroadcase" />
<TextView
android:id="@+id/Broadcase_quick"
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center"
android:text=" " />
LinearLayout>
明細書ファイル
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcasereceiver"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.broadcasereceiver.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
intent-filter>
activity>
<receiver android:name="com.example.broadcasereceiver.CostomBroadcaseReceiver" >
<intent-filter>
<action android:name="CostomBroadcaseReceiver"/>
intent-filter>
receiver>
<receiver android:name="com.example.broadcasereceiver.StaticBroadcaseReceiver"
>
<intent-filter >
<action android:name="StaticBroadcaseReceiver"/>
intent-filter>
receiver>
<receiver android:name="com.example.broadcasereceiver.QuickBroadcaseReceiver"
>
<intent-filter android:priority="100" >
<action android:name="QuickBroadcaseReceiver"/>
<action android:name="StaticBroadcaseReceiver"/>
intent-filter>
receiver>
application>
manifest>