最近在用安卓广播的时候,按照流程进行操作,可是不管怎样都没有出现我接受的广播,网上查阅资料以后,发现在Android8中,如果是静态注册广播,需要在action中保留原来的静态广播,加入Component参数;代码如下:

AndroidMainifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.administrator.myapplication">
<!-- 权限 -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<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=".BroadcastActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"></action>
<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.administrator.myapplication.MyBroadcastReceiver"/>
</intent-filter>
</receiver>
</application>
</manifest>

  

MyBroadcastReceiver.java:
package com.example.administrator.myapplication;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast; public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "收到广播了", Toast.LENGTH_LONG).show();
}
}

  

BroadcastActivity.java:
package com.example.administrator.myapplication;

import android.content.ComponentName;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button; public class BroadcastActivity extends AppCompatActivity {
private MyBroadcastReceiver myBroadcastReceiver; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_broadcast); //注册广播
myBroadcastReceiver = new MyBroadcastReceiver();
IntentFilter itFilter = new IntentFilter();
Button btn = (Button) findViewById(R.id.sendBtn);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent("com.example.administrator.myapplication.MyBroadcastReceiver");
//第一个参数为包的路径,第二个参数为类名
intent.setComponent(new ComponentName("com.example.administrator.myapplication", "com.example.administrator.myapplication.MyBroadcastReceiver"));
sendBroadcast(intent);
}
});
} //注销的时候取消广播
public void onDestroy() {
super.onDestroy();
unregisterReceiver(myBroadcastReceiver);
}
}

 

Android8 自定义广播接收不到的问题的更多相关文章

  1. Android8自定义广播无法收到消息

    在Android 8之前,如果要发送自定义的静态广播,做好其他配置后,只需实例化一个Intent对象intent,然后将其作为参数传入sendBroadcast()方法中即可,例如 Intent in ...

  2. Android8.0自定义广播接收不到问题

    https://blog.csdn.net/kongqwesd12/article/details/78998151

  3. android 发送自定义广播以及接收自定义广播

    发送自定义广播程序: 布局文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout x ...

  4. android中的广播接收实现总结

    1 首先根据广播应用内接收和应用外接收,分两个类进行管理[1]  LocalBroadcastManager,应用内广播管理类[2]  BroadcastManager  广播管理类(部分应用内,应用 ...

  5. Android(java)学习笔记178:BroadcastReceiver之 自定义广播

    广播使用:               电台:对外发送信号.---------电台发送广播(可以自定义)               收音机:接收电台的信号.-----广播接收者 这里,我们就说明自定 ...

  6. 5.AutoCompleteTextView、自定义广播

    新建信息 布局:自动出来的是系统的组件,里面是listview,写ontextchanglis也行 <LinearLayout xmlns:android="http://schema ...

  7. [android] 自定义广播事件

    上一节的短信拦截在4.0以上系统中无效,可以使用这种办法实现,定义一个activity,清单文件中指定主题为透明,在onCreate()方法里面直接调用finsh()方法,关掉,这样可以就可以实现了 ...

  8. android: 发送自定义广播

    5.3.1    发送标准广播 在发送广播之前,我们还是需要先定义一个广播接收器来准备接收此广播才行,不然发 出去也是白发.因此新建一个 MyBroadcastReceiver 继承自 Broadca ...

  9. 自定义广播(BroadcastReceiver)事件 --Android开发

    本例演示自定义广播事件.我们需要做的是,在主活动中写发送广播的代码,然后在接收广播的类中写接收广播的代码. 1.主活动中点击按钮后发送广播 MainActivity.java: public clas ...

随机推荐

  1. jquery中遍历

    1.jQuery--Dom遍历 1)jquery遍历---祖先元素 parents() 方法返回被选元素的所有祖先元素,它一路向上直到文档的根元素 (<html>).也可以使用可选参数来过 ...

  2. 超详细 Spring @RequestMapping 注解使用技巧

    @RequestMapping 是 Spring Web 应用程序中最常被用到的注解之一.这个注解会将 HTTP 请求映射到 MVC 和 REST 控制器的处理方法上. 在这篇文章中,你将会看到 @R ...

  3. DeepCas:an end-to-end predictorof information Cascades

  4. react-native Execution failed for task ':app:prepareRnReduxReactNativeUpdateUnspecifiedLibrary'报错

    详细报错 Could not copy zip entry E:\项目目录\node_modules\react-native-update\android\build\outputs\aar\rea ...

  5. Windows远程桌面(mstsc)不能复制粘贴的解决办法

    原来通过mstsc远程连接Windows能够直接在本地和服务器之间复制.粘贴一些文字和文件,最近突然无法直接复制粘贴了,只能通过远程映射的本地磁盘方式交换文件,太为繁琐. 已经确认远程桌面连接选项“本 ...

  6. CSS魔法堂:一起玩透伪元素和Content属性

    前言  继上篇<CSS魔法堂:稍稍深入伪类选择器>记录完伪类后,我自然而然要向伪元素伸出"魔掌"的啦^_^.本文讲讲述伪元素以及功能强大的Contet属性,让我们可以通 ...

  7. x264阅读记录-1

    x264阅读记录-1 采用x264版本是x264-snapshot-20060316-2245. 1. main函数 x264的main函数位于x264.c中,下面是main函数调用情况: (1)_s ...

  8. Css3实现波浪效果2

    一.不规则圆,旋转实现波浪效果 .info { width: 200px; height: 200px; ; background: #009A61; border-radius: 45%; colo ...

  9. MySQL监控全部执行过的sql语句

    MySQL监控全部执行过的sql语句 查看是否开启日志记录show variables like “general_log%” ; +——————+———-+|Variable_name|Value| ...

  10. WSL(Windows Subsystem for Linux)的安装与使用及 mongodb安装

    有关WSL的介绍这里就不做解释了.另外,本文仅适用于win10 build 16215以及之后的版本,之前的版本可参考官方链接. (可使用“winver”命令查看windows版本号) 安装:1.  ...