Last active
November 8, 2020 14:24
-
-
Save ertugrulozcan/70973144b1c582618a6c to your computer and use it in GitHub Desktop.
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
<?xml version="1.0" encoding="utf-8"?> | |
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context=".MainActivity"> | |
<Button | |
android:id="@+id/ShowLocationButton" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Show Location" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintHorizontal_bias="0.5" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
</androidx.constraintlayout.widget.ConstraintLayout> |
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
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.aero.gpstracker"> | |
<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/Theme.GpsTracker"> | |
<activity android:name=".MainActivity"> | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
</application> | |
</manifest> |
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
package Utilities; | |
import android.Manifest; | |
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.Bundle; | |
import android.os.IBinder; | |
import android.provider.Settings; | |
import android.util.Log; | |
import androidx.core.app.ActivityCompat; | |
/** | |
* Ahmet Ertugrul OZCAN | |
* Cihazin konum bilgisini goruntuler | |
*/ | |
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; | |
// 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) | |
{ | |
this.mContext = context; | |
getLocation(); | |
} | |
// | |
// Konum bilgisini dondurur | |
// | |
public Location getLocation() | |
{ | |
Location location = null; | |
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) | |
{ | |
throw new Exception("Check GPS or internet connection!"); | |
} | |
else | |
{ | |
// Once internetten alinan konum bilgisi kayitlanir | |
if (isNetworkEnabled) | |
{ | |
if (ActivityCompat.checkSelfPermission((Activity)mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission((Activity)mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) | |
{ | |
throw new Exception("Permission needed for location!"); | |
} | |
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); | |
} | |
} | |
// 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); | |
} | |
} | |
} | |
} | |
} | |
catch (Exception e) | |
{ | |
e.printStackTrace(); | |
} | |
return location; | |
} | |
@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; | |
} | |
// 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
package com.aero.gpstracker; | |
import androidx.appcompat.app.AppCompatActivity; | |
import android.content.Intent; | |
import android.location.Location; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.widget.Button; | |
import android.widget.Toast; | |
import android.os.Bundle; | |
import Utilities.GPSTracker; | |
public class MainActivity extends AppCompatActivity | |
{ | |
private Button ShowLocationButton; | |
// GPSTracker nesnesi | |
private GPSTracker gpsTracker; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) | |
{ | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
ShowLocationButton = (Button) findViewById(R.id.ShowLocationButton); | |
// ShowLocationButton butonuna tiklandiginda | |
ShowLocationButton.setOnClickListener(new View.OnClickListener() | |
{ | |
@Override | |
public void onClick(View arg0) | |
{ | |
gpsTracker = new GPSTracker(MainActivity.this); | |
Location location = gpsTracker.getLocation(); | |
// Eger konum bilgisi alinabiliyorsa ekranda goruntulenir | |
if (location != null) | |
{ | |
double latitude = location.getLatitude(); | |
double longitude = location.getLongitude(); | |
Toast.makeText(getApplicationContext(), "Konumunuz : \nEnlem " + latitude + "\nBoylam " + longitude, Toast.LENGTH_LONG).show(); | |
} | |
else | |
{ | |
// Konum bilgisi alinamiyorsa mesaj kutusunu goster | |
gpsTracker.showSettingsAlert(); | |
} | |
} | |
}); | |
} | |
} |
Hocam çalıştıramadım ben
Layout ve manifest dosyalarını da ekledim gist'e. Manifest dosyanda "ACCESS_COARSE_LOCATION" ve "ACCESS_FINE_LOCATION" izinlerinin bulunması gerekiyor ve çalıştırdığın cihazda ayarlardan uygulamaya konum erişimine izin verilmiş olması gerekiyor.
Yine de çalışmazsa sorun android sürümünde olabilir, 6 yıl önce yazmış olduğum bir kod, yeni versiyonlarda değişiklik yapılan kütüphaneler vs olabilir belki. Kolay gelsin.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hocam çalıştıramadım ben