【问题标题】:Broadcast receiver for nought and Oreo + devices not workingnaught 和 Oreo + 设备的广播接收器无法正常工作
【发布时间】:2019-02-21 04:20:38
【问题描述】:
public class ScreenReceiver extends BroadcastReceiver {

    private boolean screenOff;

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF))    {
            screenOff = true;
        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            screenOff = false;
        }

    }


   <receiver
             android:name=".ScreenReceiver"
             android:enabled="true"
             android:exported="true">
             <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />
                 <action android:name="android.intent.action.DREAMING_STARTED" />
                <action android:name="android.intent.action.DREAMING_STOPPED" />
                 <action android:name="android.intent.action.CLOSE_SYSTEM_DIALOGS" />
                 <action android:name="android.intent.action.SCREEN_ON" />
                 <action android:name="android.intent.action.SCREEN_OFF" />
                <action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />
                 <action android:name="android." />
             </intent-filter>
        </receiver>

在 naught 和 oreo 设备上没有收到任何回调,在棉花糖设备上试过它工作正常。但在 oreo 设备上它不工作,并且电池连接和网络更改接收器不工作。

【问题讨论】:

  • 从牛轧糖开始,做了后台优化。面向 Android 8.0 或更高版本的应用无法再在其清单中为隐式广播注册广播接收器。
  • 我知道行为变化,但我需要它来获取所有操作回调。有没有办法做到这一点。
  • 在搜索后我发现这是正确的方法github.com/devggaurav/…

标签: java android broadcastreceiver android-8.0-oreo android-7.0-nougat


【解决方案1】:

您不能从 Oreo 在 manifest.xml 中注册广播接收器。 可以看 Android 8.0 Behavior Changes

应用不能使用它们的清单来注册大多数隐式 广播(即不是专门针对 应用程序)。

解决方案

改为在您的相关活动中注册您的接收器。像这样。

public class MainActivity extends AppCompatActivity {
    BroadcastReceiver receiver;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        filter.addAction(Intent.ACTION_SCREEN_ON);
        filter.addAction("android.intent.action.LOCKED_BOOT_COMPLETED");
        receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                // todo
            }
        };
        registerReceiver(receiver, filter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (receiver != null)
            unregisterReceiver(receiver);
    }
}

如果您没有找到相关的常量字符串,您可以将操作添加为与清单相同的字符串。

【讨论】:

  • 我知道,但还有什么办法??
  • 这将是特定于活动的?
  • 你想在哪里注册?您可以在任何上下文可用的地方注册BroadcastReceiver,例如服务、片段和对话框等。
  • github.com/devggaurav/…。这就是有人做到这一点的方式。
  • 优秀的解决方案!,您应该在与此相关的其他问题上发布此解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-22
相关资源
最近更新 更多