Android 如何超时RequestLocationUpdate?
当未找到Android 如何超时RequestLocationUpdate?,android,locationmanager,android-gps,android-looper,Android,Locationmanager,Android Gps,Android Looper,当未找到GPS修复程序时,代码将无限期地卡在活套中。 我想添加一个超时,这样如果没有找到GPSfix,它应该从looper中出来并执行代码的其余部分 如果你能帮我解决这个问题,我将不胜感激 public class service_task extends Service { @SuppressLint("MissingPermission") @Override public void onCreate() { } @Override pub
GPS
修复程序时,代码将无限期地卡在活套中
。
我想添加一个超时,这样如果没有找到GPS
fix,它应该从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.myLooper())代码>
然后使用handler.postDelayed(Runnable,long)
发布一个新的Runnable
,在给定延迟后取消位置更新并退出活套