Compare commits
No commits in common. "77e343bfbb33278569ac4a21eaae2c30a0f73e69" and "fa32356a9c28eac4e483744072e6559821105063" have entirely different histories.
77e343bfbb
...
fa32356a9c
|
|
@ -23,25 +23,20 @@ public class LocationLoggingService extends Service implements LocationListener
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public IBinder onBind(Intent intent) {
|
public IBinder onBind(Intent intent) {
|
||||||
return new LocationLoggingServiceBinder(this);
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@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.");
|
||||||
|
|
||||||
subscribeToLocationUpdates(this);
|
LocationManager locationManager = (LocationManager) getSystemService(Service.LOCATION_SERVICE);
|
||||||
|
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.");
|
||||||
|
|
|
||||||
|
|
@ -1,15 +0,0 @@
|
||||||
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,25 +1,15 @@
|
||||||
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 implements LocationListener, ServiceConnection {
|
public class MainActivity extends AppCompatActivity {
|
||||||
private final String TAG = MainActivity.class.getName();
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
|
|
@ -36,55 +26,4 @@ public class MainActivity extends AppCompatActivity implements LocationListener,
|
||||||
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,8 +8,6 @@
|
||||||
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