[英]Calling Location Manager twice in a service in android
我正在嘗試兩次致電位置經理。
首先,它將調用Manager onStart,然后將計時器放置20秒鍾,然后再次調用它。
第一次工作,但是20秒后第二次根本沒有調用位置管理器。
以下是我嘗試過的代碼:
public void onStart(Intent intent, int startId)
{
super.onStart(intent, startId);
Log.e("Location1", "Inside onStart");
t = new Timer();
locationFirst = "First";
talker = new TextToSpeech(this, this);
location = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new MyLocationListener();
location.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 10,locationListener);
}
public void testMethod()
{
//Declare the timer
//Set the schedule function and rate
t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
Log.e("Time",""+time);
time += 1;
String todis = String.valueOf(time);
if(todis.contains("20"))
{
Looper.myLooper();
Looper.prepare();
talker.speak("Testing Service inside 20 in a App",TextToSpeech.QUEUE_ADD,null);
locationFirst = "Second";
locationTocall();
time = 0;
t.cancel();
}
}
}, 0, 1000);
}
public void locationTocall()
{
locationListener = new MyLocationListener();
location.requestLocationUpdates(location.NETWORK_PROVIDER, 5000, 10,locationListener);
}
private class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location loc)
{
Log.e("Location1", "Inside MyLocation");
String longitude = String.valueOf(loc.getLongitude());
String latitude = String.valueOf(loc.getLatitude());
String s = latitude+"\n"+longitude;
Log.e ("Location1", " "+s);
location.removeUpdates(locationListener);
if(locationFirst.equalsIgnoreCase("FIRST"))
{
testMethod();
}
else
{
}
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
在您的情況下,位置更新的最小距離是10米,可能是因為這個原因。
我認為您在onStart
方法中缺少對timer.start()
的onStart
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.