Skip to content

Commit

Permalink
Added Gps tracker code - start stop service
Browse files Browse the repository at this point in the history
Added gps locatation in android code, can start stop location
Can get location on UI.
Seprate comonent to get location
  • Loading branch information
dinkar1708 authored and hhaasbroek committed Aug 16, 2018
1 parent 11fd3a8 commit 07f3557
Show file tree
Hide file tree
Showing 27 changed files with 818 additions and 0 deletions.
1 change: 1 addition & 0 deletions locationTrack/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
35 changes: 35 additions & 0 deletions locationTrack/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 27



defaultConfig {
applicationId "app.prior.lollipop"
minSdkVersion 15
targetSdkVersion 27
versionCode 1
versionName "1.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation 'com.android.support:appcompat-v7:27.0.2'
implementation 'com.android.support:design:27.0.2'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.google.android.gms:play-services-location:11.8.0'
}
21 changes: 21 additions & 0 deletions locationTrack/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package app.prior.tracker;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() throws Exception {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();

assertEquals("app.prior.lollipop", appContext.getPackageName());
}
}
28 changes: 28 additions & 0 deletions locationTrack/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="app.prior.tracker">

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<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">
<activity android:name="app.prior.tracker.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<service
android:name="app.prior.tracker.MyIntentService"
android:exported="false"></service>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
package app.prior.tracker;

import android.content.Context;
import android.location.Location;
import android.os.Looper;
import android.support.annotation.NonNull;
import android.util.Log;

import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationCallback;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationResult;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;


/**
* stand alone component for location updates
*/
public class LocationUpdatesComponent {

private static final String TAG = LocationUpdatesComponent.class.getSimpleName();

/**
* The desired interval for location updates. Inexact. Updates may be more or less frequent.
*/
private static final long UPDATE_INTERVAL_IN_MILLISECONDS = 3 * 1000;

/**
* The fastest rate for active location updates. Updates will never be more frequent
* than this value.
*/
private static final long FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS =
UPDATE_INTERVAL_IN_MILLISECONDS / 2;

private LocationRequest mLocationRequest;

/**
* Provides access to the Fused Location Provider API.
*/
private FusedLocationProviderClient mFusedLocationClient;

/**
* Callback for changes in location.
*/
private LocationCallback mLocationCallback;

/**
* The current location.
*/
private Location mLocation;

public ILocationProvider iLocationProvider;

public LocationUpdatesComponent(ILocationProvider iLocationProvider) {
this.iLocationProvider = iLocationProvider;
}

/**
* create first time to initialize the location components
*
* @param context
*/
public void onCreate(Context context) {
Log.i(TAG, "created...............");
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(context);

mLocationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
Log.i(TAG, "onCreate...onLocationResult...............loc " + locationResult.getLastLocation());

onNewLocation(locationResult.getLastLocation());
}
};
// create location request
createLocationRequest();
// get last known location
getLastLocation();
}

/**
* start location updates
*/
public void onStart() {
Log.i(TAG, "onStart ");
//hey request for location updates
requestLocationUpdates();
}

/**
* remove location updates
*/
public void onStop() {
Log.i(TAG, "onStop....");
removeLocationUpdates();
}

/**
* Makes a request for location updates. Note that in this sample we merely log the
* {@link SecurityException}.
*/
public void requestLocationUpdates() {
Log.i(TAG, "Requesting location updates");
try {
mFusedLocationClient.requestLocationUpdates(mLocationRequest,
mLocationCallback, Looper.getMainLooper());
} catch (SecurityException unlikely) {
Log.e(TAG, "Lost location permission. Could not request updates. " + unlikely);
}
}

/**
* Removes location updates. Note that in this sample we merely log the
* {@link SecurityException}.
*/
public void removeLocationUpdates() {
Log.i(TAG, "Removing location updates");
try {
mFusedLocationClient.removeLocationUpdates(mLocationCallback);
// Utils.setRequestingLocationUpdates(this, false);
// stopSelf();
} catch (SecurityException unlikely) {
// Utils.setRequestingLocationUpdates(this, true);
Log.e(TAG, "Lost location permission. Could not remove updates. " + unlikely);
}
}

/**
* get last location
*/
private void getLastLocation() {
try {
mFusedLocationClient.getLastLocation()
.addOnCompleteListener(new OnCompleteListener<Location>() {
@Override
public void onComplete(@NonNull Task<Location> task) {
if (task.isSuccessful() && task.getResult() != null) {
mLocation = task.getResult();
Log.i(TAG, "getLastLocation " + mLocation);
// Toast.makeText(getApplicationContext(), "" + mLocation, Toast.LENGTH_SHORT).show();
onNewLocation(mLocation);
} else {
Log.w(TAG, "Failed to get location.");
}
}
});
} catch (SecurityException unlikely) {
Log.e(TAG, "Lost location permission." + unlikely);
}
}

private void onNewLocation(Location location) {
Log.i(TAG, "New location: " + location);
// Toast.makeText(getApplicationContext(), "onNewLocation " + location, Toast.LENGTH_LONG).show();

mLocation = location;
if (this.iLocationProvider != null) {
this.iLocationProvider.onLocationUpdate(mLocation);
}
}

/**
* Sets the location request parameters.
*/
private void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}

/**
* implements this interface to get call back of location changes
*/
public interface ILocationProvider {
void onLocationUpdate(Location location);
}
}
Loading

0 comments on commit 07f3557

Please sign in to comment.