使用中のAndroidのBroadcastReceiver

図1に示すように、Androidの放送は、静的登録と動的登録に分割されています

図2は、以下の単純な静的登録の一例です

  • 連続作成BroadcastReceiverサブカテゴリのを
public class DeviceBootReceiver extends BroadcastReceiver {

    private static final String TAG = DeviceBootReceiver.class.getName();
    
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "开机了:" + intent.getAction());
    }
}

電話を受けるため、このクラスの機能は、ラジオに切り替えています。

  • AndroidManifest.xmlコンポーネントを登録します
<receiver android:name=".DeviceBootReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

actionバリュータグは、ここから放送、放送の種類を一致させるために使用されます。
電話がオンになっているとき、あなたは、システムからのメッセージを受信するように。

同様に、我々はまた、アプリケーションがインストールされているアンインストールアプリケーション、USBプラグと他の放送システムを監視することができます。ただ、actionわずかに異なる値。

<receiver android:name=".OtherStateReceiver">
    <intent-filter>
        <!--电量过低-->
        <action android:name="android.intent.action.BATTERY_LOW" />
        <!--USB连接-->
        <action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
        <!--USB断开-->
        <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
        <!--软件包安装-->
        <action android:name="android.intent.action.PACKAGE_ADDED" />
        <!--软件包卸装-->
        <action android:name="android.intent.action.PACKAGE_REMOVED" />
    </intent-filter>
</receiver>

上記の放送ブーツに加えて、他の放送局などの登録が正常であることを提供するAPIレベル<26(Android8.0以下)
Googleは、受信者プログラムパーマネントメモリリソースの消費を防ぐために、いくつかのを禁止しますManifest declared receiver私たちは、あなたが動的登録を使用する必要があります聞くために続けていきたいです。

図3に示すように、以下は動的登録の一例です。

public class SystemBroadcastReceiverActivity extends AppCompatActivity {

    private SystemBroadcastReceiver receiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_system_broadcast_receiver);

        IntentFilter filter = new IntentFilter();
        //USB连接
        filter.addAction(Intent.ACTION_POWER_CONNECTED);
        //USB断开
        filter.addAction(Intent.ACTION_POWER_DISCONNECTED);
        receiver = new SystemBroadcastReceiver();
        registerReceiver(receiver, filter);
    }

    private class SystemBroadcastReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            System.out.println(intent.getAction());
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(receiver);
    }
}

その後、AndroidManifest.xml登録Activity缶を。

4、カスタム放送

放送受信システムされるの前に、我々はまた、同じアプリケーションまたは異なるアプリケーションにカスタマイズされた放送および受信を送信することができます。

ここではカスタム放送の静的登録は以下のとおりです。
  • 設立はActivityButton放送のクリックを送ります
public class CustomSenderActivity extends AppCompatActivity {

    public static final String BROADCAST_ACTION = "com.hncj.android.manifest.CustomSenderActivity";
    public static final String BROADCAST_CONTENT = "broadcast_content";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_sender);
    }

    @SuppressLint("WrongConstant")
    public void sendBroadcast(View view) {
        Intent intent = new Intent();
        intent.setAction(BROADCAST_ACTION);
        intent.putExtra(BROADCAST_CONTENT, "我发送了广播,你看收没收到?");
        
        //解决API26及以上的Android静态注册的Receiver收不到广播
        //第一种解决方案:设置标志 Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND
        intent.addFlags(0x01000000); //不论是一个应用自己发自己收还是一个应用发一个应用收,都可以实现效果

        //第二种解决方案:指定包名(这里是一个应用,自己发,自己收,都在一个包下)
        //intent.setPackage(getPackageName());  //如果是给另一个应用发广播 则写成intent.setPackage("另一个项目应用接收者的包名");

        //第三种解决方案: 明确指定包名,类名
        //intent.setComponent(new ComponentName(this, CustomReceiver.class)); //如果是给另一个应用发广播 则写成 intent.setComponent(new ComponentName("另一个应用接收者包名", "另一个应用接收者类名"));

        //发送广播
        sendBroadcast(intent);
    }
}
  • レイアウトファイル
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="40dp">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="sendBroadcast"
        android:background="#008080"
        android:textSize="30dp"
        android:text="发送广播"></Button>
</LinearLayout>
  • 受信者
public class CustomReceiver extends BroadcastReceiver {

    private static final String TAG = CustomReceiver.class.getName();

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        Log.d(TAG, "我收到了来自" + action + "的广播");
    }
}
  • AndroidManifest.xmlファイル
<activity android:name=".manifest.CustomSenderActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

<receiver android:name=".manifest.CustomReceiver">
    <intent-filter>
        <action android:name="com.hncj.android.manifest.CustomSenderActivity" />
    </intent-filter>
</receiver>

ここでは、現在のプロジェクトの受信者だけでなく、別のプロジェクトでは、受信者があり、コードは基本的に同じです。
関連API:

整然としたブロードキャストを送信します。

<!--广播中携带数据-->
Bundle bundle = new Bundle();
bundle.putString("username", "nacy");
sendOrderedBroadcast(intent, null, null, null, Activity.RESULT_OK, null, bundle);

(--1000-1000)を受信するためにどの受信者定義する、優先順位の高い、より早く受信した放送:優先順位の受信者を設定します

<receiver android:name=".manifest.CustomReceiver">
    <!--priority可以设置优先级-->
    <intent-filter android:priority="9">
        <action android:name="com.hncj.android.manifest.CustomSenderActivity" />
    </intent-filter>
</receiver>

放送の終了:

<!--广播不再向下传递-->
abortBroadcast();

データ放送を削除します。

//取出Bundle对象,类似于一个map
 Bundle bundle = getResultExtras(true);

許可を受けた設定し、受信者:()

  • まず、送信者AndroidManifest.xml権限(パッケージ名+許可名)を宣言するために
<permission android:name="com.hncj.android.RECEIVER_PERMISSION" />
  • パーミッションの定義受信者が送信するための時間を持っている必要があります。
sendOrderedBroadcast(intent, Manifest.permission.RECEIVER_PERMISSION, null, null, Activity.RESULT_OK, null, bundle);
  • 必要なのでは受信者AndroidManifest.xml権限:
<uses-permission android:name="com.hncj.android.RECEIVER_PERMISSION" />

送信者権限を設定:(誰が私を与えることができます)

  • 最初の受信者にAndroidManifest.xml権限を宣言する
<permission android:name="com.hncj.android.SENDER_PERMISSION" />
  • 説明権利送信側は受信側のタブを持っています:
<receiver android:name=".manifest.CustomReceiver" android:permission="com.hncj.android.SENDER_PERMISSION">
    <intent-filter android:priority="9">
        <action android:name="com.hncj.android.manifest.CustomSenderActivity" />
    </intent-filter>
</receiver> 
  • 送信者のAndroidManifest.xml使用権
<uses-permission android:name="com.hncj.android.SENDER_PERMISSION" />

動的カスタム放送、唯一のアクションでカスタムコンテンツでは、上記の動的登録の例があり登録してください。

おすすめ

転載: www.cnblogs.com/wotoufahaiduo/p/11639140.html