BroadCastreceiver leaked inside Jobservice firebase

182
April 28, 2018, at 4:36 PM

I am trying to start a service from my BroadCastReceiver which is inside JobService when the charger is connected but when I run my code I get IntentReceiverLeaked in LogCat. I am registering my receiver inside onStartJob. and also I unregister it inside OnStopJob but still it is giving me error I have provided my full code please help me. Thanks..

Here is my JobService class

public class JobOreoAlarm  extends JobService
{
    private boolean jobCancel=false;
    @Override
    public boolean onStartJob(JobParameters params)
    {
        if(jobCancel)
        {
            return true;
        }
         AlarmBroadCast=new BroadcastReceiver()
        {
            @Override
            public void onReceive(Context context, Intent intent)
            {
                mSharedPreferencesRing=context.getSharedPreferences("alert", Context.MODE_PRIVATE);
                selection= mSharedPreferencesRing.getInt("alerts",90);

                try
                {
                    levels = intent.getIntExtra("level", 0);
                    status = intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN);
                }
                catch (NullPointerException e)
                {
                    e.printStackTrace();
                }

                if(status==BatteryManager.BATTERY_STATUS_CHARGING)
                {
                    if (levels ==selection)
                    {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
                        {
                            context.startForegroundService(new Intent(context, MyAlarmServiceClass.class));
                        }
                        else
                        {
                            context.startService(new Intent(context,MyAlarmServiceClass.class));
                        }
                    }
                }
            }
        };
        getNotification();
        registerReceiver(AlarmBroadCast, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
        jobFinished(params,false);
        return true;
    }

    @Override
    public boolean onStopJob(JobParameters params)
    {
        if(AlarmBroadCast!=null)
        {
            unregisterReceiver(AlarmBroadCast);
        }
        jobCancel=true;
        return true;
    }
    @Override
    public void onDestroy()
    {
        if(AlarmBroadCast!=null)
        {
            unregisterReceiver(AlarmBroadCast);
        }
        super.onDestroy();
    }
    @Override
    public void onCreate()
    {
        super.onCreate();
        startForeground(1,getNotification());
    }
  }

Here is my code to start JobService

    dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(this));
    myJob = dispatcher.newJobBuilder()
            .setService(JobOreoAlarm.class) // the JobService that will be called
            .setLifetime(Lifetime.FOREVER)
            .setRecurring(true)
            .setTrigger(Trigger.executionWindow(0, 120))
            .setReplaceCurrent(false)
            .setRetryStrategy(RetryStrategy.DEFAULT_EXPONENTIAL)
            .setConstraints(Constraint.DEVICE_CHARGING)
            .setTag("oreo")        // uniquely identifies the job
            .build();
    dispatcher.mustSchedule(myJob);
Rent Charter Buses Company
READ ALSO
AudioTrack setStereoVolume works incorrectly

AudioTrack setStereoVolume works incorrectly

I want to play sound on only one channel - headphones right or left

242
Android studio xml editor does not take focus or click on component in preview

Android studio xml editor does not take focus or click on component in preview

I am facing this bug of android studio since AS30, now after 3 upgrades have released after AS3

162
Navigation Drawer Activity Error

Navigation Drawer Activity Error

I just updated to Android Studio 31

211
Fetching sensor data from Firebase database

Fetching sensor data from Firebase database

How can I fetch data from Firebase database to my Android application? Here is the structure of the database

179