我正在开发一个位置感应应用程序,但我已陷入停滞状态。 我想要做的是能够向Android设备询问单个GPS位置并将其返回。
到目前为止,这就是我所拥有的。应该定期更新位置的服务:
import android.app.Service;
import android.content.Context;
import android.content.Intent
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.widget.Toast;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class KeepTime extends Service implements LocationListener {
float minAccuracyMeters = 20.0f;
LocationManager locationManager;
int index = 0;
int maxAmountOfTries = 5;
Timer timer;
boolean currentlyListening = false;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("Service started..");
Toast.makeText(this, "Service started", Toast.LENGTH_SHORT).show();
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
if(!isLocationEnabled()){
System.out.println("Location service is not active. Stopping self.");
Toast.makeText(this, "Location service is not active. Stopping self.", Toast.LENGTH_SHORT).show();
this.stopSelf();
return 0;
}
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
if (currentlyListening) {
} else {
setListener();
}
}
}, 0, 1000 * 60 * 10);
return Service.START_NOT_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
mll.removeListener();
mll.cancelTimer();
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_SHORT).show();
System.out.println("Service destroyed..");
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
private boolean isLocationEnabled() {
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) ||
locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}
private void setListener()
{
System.out.println("Set listener called..");
try {
System.out.println("About to set listeners");
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
System.out.println("Location listeners set..");
currentlyListening = true;
}catch(SecurityException e){
System.out.println("Security exception. "+e);
}
}
public void removeListener() {
try {
locationManager.removeUpdates(this);
System.out.println("Removed updates..");
currentlyListening = false;
} catch (SecurityException se) {
System.out.println("Security exception: " + se);
}
}
@Override
public void onLocationChanged(Location location) {
System.out.println("Location changed..");
double longitudeNetwork = location.getLongitude();
double latitudeNetwork = location.getLatitude();
double accuracy = location.getAccuracy();
if(accuracy < minAccuracyMeters || index > maxAmountOfTries) {
System.out.println("Accuracy is less than " + minAccuracyMeters + " Recording..");
System.out.println(longitudeNetwork+ " "+latitudeNetwork+ " "+accuracy);
removeListener();
}
else{
System.out.println("Accuracy was not good enough. Still recorded location.");
}
index++;
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
System.out.println("Status changed");
}
@Override
public void onProviderEnabled(String provider) {
System.out.println("Provider enabled.");
}
@Override
public void onProviderDisabled(String provider) {
System.out.println("Provider disabled.");
}
}
每隔10分钟,我希望在达到一定精度或经过一定时间后打印出位置。但它抛出异常。 这是一个例外:
FATAL EXCEPTION: Timer-0
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
我试过调试准备,但这只会造成更大的破坏。 任何想法如何实现我的目标?
答案 0 :(得分:1)
TimerTask适用于不同的线程,因此您的问题可能会引用this
并检查this项目,似乎它完全提到了你的整个想法
修改强>
不是浪费时间,而是为他展示关键材料。对于第一个链接,我已经告诉它是关键。
TimerTask适用于不同的线程
我并没有直接将你转发给浪费的链接,因为我会分析你的整个代码。它指的是github上commonsguy的完美项目。所以你可以通过查看commonsguy项目来消除你的错误。
答案 1 :(得分:0)
locationmanager.requestlocationupdates有一个间隔参数,以便何时再次获取位置。尝试按ctrl并将鼠标悬停在requestlocationupdate语法上。