Android LocationManager requestLocationUpdates 改变了吗?
Android LocationManager requestLocationUpdates changed?
我的应用程序在 Ice Cream Sandwich 中运行得非常好,但现在我在 KitKat 上试用它并遇到了一些问题。
该应用程序是我 运行 在我的旧 phone 中的一种服务器,它会在请求时提供位置。在 ICS 中,当请求位置时,GPS 图标开始闪烁,应用程序很快就会收到位置更新并将其发送出去。但现在有了 KitKat,GPS 图标在请求位置时不会开始闪烁。该应用程序提供 60 秒的时间来查找 GPS 位置,但通常 GPS 在此期间甚至没有激活。 GPS 仍然时不时地突然自动激活(在 60 年代期间),并将位置提供给我的应用程序。
为什么即使我的应用请求位置,GPS 也没有激活? 如前所述,我的应用程序可以毫无问题地使用 ICS。 而且我的清单中确实设置了所需的权限。
public 个变量:
public static LocationManager mlocManager = null;
public static LocationListener mlocListener_fast = null;
创建时:
mlocListener_fast = new MyLocationListener();
mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
处理用户请求的命令(位置请求)
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener_fast);
我应该做些什么来立即获取 GPS 位置,而不用等待突然的位置 updates/GPS 激活,可能是由 Android 或其他应用程序触发的?不幸的是我没有任何其他 Android 设备我可以试试这个。
编辑:
似乎如果应用程序在启动时不请求位置,那么每次请求时位置请求都会起作用。但是,如果在启动时请求(并接收到)位置,则位置请求不再有效。是什么原因造成的?我使用完全相同的行(相同的位置管理器和相同的位置侦听器)在启动时和稍后请求时进行位置请求。
即使启动时使用的位置侦听器与后来使用的不同,位置请求也不再起作用。甚至在再次请求位置之前再次尝试初始化位置管理器,但没有帮助。这是怎么回事??
编辑 2:
KitKat 似乎无法请求多个位置请求。我曾经有多个用于不同目的的位置侦听器。例如,一个用于每小时更新一次位置,另一个用于立即获取位置(用户请求更新)。现在看来,如果我像往常一样有 1/60 分钟的位置侦听器 运行,那么 KitKat 位置管理器将无法处理即时位置请求。有人遇到过这个问题吗?最好知道哪个 Android 版本有这个问题。
此问题的解决方法是仅使用一个 LocationManager 和一个 LocationListener。如果您的应用程序需要不同类型的同步位置请求(使用不同的参数),那么您需要实现一个 "location request handler" 来决定位置请求应该使用哪些参数,即哪些参数对位置有最严格的要求。
这是一个简单的示例代码,解释了 "location request handler" 的概念:
class LR {
long lock_min_time; // defined in set_lock_lr before using
float lock_min_dist;
boolean lock_active = false;
long idle_min_time = 3600000; // 1 per hour
float idle_min_dist = 200;
boolean idle_active = true;
long fast_min_time = 0;
float fast_min_dist = 0;
boolean fast_active = false;
//constructor
public LR()
{}
public void set_lock_lr(long min_time, float min_dist, boolean active)
{
lock_active = active;
lock_min_dist = min_dist;
lock_min_time = min_time;
System.out.println("LR lock set: "+min_time+", "+min_dist+", "+active);
update_location_request();
}
public void set_idle_lr(boolean active)
{
idle_active = active;
System.out.println("LR idle set: "+active);
update_location_request();
}
public void set_fast_lr(boolean active)
{
fast_active = active;
System.out.println("LR fast set: "+active);
update_location_request();
}
private void update_location_request()
{
// Remove current location request
mlocManager_basic.removeUpdates(mlocListener_basic);
if(fast_active)
{
mlocManager_basic.requestLocationUpdates(LocationManager.GPS_PROVIDER, fast_min_time, fast_min_dist, mlocListener_basic);
System.out.println("LR: fast_active");
}
else if(lock_active)
{
mlocManager_basic.requestLocationUpdates(LocationManager.GPS_PROVIDER, lock_min_time, lock_min_dist, mlocListener_basic);
System.out.println("LR: lock_active");
}
else if(idle_active) // only idle updates
{
mlocManager_basic.requestLocationUpdates(LocationManager.GPS_PROVIDER, idle_min_time, idle_min_dist, mlocListener_basic);
System.out.println("LR: idle_active");
}
}
}
我的应用程序在 Ice Cream Sandwich 中运行得非常好,但现在我在 KitKat 上试用它并遇到了一些问题。
该应用程序是我 运行 在我的旧 phone 中的一种服务器,它会在请求时提供位置。在 ICS 中,当请求位置时,GPS 图标开始闪烁,应用程序很快就会收到位置更新并将其发送出去。但现在有了 KitKat,GPS 图标在请求位置时不会开始闪烁。该应用程序提供 60 秒的时间来查找 GPS 位置,但通常 GPS 在此期间甚至没有激活。 GPS 仍然时不时地突然自动激活(在 60 年代期间),并将位置提供给我的应用程序。
为什么即使我的应用请求位置,GPS 也没有激活? 如前所述,我的应用程序可以毫无问题地使用 ICS。 而且我的清单中确实设置了所需的权限。
public 个变量:
public static LocationManager mlocManager = null;
public static LocationListener mlocListener_fast = null;
创建时:
mlocListener_fast = new MyLocationListener();
mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
处理用户请求的命令(位置请求)
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener_fast);
我应该做些什么来立即获取 GPS 位置,而不用等待突然的位置 updates/GPS 激活,可能是由 Android 或其他应用程序触发的?不幸的是我没有任何其他 Android 设备我可以试试这个。
编辑:
似乎如果应用程序在启动时不请求位置,那么每次请求时位置请求都会起作用。但是,如果在启动时请求(并接收到)位置,则位置请求不再有效。是什么原因造成的?我使用完全相同的行(相同的位置管理器和相同的位置侦听器)在启动时和稍后请求时进行位置请求。
即使启动时使用的位置侦听器与后来使用的不同,位置请求也不再起作用。甚至在再次请求位置之前再次尝试初始化位置管理器,但没有帮助。这是怎么回事??
编辑 2:
KitKat 似乎无法请求多个位置请求。我曾经有多个用于不同目的的位置侦听器。例如,一个用于每小时更新一次位置,另一个用于立即获取位置(用户请求更新)。现在看来,如果我像往常一样有 1/60 分钟的位置侦听器 运行,那么 KitKat 位置管理器将无法处理即时位置请求。有人遇到过这个问题吗?最好知道哪个 Android 版本有这个问题。
此问题的解决方法是仅使用一个 LocationManager 和一个 LocationListener。如果您的应用程序需要不同类型的同步位置请求(使用不同的参数),那么您需要实现一个 "location request handler" 来决定位置请求应该使用哪些参数,即哪些参数对位置有最严格的要求。
这是一个简单的示例代码,解释了 "location request handler" 的概念:
class LR {
long lock_min_time; // defined in set_lock_lr before using
float lock_min_dist;
boolean lock_active = false;
long idle_min_time = 3600000; // 1 per hour
float idle_min_dist = 200;
boolean idle_active = true;
long fast_min_time = 0;
float fast_min_dist = 0;
boolean fast_active = false;
//constructor
public LR()
{}
public void set_lock_lr(long min_time, float min_dist, boolean active)
{
lock_active = active;
lock_min_dist = min_dist;
lock_min_time = min_time;
System.out.println("LR lock set: "+min_time+", "+min_dist+", "+active);
update_location_request();
}
public void set_idle_lr(boolean active)
{
idle_active = active;
System.out.println("LR idle set: "+active);
update_location_request();
}
public void set_fast_lr(boolean active)
{
fast_active = active;
System.out.println("LR fast set: "+active);
update_location_request();
}
private void update_location_request()
{
// Remove current location request
mlocManager_basic.removeUpdates(mlocListener_basic);
if(fast_active)
{
mlocManager_basic.requestLocationUpdates(LocationManager.GPS_PROVIDER, fast_min_time, fast_min_dist, mlocListener_basic);
System.out.println("LR: fast_active");
}
else if(lock_active)
{
mlocManager_basic.requestLocationUpdates(LocationManager.GPS_PROVIDER, lock_min_time, lock_min_dist, mlocListener_basic);
System.out.println("LR: lock_active");
}
else if(idle_active) // only idle updates
{
mlocManager_basic.requestLocationUpdates(LocationManager.GPS_PROVIDER, idle_min_time, idle_min_dist, mlocListener_basic);
System.out.println("LR: idle_active");
}
}
}