最近在用安卓广播的时候,按照流程进行操作,可是不管怎样都没有出现我接受的广播,网上查阅资料以后,发现在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. angular学习笔记(1)- 四大核心特性

    angular1学习笔记(1) -  angular1四大核心特性 1.MVC model:数据模型层 controller:业务逻辑和控制逻辑 view:视图层,负责展示 2.模块化 Module ...

  2. hdu6026 Deleting Edges(Dijkstra+思路)

    https://vjudge.net/problem/HDU-6026 我一直想不明白的是,它的乘法是如何保证n-1条边的.后来画了一张图大概能明白了. 结合最后的乘法二层循环的代码来看,当i=4的时 ...

  3. Linux curl命令使用代理、以及代理种类介绍(转)

    Linux curl命令使用代理.以及代理种类介绍   本文转自http://aiezu.com/article/linux_curl_proxy_http_socks.html,感谢作者 有时出于个 ...

  4. delphi开源JWT

    delphi开源JWT 开源GIT地址:https://github.com/paolo-rossi/delphi-jose-jwt JSON Web Token (JWT)是一个开放标准(RFC 7 ...

  5. SpringBoot+SpringDataJPA项目中使用EntityManager执行自定义复杂SQL的方法

    import javax.annotation.Resource; import javax.persistence.EntityManager; @Resource private EntityMa ...

  6. Guava-Objects使用

    前言 Java中的Object提供了很多方法供所有的类使用,特别是toString.hashCode.equals.getClass等方法,在日常开发中作用很大,Guava中包含Objects类,其提 ...

  7. localstorage 更新监测 storage事件

    1.存储更新监测 存储状态监测的原理是storage事件.storage事件说明: https://developer.mozilla.org/zh-CN/docs/Web/API/StorageEv ...

  8. 一个简单的开源PHP爬虫框架『Phpfetcher』

    这篇文章首发在吹水小镇:http://blog.reetsee.com/archives/366 要在手机或者电脑看到更好的图片或代码欢迎到博文原地址.也欢迎到博文原地址批评指正. 转载请注明: 吹水 ...

  9. linux随机数生成

    随机数多应用在密码的随机生成 #随机数生成 $RANDOM (1-32767) 11.内部系统变量($RANDOM) 1-32767 22. awk 'BEGIN{srand();print rand ...

  10. phpexcel 导入超过26列、处理时间格式

    见地址:http://www.thinkphp.cn/topic/33376.html excel处理时间: https://blog.csdn.net/xqd890608/article/detai ...