最近在学习Android,因为学校的书本是13年的(无力吐槽),那时Android才4.3太老了,现在都Android9了,有些技术已经改变了,不再适用于现在的Android版本,在写书上的示例代码时遇到了这个问题,通过网上的搜索和在官网看到的介绍解决了这个问题。具体是因为Android8在静态广播的使用上进行了限制。

具体可查看:https://developer.android.google.cn/about/versions/oreo/background


解决办法:

1、使用动态广播代替静态广播,例如:

IntentFilter filter=new IntentFilter("music");Broadcast mBroadcast =new Broadcast();registerReceiver(mBroadcast,filter);

2、依旧使用静态广播,但是在发送广播的Intent里添加Component参数,例如:

intent2=new Intent();intent2.setAction("music");intent2.putExtra("key","方法2");intent2.setComponent(new ComponentName("com.example.dream.ex5_3","com.example.dream.ex5_3.Broadcast"));sendBroadcast(intent2);

ComponentName的第一个参数是广播接收器的包名,第二个参数是广播接收器的路径(也就是包名加广播接收器的类名)。

更多相关文章

  1. Android的startActivityForResult()与onActivityResult()与setRe
  2. android: 静态XML和动态加载XML混合使用,以及重写Layout控件
  3. android (java) 网络发送get/post请求参数设置
  4. Android 开发 调用图库选择图片实现和参数详解
  5. Android应用程序注冊广播接收器(registerReceiver)的过程分析
  6. android平台下基于cmake编译链接ffmpeg-3.4.x的32位和64位(armea
  7. Android USB HID设备通信controlTransfer 接口参数
  8. Android中动态初始化布局参数以及ConstraintLayout使用中遇到的

随机推荐

  1. Android中的Banner效果
  2. Android笔记(28)MVVM架构过程
  3. 用SlidingDrawer实现类似android状态栏下
  4. android
  5. 2013.07.08——— android MediaRecorder
  6. Android(安卓)图书总汇
  7. 写了个android简洁的日志打印工具类。
  8. android应用不生成桌面应用图标
  9. android解压zip包
  10. Android: QQ UI 设计