My buildLocationEvent(location) in onLocationChanged never runs? Is there a particular reason why this would be? I can see no reason, perhaps I am missing somethig obvious, please advise me what is going wrong here and how i can sort this so i can start to get the location data out properly.
I have copied the code below:
public class LService extends Service {
public static final String TAG = "Lervice";
private LocationListener listener;
private LocationManager manager;
private static LocationEvent locationEvent = new LocationEvent();
@RequiresApi(api = Build.VERSION_CODES.O)
@SuppressLint("MissingPermission")
@Override
public void onCreate() {
super.onCreate();
startMyOwnForeground();
listener = new LocationListener() {
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onLocationChanged(Location location) {
buildLocationEvent(location);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}
};
manager = (LocationManager)getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100, 0, listener);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
@RequiresApi(api = Build.VERSION_CODES.O)
private void startMyOwnForeground() {
String NOTIFICATION_CHANNEL_ID = "com.app.myapp";
String channelName = "LService";
NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
chan.setLightColor(Color.GREEN);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
assert manager != null;
manager.createNotificationChannel(chan);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
Notification notification = notificationBuilder.setOngoing(true)
.setContentTitle("App is running in background")
.setPriority(NotificationManager.IMPORTANCE_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.build();
startForeground(3, notification);
}
@Override
public void onDestroy() {
super.onDestroy();
if(manager != null){
manager.removeUpdates(listener);
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@RequiresApi(api = Build.VERSION_CODES.O)
public void buildLocationEvent(Location location){
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
double[] coordinate = {latLng.latitude, latLng.longitude};
locationEvent.setCoordinate(coordinate);
locationEvent.setAltitude(location.getAltitude());
locationEvent.setHorizontalAccuracy(location.getAccuracy());
locationEvent.setVerticalAccuracy(location.getVerticalAccuracyMeters());
locationEvent.setTimestamp(location.getTime());
sendLocationToManager(locationEvent);
}
private void sendLocationToManager(LocationEvent locationEvent){
Intent intent = new Intent("locationCreated");
sendLocalBroadcastEvent(intent, locationEvent);
}
private void sendLocalBroadcastEvent(Intent intent, LocationEvent locationEvent){
intent.putExtra("locationevent", locationEvent);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
}
Thank you for your help on this. If you need anything clarifying please let me know.
Permissions are called in Main when i run:
String[] appPermissions = {
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_BACKGROUND_LOCATION
};
private static final int PERMISSIONS_REQUEST_CODE = 1240;
//get permissions for location
Log.d(TAG, "permissions: "+ checkAndRequestPermissions()); //false
if(true) {
//I RUN MY CODE HERE
}
}
private boolean checkAndRequestPermissions() {
List<String> listPermissionsNeeded = new ArrayList<>();
for (String perm : appPermissions) {
if (ContextCompat.checkSelfPermission(this, perm) != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(perm);
}
}
if (!listPermissionsNeeded.isEmpty()) {
ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]),
PERMISSIONS_REQUEST_CODE
);
return false;
}
//All Permissions granted
return true;
}
Hope that helps.
This is how i start the service:
public void startUpdatingLocation(){
//Start sensor service
Intent locationIntent = new Intent(MyApplication.getAppContext(), LService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
MyApplication.getAppContext().startForegroundService(locationIntent);
} else {
MyApplication.getAppContext().startService(locationIntent);
}
}