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发送广播需要加这句

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

    评论

报告相同问题?

悬赏问题

  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败