-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
324 additions
and
15 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
230 changes: 230 additions & 0 deletions
230
app/src/main/java/com/mekhti/smartsmsbox/utils/GpsTracker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,230 @@ | ||
package com.mekhti.smartsmsbox.utils; | ||
|
||
import android.app.Activity; | ||
import android.app.AlertDialog; | ||
import android.app.Service; | ||
import android.content.Context; | ||
import android.content.DialogInterface; | ||
import android.content.Intent; | ||
import android.content.pm.PackageManager; | ||
import android.location.Location; | ||
import android.location.LocationListener; | ||
import android.location.LocationManager; | ||
import android.os.Build; | ||
import android.os.Bundle; | ||
import android.os.IBinder; | ||
import android.provider.Settings; | ||
import android.support.v4.app.ActivityCompat; | ||
import android.support.v4.content.ContextCompat; | ||
import android.util.Log; | ||
|
||
public class GpsTracker extends Service implements LocationListener | ||
{ | ||
private final Context mContext; | ||
|
||
// Cihazda gps acik mi? | ||
boolean isGPSEnabled = false; | ||
|
||
// Cihazda veri baglantisi aktif mi? | ||
boolean isNetworkEnabled = false; | ||
|
||
boolean canGetLocation = false; | ||
|
||
// Konum | ||
Location location; | ||
// Enlem | ||
double latitude; | ||
// Boylam | ||
double longitude; | ||
|
||
// Konum guncellemesi gerektirecek minimum degisim miktari | ||
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // metre | ||
|
||
// Konum guncellemesi gerektirecek minimum sure miktari | ||
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // dakika | ||
|
||
// LocationManager nesnesi | ||
protected LocationManager locationManager; | ||
|
||
// | ||
// Kurucu Metod - Constructor | ||
// | ||
public GpsTracker(Context context, Activity activity) | ||
{ | ||
this.mContext = context; | ||
getLocation(activity); | ||
} | ||
|
||
// | ||
// Konum bilgisini dondurur | ||
// | ||
public Location getLocation(Activity activity) | ||
{ | ||
try | ||
{ | ||
locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE); | ||
|
||
// GPS acik mi? | ||
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); | ||
|
||
// Internet acik mi? | ||
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); | ||
|
||
if (!isGPSEnabled && !isNetworkEnabled) | ||
{ | ||
|
||
} | ||
else | ||
{ | ||
this.canGetLocation = true; | ||
|
||
// Once internetten alinan konum bilgisi kayitlanir | ||
if (isNetworkEnabled) | ||
{ | ||
if ( Build.VERSION.SDK_INT >= 23 && | ||
ContextCompat.checkSelfPermission( mContext, android.Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED && | ||
ContextCompat.checkSelfPermission( mContext, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { | ||
ActivityCompat.requestPermissions(activity, | ||
new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION}, | ||
1); | ||
return null; | ||
} | ||
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); | ||
Log.d("Network", "Network"); | ||
if (locationManager != null) | ||
{ | ||
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); | ||
if (location != null) | ||
{ | ||
latitude = location.getLatitude(); | ||
longitude = location.getLongitude(); | ||
} | ||
} | ||
} | ||
// GPS'ten alinan konum bilgisi; | ||
if (isGPSEnabled) | ||
{ | ||
if (location == null) | ||
{ | ||
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); | ||
Log.d("GPS Enabled", "GPS Enabled"); | ||
if (locationManager != null) | ||
{ | ||
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); | ||
if (location != null) | ||
{ | ||
latitude = location.getLatitude(); | ||
longitude = location.getLongitude(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
e.printStackTrace(); | ||
} | ||
|
||
return location; | ||
} | ||
|
||
// Enlem bilgisini dondurur | ||
public double getLatitude() | ||
{ | ||
if(location != null) | ||
{ | ||
latitude = location.getLatitude(); | ||
} | ||
|
||
return latitude; | ||
} | ||
|
||
// Boylam bilgisini dondurur | ||
public double getLongitude() | ||
{ | ||
if(location != null) | ||
{ | ||
longitude = location.getLongitude(); | ||
} | ||
|
||
return longitude; | ||
} | ||
|
||
@Override | ||
public void onLocationChanged(Location location) | ||
{ | ||
} | ||
|
||
@Override | ||
public void onProviderDisabled(String provider) | ||
{ | ||
} | ||
|
||
@Override | ||
public void onProviderEnabled(String provider) | ||
{ | ||
} | ||
|
||
@Override | ||
public void onStatusChanged(String provider, int status, Bundle extras) | ||
{ | ||
} | ||
|
||
@Override | ||
public IBinder onBind(Intent arg0) | ||
{ | ||
return null; | ||
} | ||
|
||
public boolean canGetLocation() | ||
{ | ||
return this.canGetLocation; | ||
} | ||
|
||
// Konum bilgisi kapali ise kullaniciya ayarlar sayfasina baglanti iceren bir mesaj goruntulenir | ||
public void showSettingsAlert() | ||
{ | ||
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext); | ||
|
||
// Mesaj basligi | ||
alertDialog.setTitle("GPS Kapalı"); | ||
|
||
// Mesaj | ||
alertDialog.setMessage("Konum bilgisi alınamıyor. Ayarlara giderek gps'i aktif hale getiriniz."); | ||
|
||
// Mesaj ikonu | ||
//alertDialog.setIcon(R.drawable.delete); | ||
|
||
// Ayarlar butonuna tiklandiginda | ||
alertDialog.setPositiveButton("Ayarlar", new DialogInterface.OnClickListener() | ||
{ | ||
public void onClick(DialogInterface dialog,int which) | ||
{ | ||
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); | ||
mContext.startActivity(intent); | ||
} | ||
}); | ||
|
||
// Iptal butonuna tiklandiginda | ||
alertDialog.setNegativeButton("İptal", new DialogInterface.OnClickListener() | ||
{ | ||
public void onClick(DialogInterface dialog, int which) | ||
{ | ||
dialog.cancel(); | ||
} | ||
}); | ||
|
||
// Mesaj kutusunu goster | ||
alertDialog.show(); | ||
} | ||
|
||
// LocationManager'in gps isteklerini durdurur | ||
public void stopUsingGPS() | ||
{ | ||
if(locationManager != null) | ||
{ | ||
locationManager.removeUpdates(GpsTracker.this); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.