xiaozezezeze 2018-09-05 07:34 采纳率: 0%
浏览 4015
已结题

Android8.1.0版本发送标准广播接收不到

在Android studio上开发,使用真机模拟,系统是Android8.1.0,在A程序上设置了一个Button,发送标准广播给B程序,应该会收到两条提示信息(“A收到广播”,”B收到广播”),但是只出现了A收到的信息。是按照Android第一行代码这本书5.3.2这节写的代码,但不知道为何不好使。
A程序代码
package="com.example.administrator.myreceiver">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <receiver
        android:name=".MyBroadcastReceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="com.example.broadcast.MY_BROADCAST"/>
        </intent-filter>
    </receiver>
</application>

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button button=(Button)findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent=new Intent("com.example.broadcasttest.MY_BROADCAST");
            intent.setComponent(new ComponentName("com.example.administrator.myreceiver","com.example.administrator.myreceiver.MyBroadcastReceiver"));//高版本Android发送广播需要加这句
            sendBroadcast(intent);
        }
    });
}

}

public class MyBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    // TODO: This method is called when the BroadcastReceiver is receiving
    // an Intent broadcast.
    //throw new UnsupportedOperationException("Not yet implemented");
    Toast.makeText(context, "received in MyBroadcastReceiver", Toast.LENGTH_SHORT).show();
}

}

B程序代码
public class AnotherBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    // TODO: This method is called when the BroadcastReceiver is receiving
    // an Intent broadcast.
    //throw new UnsupportedOperationException("Not yet implemented");
    Toast.makeText(context, "received in AnotherBroadcastReceiver", Toast.LENGTH_SHORT).show();
}

}

package="com.example.administrator.brooadcasttest_2">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <receiver
        android:name=".AnotherBroadcastReceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="com.example.broadcast.MY_BROADCAST"/>
        </intent-filter>
    </receiver>
</application>

  • 写回答

1条回答

  • qq_26763799 2018-09-05 09:32
    关注

    intent.setComponent(new ComponentName("com.example.administrator.myreceiver","com.example.administrator.myreceiver.MyBroadcastReceiver"));//高版本Android发送广播需要加这句

    这里你只指定了一个广播接受器,另一个是收不到吧...

    评论

报告相同问题?

悬赏问题

  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算