Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ android {

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
10 changes: 9 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.mapgears.sharedmemoryen">

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
android:theme="@style/AppTheme"
tools:ignore="AllowBackup">
<service
android:name=".BackgroundLocationService"
android:enabled="true" />

<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package com.mapgears.sharedmemoryen;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.Binder;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.RequiresApi;


public class BackgroundLocationService extends Service {

private static final String TAG = "SharedMemory";
private final LocationServiceBinder binder = new LocationServiceBinder();
private LocationListener mLocationListener;
private LocationManager mLocationManager;

public BackgroundLocationService() {
}

@Override
public IBinder onBind(Intent intent) { return binder; }

private class LocationListener implements android.location.LocationListener {

LocationListener() {
new Location(LocationManager.GPS_PROVIDER);
}

@Override
public void onLocationChanged(Location location) {
Log.d(TAG, "LocationChanged: " + location);
Toast.makeText(BackgroundLocationService.this, "LAT: " + location.getLatitude() + "\n LONG: " + location.getLongitude(), Toast.LENGTH_SHORT).show();
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {
}
}

@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
startForeground(12345678, getNotification());
startTracking();
return START_STICKY;
}

@Override
public void onDestroy() {
super.onDestroy();
if (mLocationManager != null) {
try{
mLocationManager.removeUpdates(mLocationListener);
} catch (Exception ignored) {}
}

}

private void startTracking() {

initializeLocationManager();
mLocationListener = new LocationListener();

try {
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 300, 1, mLocationListener);
} catch (java.lang.SecurityException ex) {
Log.i("TAG", "fail to request location update, ignore", ex);
} catch (IllegalArgumentException ex) {
Log.d("TAG", "gps provider does not exist " + ex.getMessage());
}

}

private void initializeLocationManager() {
if(mLocationManager == null){
mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
}
}

public void stopTracking(){
stopForeground(true);
this.onDestroy();
}

@RequiresApi(api = Build.VERSION_CODES.O)
private Notification getNotification() {

NotificationChannel channel = new NotificationChannel("channel_01", "GPS Status", NotificationManager.IMPORTANCE_DEFAULT);

NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);

Notification.Builder builder = new Notification.Builder(getApplicationContext(), "channel_01").setAutoCancel(true);
return builder.build();
}

class LocationServiceBinder extends Binder {
public BackgroundLocationService getService(){return BackgroundLocationService.this;}
}
}
107 changes: 107 additions & 0 deletions app/src/main/java/com/mapgears/sharedmemoryen/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,121 @@
package com.mapgears.sharedmemoryen;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

private static final int ACCES_FINE_LOCATION_REQUEST = 0;
private static final String TAG = "SharedMemory";
private BackgroundLocationService gpsService;
private boolean isTracking = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "Inside onCreateActivity");
}

@Override
protected void onStart() {
super.onStart();
initializeToggleServiceButton();
}

private void initializeToggleServiceButton() {
final Button startService = findViewById(R.id.start_service_button);
if (isServiceRunning())
startService.setText(R.string.stop_gps);
else
startService.setText(R.string.start_gps);

startService.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!isTracking) {
startTracking();
startService.setText(R.string.stop_gps);
}
else{
startService.setText(R.string.start_gps);
stopTracking();
}
}
});
}

private void startTracking(){
if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
final Intent intent = new Intent( this.getApplication(), BackgroundLocationService.class);
this.getApplication().bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
this.getApplication().startService(intent);
isTracking = true;

} else {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
ACCES_FINE_LOCATION_REQUEST);
}
}

private final ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
if( name.getClassName().endsWith("BackgroundLocationService")){
gpsService = ((BackgroundLocationService.LocationServiceBinder)service).getService();
TextView gpsStatusLabel = findViewById(R.id.l_appStatus);
gpsStatusLabel.setText(R.string.gps_service_status);
}
}

@Override
public void onServiceDisconnected(ComponentName name) {
if (name.getClassName().endsWith("BackgroundLocationService")){
gpsService = null;
}
}
};

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == ACCES_FINE_LOCATION_REQUEST){
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
startTracking();
}
}
}

private void stopTracking(){
gpsService.stopTracking();
isTracking = false;
}
private boolean isServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)){
if("com.mapgears.sharedmemoryen.BackgroundLocationService".equals(service.service.getClassName())) {
return true;
}
}
return false;
}

}
29 changes: 25 additions & 4 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,33 @@
tools:context=".MainActivity">

<TextView
android:id="@+id/l_appTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="16dp"
android:text="@string/app_name"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/l_appStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="@string/gps_service_status"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/l_appTitle" />

<Button
android:id="@+id/start_service_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:text="@string/start_gps"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/l_appStatus" />
</androidx.constraintlayout.widget.ConstraintLayout>
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
<resources>
<string name="app_name">Shared Memory End Node</string>
<string name="gps_service_status">GPS Ready</string>
<string name="start_gps">Start Tracking</string>
<string name="stop_gps">Stop Tracking</string>
</resources>
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {

}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.2'
classpath 'com.android.tools.build:gradle:3.6.1'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Tue Jan 21 17:34:54 EST 2020
#Mon Mar 02 19:51:21 EST 2020
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip