如何使 requestLocationUpdates 超时?
how to timeout requestLocationUpdates?
当未找到 GPS
修复时,代码将无限期地卡在 looper
中。
我想添加一个超时,以便如果没有找到 GPS
修复,它应该从 looper
中出来并执行代码的剩余部分。
如果你能帮我解决这个问题,我将不胜感激。
public class service_task extends Service {
@SuppressLint("MissingPermission")
@Override
public void onCreate() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
private class ServerThread extends Thread implements LocationListener {
public LocationManager locationManager = null;
public String msg = "default";
public String id = "default";
private Location mLocation = null;
public Socket socket = null;
public int serviceid;
public ServerThread(LocationManager locationManager, int startid) {
super("UploaderService-Uploader");
this.locationManager = locationManager;
this.serviceid=startid;
}
@SuppressLint("MissingPermission")
public void run() {
Looper.prepare();
this.locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
Looper.loop();
if (mLocation!=null) {
msg = "GPS data:" + mLocation;
}else{
msg ="No GPS data";
}
stopSelf(serviceid);
}
@Override
public void onLocationChanged(Location location) {
mLocation = location;
Log.d("D", String.valueOf(location));
this.locationManager.removeUpdates(this);
Looper.myLooper().quit();
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
}
@SuppressLint("MissingPermission")
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("D", "startcommand");
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
ServerThread thread = new ServerThread(locationManager,startId);
thread.start();
return START_REDELIVER_INTENT;
}
}
创建一个使用您的 Looper
的 Handler
:handler = new Handler(Looper.myLooper());
然后使用 handler.postDelayed(Runnable, long)
到 post 一个新的 Runnable
,它会在给定的延迟后取消位置更新并退出您的 Looper
。
当未找到 GPS
修复时,代码将无限期地卡在 looper
中。
我想添加一个超时,以便如果没有找到 GPS
修复,它应该从 looper
中出来并执行代码的剩余部分。
如果你能帮我解决这个问题,我将不胜感激。
public class service_task extends Service {
@SuppressLint("MissingPermission")
@Override
public void onCreate() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
private class ServerThread extends Thread implements LocationListener {
public LocationManager locationManager = null;
public String msg = "default";
public String id = "default";
private Location mLocation = null;
public Socket socket = null;
public int serviceid;
public ServerThread(LocationManager locationManager, int startid) {
super("UploaderService-Uploader");
this.locationManager = locationManager;
this.serviceid=startid;
}
@SuppressLint("MissingPermission")
public void run() {
Looper.prepare();
this.locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
Looper.loop();
if (mLocation!=null) {
msg = "GPS data:" + mLocation;
}else{
msg ="No GPS data";
}
stopSelf(serviceid);
}
@Override
public void onLocationChanged(Location location) {
mLocation = location;
Log.d("D", String.valueOf(location));
this.locationManager.removeUpdates(this);
Looper.myLooper().quit();
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
}
@SuppressLint("MissingPermission")
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("D", "startcommand");
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
ServerThread thread = new ServerThread(locationManager,startId);
thread.start();
return START_REDELIVER_INTENT;
}
}
创建一个使用您的 Looper
的 Handler
:handler = new Handler(Looper.myLooper());
然后使用 handler.postDelayed(Runnable, long)
到 post 一个新的 Runnable
,它会在给定的延迟后取消位置更新并退出您的 Looper
。