Skip to content

Instantly share code, notes, and snippets.

@devuri
Forked from ertugrulozcan/AndroidManifest.xml
Created September 26, 2019 21:18
Show Gist options
  • Save devuri/a7d507b3c3296eacc4105412814d4ed6 to your computer and use it in GitHub Desktop.
Save devuri/a7d507b3c3296eacc4105412814d4ed6 to your computer and use it in GitHub Desktop.
import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
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;
/**
* 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;
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)
{
this.mContext = context;
getLocation();
}
//
// Konum bilgisini dondurur
//
public Location getLocation()
{
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)
{
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);
}
}
}
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity
{
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);
// Eger konum bilgisi alinabiliyorsa ekranda goruntulenir
if (gpsTracker.canGetLocation())
{
double latitude = gpsTracker.getLatitude();
double longitude = gpsTracker.getLongitude();
Toast.makeText(getApplicationContext(), "Konumunuz : \nEnlem " + latitude + "\nBoylam " + longitude, Toast.LENGTH_LONG).show();
}
else
{
// Konum bilgisi alinamiyorsa mesaj kutusunu goster
gpsTracker.showSettingsAlert();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings)
{
Intent intent = new Intent(this, AboutActivity.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
}
@ecemkara
Copy link

ecemkara commented Nov 5, 2020

Neden çalışmıyor ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment