To get current latitude and longitude
create one class name CurrentLocListener
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.util.Log;
public class CurrentLocListener implements LocationListener {
private static final String TAG = "CurrentLocListener - ";
Context mContext;
String latitude,longitude,token;
public CurrentLocListener(Context context){
mContext = context;
}
public void onLocationChanged(Location location) {
Log.i("Lock Me If You Can", TAG + "onLocationChanged - Latitude: " + String.valueOf(location.getLatitude()) + " Longitude: " + String.valueOf(location.getLongitude()));
Utility.setSharedKey("Latitude", String.valueOf(location.getLatitude()), mContext);
Utility.setSharedKey("Longitude", String.valueOf(location.getLongitude()), mContext);
//Intent mIntent = new Intent(General.ACTION_LOCATION_CHANGED);
//mContext.sendBroadcast(mIntent);
}
public void onProviderDisabled(String provider) {
Log.i("Lock Me If You Can", TAG + "onProviderDisabled - ");
}
public void onProviderEnabled(String provider) {
Log.i("Lock Me If You Can", TAG + "onProviderEnabled - ");
}
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.i("Lock Me If You Can", TAG + "onStatusChanged - ");
}
}
in your main activity create below method
public static void registerLocationListener(Context context) {
LocationManager lManager = null;
CurrentLocListener cListener = null;
lManager = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
boolean isGpsEnabled = false;
boolean isNetworkEnabled = false;
isGpsEnabled = lManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = lManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (isGpsEnabled) {
cListener = new CurrentLocListener(context);
lManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
cListener);
Location location = lManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
setSharedKey("Latitude",
String.valueOf(location.getLatitude()), context);
setSharedKey("Longitude",
String.valueOf(location.getLongitude()), context);
}
}
if (isNetworkEnabled) {
cListener = new CurrentLocListener(context);
lManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
0, 0, cListener);
Location location = lManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
setSharedKey("Latitude",
String.valueOf(location.getLatitude()), context);
setSharedKey("Longitude",
String.valueOf(location.getLongitude()), context);
}
}
}
public static void setSharedKey(String key, String value, Context context) {
SharedPreferences spre = context.getSharedPreferences(
context.getPackageName(), Context.MODE_PRIVATE);
SharedPreferences.Editor prefEditor = spre.edit();
prefEditor.putString(key, value);
//prefEditor.clear();
prefEditor.commit();
}
public static String getSharedKey(String key, Context context) {
SharedPreferences spre = context.getSharedPreferences(
context.getPackageName(), Context.MODE_PRIVATE);
return spre.getString(key, "");
}
in your oncreate method write below code
registerLocationListener(mcontext);
latitude = Utility.getSharedKey("Latitude",mcontext);
longitude = Utility.getSharedKey("Longitude",mcontext);
give permissions in your menifest file
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
create one class name CurrentLocListener
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.util.Log;
public class CurrentLocListener implements LocationListener {
private static final String TAG = "CurrentLocListener - ";
Context mContext;
String latitude,longitude,token;
public CurrentLocListener(Context context){
mContext = context;
}
public void onLocationChanged(Location location) {
Log.i("Lock Me If You Can", TAG + "onLocationChanged - Latitude: " + String.valueOf(location.getLatitude()) + " Longitude: " + String.valueOf(location.getLongitude()));
Utility.setSharedKey("Latitude", String.valueOf(location.getLatitude()), mContext);
Utility.setSharedKey("Longitude", String.valueOf(location.getLongitude()), mContext);
//Intent mIntent = new Intent(General.ACTION_LOCATION_CHANGED);
//mContext.sendBroadcast(mIntent);
}
public void onProviderDisabled(String provider) {
Log.i("Lock Me If You Can", TAG + "onProviderDisabled - ");
}
public void onProviderEnabled(String provider) {
Log.i("Lock Me If You Can", TAG + "onProviderEnabled - ");
}
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.i("Lock Me If You Can", TAG + "onStatusChanged - ");
}
}
in your main activity create below method
public static void registerLocationListener(Context context) {
LocationManager lManager = null;
CurrentLocListener cListener = null;
lManager = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
boolean isGpsEnabled = false;
boolean isNetworkEnabled = false;
isGpsEnabled = lManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = lManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (isGpsEnabled) {
cListener = new CurrentLocListener(context);
lManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
cListener);
Location location = lManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
setSharedKey("Latitude",
String.valueOf(location.getLatitude()), context);
setSharedKey("Longitude",
String.valueOf(location.getLongitude()), context);
}
}
if (isNetworkEnabled) {
cListener = new CurrentLocListener(context);
lManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
0, 0, cListener);
Location location = lManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
setSharedKey("Latitude",
String.valueOf(location.getLatitude()), context);
setSharedKey("Longitude",
String.valueOf(location.getLongitude()), context);
}
}
}
public static void setSharedKey(String key, String value, Context context) {
SharedPreferences spre = context.getSharedPreferences(
context.getPackageName(), Context.MODE_PRIVATE);
SharedPreferences.Editor prefEditor = spre.edit();
prefEditor.putString(key, value);
//prefEditor.clear();
prefEditor.commit();
}
public static String getSharedKey(String key, Context context) {
SharedPreferences spre = context.getSharedPreferences(
context.getPackageName(), Context.MODE_PRIVATE);
return spre.getString(key, "");
}
in your oncreate method write below code
registerLocationListener(mcontext);
latitude = Utility.getSharedKey("Latitude",mcontext);
longitude = Utility.getSharedKey("Longitude",mcontext);
give permissions in your menifest file
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
No comments:
Post a Comment
Comments