BroadcastReceiver does not work accurately unless manually registered

Simply put; BroadcastReceiver Works if defined in Manifest, but works with short Delay and does not work Always , if registered manually .

Here BroadcastReceiveris the one I created to capture the event when the date changes (day passed):

public class BootBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        Toast.makeText(context, intent.getAction(), Toast.LENGTH_LONG).show();

        switch (intent.getAction()){
            case Intent.ACTION_TIME_TICK:
            case Intent.ACTION_TIME_CHANGED:
            case Intent.ACTION_TIMEZONE_CHANGED:
            case Intent.ACTION_DATE_CHANGED:
            case Intent.ACTION_BOOT_COMPLETED:
                Log.d("BroadcastReceiver", intent.getAction().toString());
        }
    }
}

It is registered in the Manifesto here , it works, but has a slight delay and also does not always work:

<receiver android:name=".Receivers.BootBroadcastReceiver">
      <intent-filter>
           <action android:name="android.intent.action.TIME_TICK" />
           <action android:name="android.intent.action.TIME_SET" />
           <action android:name="android.intent.action.TIMEZONE_CHANGED" />
           <action android:name="android.intent.action.DATE_CHANGED"/>
           <action android:name="android.intent.action.BOOT_COMPLETED" />
      </intent-filter>
</receiver>

But when manually registering through my background service, it works fine:

public class ApplicationService extends Service{

    ...

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Intent.ACTION_DATE_CHANGED);
        intentFilter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
        intentFilter.addAction(Intent.ACTION_TIME_CHANGED);
        intentFilter.addAction(Intent.ACTION_TIME_TICK);
        registerReceiver(new BootBroadcastReceiver(), intentFilter);

        return START_STICKY;
    }
}

, , BroadcastReceiver , - strong > Service.

.

+4
3

:

<receiver android:name=".Receivers.BootBroadcastReceiver">
  <intent-filter
        android:enabled="true"
        android:exported="true">
       <action android:name="android.intent.action.TIME_TICK" />
       <action android:name="android.intent.action.TIME_SET" />
       <action android:name="android.intent.action.TIMEZONE_CHANGED" />
       <action android:name="android.intent.action.DATE_CHANGED"/>
       <action android:name="android.intent.action.BOOT_COMPLETED" />
  </intent-filter>

+1

Define a BootBroadcastReceiver anywhere in an Activity / Fragment as follows:

mBootBroadcastReceiver = new BootBroadcastReceiver() {
 @Override
 public void onReceive(Context context, Intent intent) {
     Log.d(TAG," BootBroadcastReceiver"); //do something with intent
   }
 };

mIntentFilter=new IntentFilter("action_name");

Now register BootBroadcastReceiver in onResume () and unregister in onPause ()

       @Override
        protected void onResume() {
            super.onResume();
            registerReceiver(mBootBroadcastReceiver, mIntentFilter);
        }




        @Override
        protected void onPause() {
            if(mReceiver != null) {
                unregisterReceiver(mBootBroadcastReceiver);
                mBootBroadcastReceiver = null;
            }
            super.onPause();
        }

Add permission to your manifest.

 <uses-permission android:name="android.permission.READ_PHONE_STATE" />
  <uses-permission android:name="ANDROID.PERMISSION.RECEIVE_SMS" />
  <uses-permission android:name="android.permission.READ_SMS" />
0
source

Source: https://habr.com/ru/post/1676219/


All Articles