I have an app which shows users current coordinates and update it as every 5 meters. but i have problem in the line which i call for "requestLocationUpdates" which is in this line:
Location location1 = locationManager.requestLocationUpdates(GpsProvider, 0, MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, mylocationListener);
.
if i place Context as last parameter i will get an error which tells(note that "GpsTracker" is my Main Activity) :
"Cannot resolve method 'requestLocationUpdates(java.lang.String, int, long, com.project.gpstrackerr.GpsTracker)'"
and if i place my locationlistener i will get an error which tells:
Incompatible types.
Required:android.location.Location
Found:void
here is my Full code of my GpsTracker Activity(which is my Main Activity)
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.text.SimpleDateFormat;
import java.util.Date;
public class GpsTracker extends Activity
{
public static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1;
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 20;
boolean isGPSEnabled = false, isNetworkEnabled = false;
boolean isGPSEnabled2 = false, isNetworkEnabled2 = false;
DatabaseTable cn;
DatabaseManager db = new DatabaseManager(this);
HttpClass JSONSaver = new HttpClass();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
Context context = GpsTracker.this;
LocationManager locationManager, locationManagerCHECK;
TextView ET_Coordinates, ET_NU;
ListView listView;
//for Coordinates details in Location Listener
double latitude;
double longitude;
String Date;
//for Getting Coordinates details
String strLat, strLong, strDate;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Show Last Known Location
ShowLastKnownLocation();
String NetworkProvider = LocationManager.NETWORK_PROVIDER;
String GpsProvider = LocationManager.GPS_PROVIDER;
locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
isGPSEnabled2 = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled2 = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}
public LocationListener mylocationListener = new LocationListener()
{
@Override
public void onLocationChanged(Location location)
{
Date date = new Date();
Date = dateFormat.format(date);
latitude = location.getLatitude();
longitude = location.getLongitude();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
Toast.makeText(GpsTracker.this, "Provide Status Changed" + "\n"
+ "Provider: " + provider + "\n"
+ "Status: " + status + "\n"
+ "Extras: " + extras + ".",
Toast.LENGTH_LONG).show();
}
@Override
public void onProviderEnabled(String provider)
{
Toast.makeText(GpsTracker.this,
"Provider Turned ON!", Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider)
{
Toast.makeText(GpsTracker.this,
"Provider is OFF!", Toast.LENGTH_SHORT).show();
}
};
protected void showUpdatedLocation(boolean checkGPSEnabled, boolean checkNetworkEnabled, String NetworkProvider, String GpsProvider, LocationManager locationManager)
{
if (checkGPSEnabled)
{
//I have problem in this Line
Location location1 = locationManager.requestLocationUpdates(GpsProvider, 0, MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, mylocationListener);
if (location1 != null)
{
Date = dateFormat.format(date);
latitude = location1.getLatitude();
longitude = location1.getLongitude();
db.addContact(new DatabaseTable(Date, latitude, longitude));
JSONSaver.writeJSON(Date, latitude, longitude);
showOnTextView(Date, latitude, longitude);
}
}
else if (isNetworkEnabled2)
{
Location location1 = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location1 != null)
{
//I have problem in this Line
Location location1 = locationManager.requestLocationUpdates(NetworkProvider, 0, MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, mylocationListener);
Date = dateFormat.format(date);
latitude = location1.getLatitude();
longitude = location1.getLongitude();
db.addContact(new DatabaseTable(Date, latitude, longitude));
JSONSaver.writeJSON(Date, latitude, longitude);
showOnTextView(Date, latitude, longitude);
}
}
}
}
I want to know where is my problem and how can i solve it and which of them(Context or LocationListener) is better to be used for doing my job.
It's my first time that i ask a question in Stackoverflow so if explanations have some lacks or i was not very clear or any other problems, feel free to ask for more information in comments I will try to be as much as clear i can. and beside of that I'm Kind of New to Programming so if you find something Stupid in my codes Don't get Surprised :D. Thanks for Further Helps!
Thanks for all people who helped me! the problem was for the package i used that was:
import android.location.LocationListener;
I used next package instead of first package:
import com.google.android.gms.location.LocationListener;
using this package in @chiru 's script will work fine for me!
Thanks A Lot Again!