Compare commits
3 commits
fa32356a9c
...
77e343bfbb
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
77e343bfbb | ||
|
|
66381cacf4 | ||
|
|
3b40094457 |
|
|
@ -23,20 +23,25 @@ public class LocationLoggingService extends Service implements LocationListener
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public IBinder onBind(Intent intent) {
|
public IBinder onBind(Intent intent) {
|
||||||
return null;
|
return new LocationLoggingServiceBinder(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressLint("MissingPermission")
|
|
||||||
@Override
|
@Override
|
||||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||||
Log.d(TAG, "Service started.");
|
Log.d(TAG, "Service started.");
|
||||||
|
|
||||||
LocationManager locationManager = (LocationManager) getSystemService(Service.LOCATION_SERVICE);
|
subscribeToLocationUpdates(this);
|
||||||
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
|
|
||||||
|
|
||||||
return super.onStartCommand(intent, flags, startId);
|
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
|
@Override
|
||||||
public void onDestroy() {
|
public void onDestroy() {
|
||||||
Log.d(TAG, "Service has been destroyed.");
|
Log.d(TAG, "Service has been destroyed.");
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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