[Android]App之间发送和接收广播

该文详细介绍了如何在两个Android应用之间实现广播通信。通过创建发送方的BroadOrderActivity和接收方的BroadOrderReceiver,以及在AndroidManifest.xml中设置必要的权限和过滤器,成功发送和接收自定义广播。同时,展示了第二个应用如何注册接收相同的广播。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里使用不同模块来实现app之间的广播通信,以两个app为例,通过点击按钮来实现的~

 

1. broad模块 - 忽略BootCompleteReceiver和MainActivity(这是其他的~)

 步骤

(1)创建 BroadOrderActivity,表示发送广播者

 

 

点击右下角的finish即完成创建

(BroadOrderActivity.java和在layout下的activity_broad_order.xml)

(2) 书写代码

 这部分代码如下:

BroadOrderActivity.java

package com.example.broad;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;

public class BroadOrderActivity extends AppCompatActivity implements View.OnClickListener {

    private BroadOrderReceiver broadOrderReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_broad_order);
        findViewById(R.id.btn_send_a).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        Intent intent = new Intent("com.example.broad.BroadOrderReceiver");
        sendOrderedBroadcast(intent,"com.example.broad.BroadOrderReceiver");
    }

    @Override
    protected void onStart() {
        super.onStart();
        broadOrderReceiver = new BroadOrderReceiver();
        IntentFilter filter = new IntentFilter("com.example.broad.BroadOrderReceiver");
        registerReceiver(broadOrderReceiver,filter);
    }

    @Override
    protected void onStop() {
        super.onStop();
        unregisterReceiver(broadOrderReceiver);
    }
}

activity_broad_order.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btn_send_a"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="发送广播"/>

</LinearLayout>

(3)在同一个模块下创建一个接收者 BroadOrderReceiver

 

同样点击右下角的finish完成创建 

(4)接收者代码如下(在控制台打印消息表示接收到~):

BroadOrderReceiver.java

package com.example.broad;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class BroadOrderReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("nn","BroadOrderAReceiver接收到广播");
    }
}

(5)重要的部分!我自己在这里纠结了好久,终于发现是这里的问题,才不能完成app之间通信

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

    <uses-permission android:name="com.example.broad.BroadOrderReceiver"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <permission
        android:name="com.example.broad.BroadOrderReceiver"
        android:protectionLevel="signature"
        ></permission>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.RecycleView">
        <receiver
            android:name=".BroadOrderReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.example.broad.BroadOrderReceiver"/>
            </intent-filter>
        </receiver>

        <activity
            android:name=".MainActivity"
            android:exported="false" />

 
        </receiver>

        <activity
            android:name=".BroadOrderActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

小小提一下,主要是这里要设置用户的权限(完整代码在上面,这里就是记录一下下):

<uses-permission android:name="com.example.broad.BroadOrderReceiver"/>

 <permission
        android:name="com.example.broad.BroadOrderReceiver"
        android:protectionLevel="signature"
        ></permission>

2. broadb模块 - 在我的理解中,就是另一个app

(1)创建模块

 

 点击finish就完成模块的创建

(会自动生成MainActivity.java 和 activity_main.xml,只修改了MainActivity.java)

代码如下:

MainActivity.java

package com.example.broadb;

import androidx.appcompat.app.AppCompatActivity;

import android.content.IntentFilter;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    private BroadcastBReceiver broadcastBReceiver;

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

    @Override
    protected void onStart() {
        super.onStart();
        broadcastBReceiver = new BroadcastBReceiver();
        IntentFilter intentFilter = new IntentFilter("com.example.broad.BroadOrderReceiver");
        registerReceiver(broadcastBReceiver,intentFilter);
    }

    @Override
    protected void onStop() {
        super.onStop();
        unregisterReceiver(broadcastBReceiver);
    }
}

 (2)创建 BroadcastBReceiver

创建上一个模块一样~

这里就直接附上代码

BroadcastBReceiver.java

package com.example.broadb;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class BroadcastBReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("nn","BroadcastBReceiver收到广播");
    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-permission android:name="com.example.broad.BroadOrderReceiver"/>
    <permission android:name="com.example.broad.BroadOrderReceiver"></permission>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.RecycleView">
        <receiver
            android:name=".BroadcastBReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter android:priority="100">
                <action android:name="com.example.broad.BroadOrderReceiver"/>
            </intent-filter>
        </receiver>

        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

3. 结果 - 成功~

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值