您的位置:首页 > 其它

静态注册无法监听隐式广播

2019-03-19 20:48 1526 查看

小白进化篇
(果然省电是 Android O 的第一任务。)

Toast(…)消息弹不出来

切入正题,android O,也就是android 8.0到底做了些什么,为什么注册静态无法监听隐式广播?
不急,听我慢慢道来,可能会有意外收获哦
先整理一下如何实现一个广播接收器的功能 夹带说明为何接收不到广播以及3个 解决方法。
静态注册广播:
首先在MainActivity.java中发送广播,主要代码如下:

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent=new Intent();  //建立一个intent
intent.setAction("com.android.bin"); //action用法,这里随便写的,最好包名开头
sendBroadcast(intent);
}

也可直接 Intent intent=new Intent(“com.android.bin”);
然后建立一个广播接收器
new---->>other—>Broadcast Receiver
重写onReceive方法,比如:

public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"接收到了这条信息",Toast.LENGTH_LONG).show();
}

所谓的静态注册,我们需要在AndroidManifest.xml声明

<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true"> //这个可以百度去,它的默认值还是有点意思的
<intent-filter>
<action android:name="com.android.bin"/> //和上面setAction一样即可
</intent-filter>
</receiver>

添加一个知识点,不想看就跳过(借用)

静态注册到此为止结束,好吧,作为Android O(8.0) 以上开发者,广播无效 (忍不住吐槽,这个对小白真是个坑,为了提高电池续航时间,google辛苦了,还有那个Notification 的用法,8.0以上引入了channel,不注意也会坑小白)
官网早已说明,小白还是要看的 https://developer.android.com/distribute/best-practices/develop/target-sdk.html

既然限制了静态注册接收隐式广播,那么解决方法至少三两个(也看到了有强迫症的大佬提供了静态注册的解决方法,没有亲测,也感觉没有意义!这里不做说明)
1、gradle 中的targetSdkVersion 设置小于26(看上一篇blog提到了兼容性)
2、使用动态广播registerReceiver注册形式,这个不受限制(推荐)
3、使用JobScheduler。https://developer.android.com/reference/android/app/job/JobScheduler.html

对于显式广播,则依然可以通过清单注册(静态注册)的方式监听
动态注册的优先级是要高于静态注册优先级的,因此在必要的情况下,我们是需要动态注册广播接收器的

那么如何注册一个动态广播呢?

放一个按钮吧,放不放随你,为了演示清楚
布局文件:

<?xml version="1.0" encoding="utf-8"?<
3ff7
/span>>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="动态注册发送广播"
android:id="@+id/button"
</RelativeLayout>

动态注册代码,为了好看懂,没搞些花里胡哨的,只需在MainActivty.java里面写就可以了

package com.android.myapplication7;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private static final String ACTION = "com.android,bin";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
IntentFilter filter = new IntentFilter();
filter.addAction(ACTION);
filter.setPriority(Integer.MAX_VALUE);  //设置优先级,这里最大,不设也行
registerReceiver(MyReceiver, filter);
Intent intent = new Intent();
intent.setAction(ACTION);
sendBroadcast(intent);
}
});
}
private BroadcastReceiver MyReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, "接收到了来自斌哥的消息", Toast.LENGTH_SHORT).show();
}
};
}

点击按钮后会出现提示信息:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: