Compare commits

...

3 commits

4 changed files with 88 additions and 5 deletions

View file

@ -23,20 +23,25 @@ public class LocationLoggingService extends Service implements LocationListener
@Override
public IBinder onBind(Intent intent) {
return null;
return new LocationLoggingServiceBinder(this);
}
@SuppressLint("MissingPermission")
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "Service started.");
LocationManager locationManager = (LocationManager) getSystemService(Service.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
subscribeToLocationUpdates(this);
return super.onStartCommand(intent, flags, startId);
}
@SuppressLint("MissingPermission")
public void subscribeToLocationUpdates(LocationListener locationListener)
{
LocationManager locationManager = (LocationManager) getSystemService(Service.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
@Override
public void onDestroy() {
Log.d(TAG, "Service has been destroyed.");

View file

@ -0,0 +1,15 @@
package com.proculite.logmylocation;
import android.os.Binder;
public class LocationLoggingServiceBinder extends Binder {
public final LocationLoggingService service;
public LocationLoggingServiceBinder(LocationLoggingService service){
this.service = service;
}
public LocationLoggingService getService(){
return this.service;
}
}

View file

@ -1,15 +1,25 @@
package com.proculite.logmylocation;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.TextView;
import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity {
public class MainActivity extends AppCompatActivity implements LocationListener, ServiceConnection {
private final String TAG = MainActivity.class.getName();
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -26,4 +36,55 @@ public class MainActivity extends AppCompatActivity {
return insets;
});
}
@Override
protected void onStart() {
super.onStart();
// Bind to service.
Intent intent = new Intent(this, LocationLoggingService.class);
bindService(intent, this, Context.BIND_AUTO_CREATE);
}
@Override
public void onLocationChanged(@NonNull Location location) {
TextView textView = findViewById(R.id.textViewMain);
String currentLocation = String.format(
"Latitude: %s\n" +
"Longitude: %s\n" +
"Altitude: %s\n" +
"Accuracy radius in meters: %s\n",
location.getLatitude(),
location.getLongitude(),
location.getAltitude(),
location.getAccuracy()
);
textView.setText(currentLocation);
}
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
Log.d(TAG, "Service connected.");
LocationLoggingServiceBinder binder = (LocationLoggingServiceBinder) iBinder;
binder.getService().subscribeToLocationUpdates(this);
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
Log.d(TAG, "Service disconnected.");
}
@Override
public void onBindingDied(ComponentName name) {
Log.d(TAG, "Service binding died.");
ServiceConnection.super.onBindingDied(name);
}
@Override
public void onNullBinding(ComponentName name) {
Log.d(TAG, "Service null binding.");
ServiceConnection.super.onNullBinding(name);
}
}

View file

@ -8,6 +8,8 @@
tools:context=".MainActivity">
<TextView
android:textAlignment="center"
android:id="@+id/textViewMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"