自定义广播没有作用

我脱下短袖 2017-11-22 10:29:59
定义广播接收类
public class MyBroadcastReceiver extends BroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent) {
Log.i("MyBroadcastReceiver", "onReceive: ");
Toast.makeText(context,"this is myReceiver",Toast.LENGTH_SHORT).show();
}
}


注册自定义广播

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.zoukeqing.helloworld">

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

<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">
<intent-filter>
<action android:name="com.example.zoukeqing.helloworld.MY_BROADCAST"/>
</intent-filter>
</receiver>
</application>

</manifest>


点击按钮发送广播
public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {

Log.d("MainActivity", "onCreate: ");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button btn = (Button)findViewById(R.id.btn_broadcast);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sendBroadcast(new Intent("com.example.zoukeqing.helloworld.MY_BROADCAST"));
}
});

}
}
...全文
455 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
键盘舞者113 2018-03-19
  • 打赏
  • 举报
回复
如果你的手机Android7.0的,而且接收者和发送者的应用不是一个,那就要加自定义权限 http://blog.csdn.net/u011043551/article/details/68962708
x443137254 2018-03-19
  • 打赏
  • 举报
回复
你是不是用小米的手机各种奇怪的问题遇到小米,一点不奇怪
  • 打赏
  • 举报
回复
android:name=".MyBroadcastReceiver" android:enabled="true" android:exported="true"
我脱下短袖 2017-11-22
  • 打赏
  • 举报
回复
请问这是怎么解决(初次使用csdn,发布问题没讲清楚)

80,349

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧