Bind to service and display location updates in activity.
This commit is contained in:
parent
66381cacf4
commit
77e343bfbb
|
|
@ -1,15 +1,25 @@
|
||||||
package com.proculite.logmylocation;
|
package com.proculite.logmylocation;
|
||||||
|
|
||||||
|
import android.content.ComponentName;
|
||||||
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.content.ServiceConnection;
|
||||||
|
import android.location.Location;
|
||||||
|
import android.location.LocationListener;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.os.IBinder;
|
||||||
|
import android.util.Log;
|
||||||
|
import android.widget.TextView;
|
||||||
|
|
||||||
import androidx.activity.EdgeToEdge;
|
import androidx.activity.EdgeToEdge;
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
import androidx.core.graphics.Insets;
|
import androidx.core.graphics.Insets;
|
||||||
import androidx.core.view.ViewCompat;
|
import androidx.core.view.ViewCompat;
|
||||||
import androidx.core.view.WindowInsetsCompat;
|
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
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
|
|
@ -26,4 +36,55 @@ public class MainActivity extends AppCompatActivity {
|
||||||
return insets;
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -8,6 +8,8 @@
|
||||||
tools:context=".MainActivity">
|
tools:context=".MainActivity">
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
|
android:textAlignment="center"
|
||||||
|
android:id="@+id/textViewMain"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:text="Hello World!"
|
android:text="Hello World!"
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue