【发布时间】:2016-11-21 07:08:36
【问题描述】:
我有一个应用程序,它向用户显示当前坐标并每 5 米更新一次。 但我在这一行中调用“requestLocationUpdates”的行有问题:
Location location1 = locationManager.requestLocationUpdates(GpsProvider, 0, MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, mylocationListener);
.
如果我将 Context 作为最后一个参数,我会收到一个错误提示(注意“GpsTracker”是我的主要活动):
"Cannot resolve method 'requestLocationUpdates(java.lang.String, int, long, com.project.gpstrackerr.GpsTracker)'"
如果我放置我的位置监听器,我会收到一个错误消息:
Incompatible types.
Required:android.location.Location
Found:void
这是我的 GpsTracker 活动的完整代码(这是我的主要活动)
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);
}
}
}
}
我想知道我的问题出在哪里,我该如何解决它以及它们中的哪一个(上下文或 LocationListener)更适合用于完成我的工作。
这是我第一次在 Stackoverflow 中提问,所以如果解释有一些不足,或者我不是很清楚或有任何其他问题, 随时询问有关 cmets 的更多信息,我将尽可能清楚。 除此之外,我对编程有点陌生,所以如果你在我的代码中发现一些愚蠢的东西,不要感到惊讶:D。 感谢您的进一步帮助!
【问题讨论】:
标签: java android android-location locationmanager locationlistener