The problem is that the fragment does not catch broadcast. In the fragment, so I register it. But in onResume I get null. Having rustled the Internet, I did not find anything intelligible. But if you put it in a separate file, and register in the manifest, then everything is fine to catch. I'm testing on API 18. What could be the problem?

private BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Log.d("logba", "onReceive : "); if (intent.getAction().equals(KeyChain.ACTION_STORAGE_CHANGED)) { Log.d("logba", "onReceive : keychain"); } } }; @Override public void onResume() { super.onResume(); Log.d("logba", "onResume : register" + getContext().getApplicationContext().registerReceiver(receiver, new IntentFilter(KeyChain.ACTION_STORAGE_CHANGED))); } @Override public void onPause() { Log.d("logba", "onPause : unregister"); getContext().getApplicationContext().unregisterReceiver(receiver); super.onPause(); } 
  • в onResume получаю null - decrypt please. - Vladyslav Matviienko
  • getContext (). getApplicationContext (). registerReceiver (receiver, new IntentFilter (KeyChain.ACTION_STORAGE_CHANGED))); - this code returns null; - AndXor
  • This is normal. Read what should return this method to the office. documentation. - Vladyslav Matviienko
  • Okay, figs with him with this registerReceiver, although they write in nete that he can return null when there is no filter, for example, if you want to get a battery charge, but the app is on a TV with no battery. I’m more worried about not catching broadkast, and if you make a separate file and register it in the manifest, everything works like a clock. - AndXor

3 answers 3

You can not receive messages in BroadCast only if your fragment will be able to onPause () and this is the norm and the correct behavior of BroadCast. If you need BroadCast, which will constantly catch messages, then you need to implement it in a separate file, as you wrote and describe this BroadCast in the manifest and assigning it the necessary filter, to which it will respond, there is no other way.

    try

     getActivity().registerReceiver(receiver, new IntentFilter(KeyChain.ACTION_STORAGE_CHANGED)); 

    well, getActivity().unregisterReceiver(receiver);

    This may be due to the fact that you register the receiver in the context of the entire application, and you generate an event in the context of the activity.

    • What I just did not try, does not help. Posted on b.android.com see what they answer. - AndXor

    I found a problem. When I call this code (see below) KeyChain activity opens and my fragment goes to pause. When the certificate was installed and toast was shown about the successful installation of the certificate, my fragment is still in pause. Therefore, he skips this broadcast and catches it perfectly in a separate file.

     Intent intent = KeyChain.createInstallIntent(); intent.putExtra(KeyChain.EXTRA_CERTIFICATE, certificate); intent.putExtra(KeyChain.EXTRA_NAME, name); startActivityForResult(intent, INSTALL_CA_CODE);