GPS跟踪器android线程
我试图让一个线程能够不断地向我的数据库发送GPS坐标。 这是我的代码:GPS跟踪器android线程,android,android-volley,android-gps,Android,Android Volley,Android Gps,我试图让一个线程能够不断地向我的数据库发送GPS坐标。 这是我的代码: public class trackThread extends Thread { Context mContext; public trackThread(Context mContext) { this.mContext = mContext; } @SuppressLint("MissingPermission") public void run() {
public class trackThread extends Thread {
Context mContext;
public trackThread(Context mContext) {
this.mContext = mContext;
}
@SuppressLint("MissingPermission")
public void run() {
LocationManager locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
trackThread.MyCurrentLoctionListener locationListener = new trackThread.MyCurrentLoctionListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
public class MyCurrentLoctionListener implements android.location.LocationListener {
@Override
public void onLocationChanged(Location location) {
final String myLocation = "Latitude = " + location.getLatitude() + " Longitude = " + location.getLongitude();
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(mContext);
String url ="http://example.org/send.php?lat=" + location.getLatitude() + "&long=" + location.getLongitude();
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
Log.e("Response is: "+ response.toString(), myLocation);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("That didn't work!", myLocation);
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
Log.e("La risposta è ", stringRequest.toString());
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
}
}
此方法通过单击简单常见活动的按钮触发
我认为这个问题是由于错误地使用了上下文造成的(对不起,我不太擅长Android编码)。
注意:我还尝试在
locationManager.requestLocationUpdates()下面添加一个简单的Log.e来调试并尝试理解问题。它已显示给我它工作不正常。出了什么问题?你是对的。问题是调用和调用了onLocationChanged方法,但它无法正常工作。您在哪里创建和启动该文本?在哪种情况下?您仍然没有说出哪些内容不正常工作。抱歉,我已对其进行了编辑。告诉我是否清楚。不过,谢谢你
public void inviaGps(View v) {
if ((ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) || ActivityCompat.checkSelfPermission(this, Manifest.permission.INTERNET) != 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.
Toast t = Toast.makeText(this, "Devi abilitare i permessi", Toast.LENGTH_SHORT);
t.show();
return;
}
trackThread p = new trackThread(getApplicationContext());
new Thread(p).start();
}