Skip to content

Commit

Permalink
Merge pull request #5 from daupawar/master
Browse files Browse the repository at this point in the history
Stop location service
  • Loading branch information
netodevel authored Jun 16, 2017
2 parents 027802c + d7c3a66 commit 9938627
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 31 deletions.
36 changes: 21 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,27 +45,33 @@ dependencies {
```java
private LocationTracker locationTracker;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

@Override
protected void onStart() {
super.onStart();
new LocationTracker("my.action")
@Override
protected void onStart() {
super.onStart();
locationTracker=new LocationTracker("my.action")
.setInterval(50000)
.setGps(true)
.setNetWork(false)
.start(getBaseContext(), this);
}
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
locationTracker.onRequestPermission(requestCode, permissions, grantResults);
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
locationTracker.onRequestPermission(requestCode, permissions, grantResults);
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}

@Override
protected void onDestroy() {
super.onDestroy();
locationTracker.stopLocationService(this);
}
```

Create your `BroadcastReceiver` to get informations of location
Expand Down
10 changes: 8 additions & 2 deletions app/src/main/java/br/com/safety/sample/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package br.com.safety.sample;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;

import br.com.safety.locationlistenerhelper.core.LocationTracker;
Expand All @@ -18,17 +19,22 @@ protected void onCreate(Bundle savedInstanceState) {
@Override
protected void onStart() {
super.onStart();
new LocationTracker("my.action")
locationTracker=new LocationTracker("my.action")
.setInterval(50000)
.setGps(true)
.setNetWork(false)
.start(getBaseContext(), this);
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
locationTracker.onRequestPermission(requestCode, permissions, grantResults);
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}

@Override
protected void onDestroy() {
super.onDestroy();
locationTracker.stopLocationService(this);
}
}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.0'
classpath 'com.android.tools.build:gradle:2.3.2'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package br.com.safety.locationlistenerhelper.core;

import android.app.ActivityManager;
import android.content.Context;
import android.os.Build;

/**
Expand All @@ -13,4 +15,15 @@ public class AppUtils {
public static boolean hasM() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M;
}

public static boolean isServiceRunning(Context context, Class serviceClass) {
ActivityManager manager =
(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package br.com.safety.locationlistenerhelper.core;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
Expand Down Expand Up @@ -37,6 +37,10 @@ public class LocationService extends Service implements GoogleApiClient.Connecti

private AppPreferences appPreferences;

public static boolean isRunning(Context context) {
return AppUtils.isServiceRunning(context, LocationService.class);
}

@Override
public void onCreate() {
super.onCreate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
*/
public class LocationTracker {

private LocationService locationService;

private PermissionUtil.PermissionRequestObject mBothPermissionRequest;

private long interval = 0;
Expand All @@ -28,8 +26,6 @@ public class LocationTracker {

private Boolean netWork;

private AppPreferences appPreferences;

public LocationTracker(String actionReceiver) {
this.actionReceiver = actionReceiver;
}
Expand All @@ -54,18 +50,29 @@ public LocationTracker start(Context context, AppCompatActivity appCompatActivit
return this;
}

private void startLocationService(Context context, AppCompatActivity appCompatActivity) {
private void startLocationService(Context context) {
Intent serviceIntent = new Intent(context, LocationService.class);
saveSettingsInLocalStorage(context);
context.startService(serviceIntent);
}

/**
* Stop locaiton service if running
* @param context Context
*/
public void stopLocationService(Context context){
if(LocationService.isRunning(context)) {
Intent serviceIntent = new Intent(context, LocationService.class);
context.stopService(serviceIntent);
}
}

public void validatePermissions(Context context, AppCompatActivity appCompatActivity) {
if (AppUtils.hasM() && !(ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED
&& ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)) {
askPermissions(context, appCompatActivity);
} else {
startLocationService(context, appCompatActivity);
startLocationService(context);
}
}

Expand All @@ -76,7 +83,7 @@ public void askPermissions(final Context context, final AppCompatActivity appCom
@Override
protected void call(int requestCode, String[] permissions, int[] grantResults) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
startLocationService(context, appCompatActivity);
startLocationService(context);
} else {
Toast.makeText(context, "Permission Deined", Toast.LENGTH_LONG).show();
}
Expand All @@ -92,11 +99,11 @@ public void onRequestPermission(int requestCode, String[] permissions, int[] gra
}

public void saveSettingsInLocalStorage(Context context) {
this.appPreferences = new AppPreferences(context);
if (this.interval != 0) { this.appPreferences.putLong("INTERVAL", this.interval); }
this.appPreferences.putString("ACTION", this.actionReceiver);
this.appPreferences.putBoolean("GPS", this.gps);
this.appPreferences.putBoolean("NETWORK", this.netWork);
AppPreferences appPreferences = new AppPreferences(context);
if (this.interval != 0) { appPreferences.putLong("INTERVAL", this.interval); }
appPreferences.putString("ACTION", this.actionReceiver);
appPreferences.putBoolean("GPS", this.gps);
appPreferences.putBoolean("NETWORK", this.netWork);
}

}

0 comments on commit 9938627

Please sign in to comment.