Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sending non-protected broadcast 告警信息处理 #50

Open
soapgu opened this issue Jun 2, 2021 · 0 comments
Open

Sending non-protected broadcast 告警信息处理 #50

soapgu opened this issue Jun 2, 2021 · 0 comments
Labels
problem problem or trouble 安卓 安卓

Comments

@soapgu
Copy link
Owner

soapgu commented Jun 2, 2021

  • 现象

在调试Android程序过程中,观察logcat的面板,会发现有红色警告出现
Sending non-protected broadcast com.space365.intent.broadcast.launcher from system 1291:com.space365.smartboard/1000 pkg com.space365.smartboard
不影响正常使用。不过有洁癖的我不开心

  • 解决方案

看来我们又干了让系统不高兴的事情了。non-protected就是不安全的。

  1. 通过权限限制广播
    这个就不展开了,官网有介绍
  1. 无限制广播应用内广播
    简单讲下,因为我的实际使用广播的范围只是跨模块并没有出应用。所以用权限多少有点小题大做的感觉
    通过LocalBroadcastManager来实现内部广播。三个点必须一起改,和原理的广播不兼容
  • 发送
private void sendLaunchBroadcast( String message , LaunchStage stage) {
       LaunchInfo.INSTANCE.setStage(stage);
       Intent intent = new Intent();
       intent.setAction(Broadcasts.launcher);
       intent.putExtra(Broadcasts.Launcher.message , message );
       intent.putExtra( Broadcasts.Launcher.stage , stage.name() );
       LocalBroadcastManager.getInstance(this.context).sendBroadcast( intent );
   }
  • 注册接收
private void setupBroadcast() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(Broadcasts.launcher);
        LocalBroadcastManager.getInstance(this.requireContext()).registerReceiver((broadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                Logger.i("onReceive launcher Broadcast>>>>>>");
            }
        }), filter);
    }
  • 注销
 @Override
    public void onStop() {
        LocalBroadcastManager.getInstance(this.requireContext()).unregisterReceiver(broadcastReceiver);
        super.onStop();
    }
@soapgu soapgu added 安卓 安卓 problem problem or trouble labels Jun 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
problem problem or trouble 安卓 安卓
Projects
None yet
Development

No branches or pull requests

1 participant