Okay I realized my original code was nearly 400 lines, so I formatted it a bit leaving only most vital parts in, so I hope somebody can help.
So basically I am running LocationListener in background service, here is the code: package als.wakeup;
import java.text.DecimalFormat;
import java.util.Calendar;
import com.google.android.gms.maps.model.LatLng;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.location.Criteria;
import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.BatteryManager;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;
public class Awake_Alarm extends Service implements GpsStatus.Listener{
public static final String BROADCAST_ACTION = "als.wakeup.Intro";
private static final int TWO_MINUTES = 1000 * 60 * 2;
public LocationManager locationManager;
public MyLocationListener listener;
public Location previousBestLocation = null;
public ConversePrefs cp;
public ConversePrefs_sets cpss;
int cp_counter = 0;
Intent intent;
int counter = 0;
int batt_level = 0;
private static int alarmID = 2;
private LocationManager mService;
private GpsStatus mStatus;
@Override
public void onCreate() {
super.onCreate();
intent = new Intent(BROADCAST_ACTION);
}
private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context ctxt, Intent intent) {
//Functions.
}
};
@Override
public void onStart(Intent intent, int startId) {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
listener = new MyLocationListener();
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 4000, 0, listener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 4000, 0, listener);
cp = new ConversePrefs(this);
cpss = new ConversePrefs_sets(this);
mService = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
mService.addGpsStatusListener(this);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
protected boolean isBetterLocation(Location location, Location currentBestLocation) {
//Functions
}
private boolean isSameProvider(String provider1, String provider2) {
//Functions
}
@Override
public void onDestroy() {
// handler.removeCallbacks(sendUpdatesToUI);
super.onDestroy();
Log.v("STOP_SERVICE", "DONE");
locationManager.removeUpdates(listener);
}
public static Thread performOnBackgroundThread(final Runnable runnable) {
final Thread t = new Thread() {
@Override
public void run() {
try {
runnable.run();
} finally {
}
}
};
t.start();
return t;
}
public class MyLocationListener implements LocationListener
{
public void onLocationChanged(final Location loc)
{
//Functions.
}
public void onProviderDisabled(String provider){}
public void onProviderEnabled(String provider){}
public void onStatusChanged(String provider, int status, Bundle extras){}
}
@Override
public void onGpsStatusChanged(int event) {
mStatus = mService.getGpsStatus(mStatus);
switch (event) {
case GpsStatus.GPS_EVENT_STARTED:
Toast.makeText(getApplicationContext(), "Started", Toast.LENGTH_SHORT).show();
Log.d("GPS", "Start");
break;
case GpsStatus.GPS_EVENT_STOPPED:
Toast.makeText(getApplicationContext(), "Stopped", Toast.LENGTH_SHORT).show();
Log.d("GPS", "Stopped");
break;
case GpsStatus.GPS_EVENT_FIRST_FIX:
Toast.makeText(getApplicationContext(), "Pre-Started", Toast.LENGTH_SHORT).show();
Log.d("GPS", "Pre Start");
break;
case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
break;
}
}
}
But my onGpsStatusChange gets called only when service gets launched. Once app starts it runs and says: Pre-Started, Stopped, Started. And then even if I turn off GPS, Wifi and Mobile Data + Airplane Mode on it doesn't even work anymore.
Basic layout:
public class Awake_Alarm extends Service implements GpsStatus.Listener{
onStart{
//Registers GpsStatusListener
}
public class MyLocationListener implements LocationListener{
}
@Override
public void onGpsStatusChanged(int event) {
//Functions that get called only once for some reason.
}
}