[英]What is wrong with this Thread? It only gets runned one time. (Thread with GPS Location)
[英]Get Location only gets the location on UI Thread (GPS Location)
您好 Android 开发人员或 Java 开发人员,
今天遇到一个问题,获取Location的代码如下:
public class GpsTracker implements LocationListener{
public Location getLocation() {
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return null;
}
LocationManager locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (isGPSEnabled) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, this);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
return location;
}else {
}
return null;
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}
第二个代码:
public void getLocation(final int childId){
GpsTracker gpsTracker = new GpsTracker();
Location location = gpsTracker.getLocation();
if (location != null){
final double longitude = location.getLongitude();
final double latitude = location.getLatitude();
StringRequest stringRequest = new StringRequest(Request.Method.POST, WebConfig.INSERT_GPS_LOCATION, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("FKchild_id", Integer.toString(childId));
params.put("longitude", Double.toString(longitude));
params.put("latitude", Double.toString(latitude));
return params;
}
};
RequestHandler.getInstance(context).addToRequestQueue(stringRequest);
}
}
上述代码完美运行。
现在问题来了
private void backgroundTasks() {
final BackgroundTask backgroundTask = new BackgroundTask(this);
ScheduledExecutorService executor = Executors.newScheduledThreadPool(8);
executor.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
final int userId = SharedPrefManager.getInstance(getApplicationContext()).getUserId();
String userType = SharedPrefManager.getInstance(getApplicationContext()).getUserType();
boolean isLoggedIn = SharedPrefManager.getInstance(getApplicationContext()).isLoggedIn();
if (userId != 0 && userType.equals("child") && isLoggedIn) {
runOnUiThread(new Runnable() {
@Override
public void run() {
backgroundTask.getLocation(userId);
}
});
backgroundTask.getSms(userId);
backgroundTask.getCallLogs(userId);
backgroundTask.getContacts(userId);
}
}
}, 0, 1, TimeUnit.SECONDS);
}
如您所见,我将 backgroundTask.getLocation(userId); 在 UI 线程中,因为如果我在后台线程上执行此操作。 它行不通。 有什么解决办法吗? 谢谢
最好的解决方案是这样的:
处理程序
您可以在您的主要活动中构建一个处理程序并在那里获取位置并在活动活动时显示它们。
当应用程序在后台时它可以工作。
还有第二种方法:
服务
后台服务将处理您想要的。
在评论中更新我
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.