mendhak / gpslogger

:satellite: Lightweight GPS Logging Application For Android.
https://gpslogger.app
Other
1.99k stars 607 forks source link

why Intent serviceIntent = new Intent(AppSettings.getInstance(), GpsLoggingService.class); #1118

Closed nienienienie closed 9 months ago

nienienienie commented 9 months ago

in

public class AlarmReceiver extends BroadcastReceiver {

    private static final Logger LOG = Logs.of(AlarmReceiver.class);

    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            LOG.debug("Alarm received");

            EventBus.getDefault().post(new CommandEvents.AutoSend(null));

            Intent serviceIntent = new Intent(AppSettings.getInstance(), GpsLoggingService.class);
            ContextCompat.startForegroundService(context, serviceIntent);
        } catch (Exception ex) {
            LOG.error("AlarmReceiver", ex);
        }
    }
}

should be rather

Intent serviceIntent = new Intent(context, GpsLoggingService.class);
mendhak commented 9 months ago

It's an early, global, static context. I'm not sure where I originally got it from but this is close enough: https://stackoverflow.com/questions/2002288/static-way-to-get-context-in-android

The AppSettings initializes a bunch of necessary things so it was simplest to use that everywhere.