所以我有一个具有 CustomAppActivity , LocationService 和 HttpURLCon 的Android应用,该应用具有 sent(String s)发出获取请求的方法。
我要弄清楚的是为什么我不能从 onLocationChanged(位置位置) 发送get请求(但可以通过Toast显示位置信息)。 我可以从CustomAppActivity的 onCreate(Bundle bundle)和LocationService的 onStartCommand(Intent intent,int flags,int startId)发出请求。
这是所有3类的代码(我将跳过导入):
CustomAppActivity :
public class CustomAppActivity extends org.qtproject.qt5.android.bindings.QtActivity {
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
Intent intent = new Intent(this, LocationService.class);
HttpURLCon.send("CustomAppActivity::startService");
startService(intent);
QOneSignalBinding.onCreate(this, bundle);
}
}
LocationService :
public class LocationService extends QtService
{
private LocationManager _locationManager = null;
private static final String TAG = "LocationService";
private static final int LOCATION_INTERVAL = 1000;//1800000
private static final float LOCATION_DISTANCE = 0;//100
private class LocationListener implements android.location.LocationListener
{
Location _lastLocation;
public LocationListener(String provider)
{
Log.d(TAG, "LocationListener " + provider);
_lastLocation = new Location(provider);
}
@Override
public void onLocationChanged(Location location)
{
Log.d(TAG, "onLocationChanged: " + location);
Context context = getApplicationContext();
Toast.makeText(context, "location" + location, Toast.LENGTH_LONG).show();
HttpURLCon.send("location" + location);
_lastLocation.set(location);
}
@Override
public void onProviderDisabled(String provider)
{
Log.d(TAG, "onProviderDisabled: " + provider);
}
@Override
public void onProviderEnabled(String provider)
{
Log.d(TAG, "onProviderEnabled: " + provider);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
Log.d(TAG, "onStatusChanged: " + provider);
}
}
LocationListener[] _locationListeners = new LocationListener[] {
new LocationListener(LocationManager.GPS_PROVIDER),
new LocationListener(LocationManager.NETWORK_PROVIDER)
};
@Override
public IBinder onBind(Intent arg0)
{
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand");
HttpURLCon.send("LocationService::onStartCommand");
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
@Override
public void onCreate() {
initializeLocationManager();
try {
_locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE, _locationListeners[1]);
} catch (java.lang.SecurityException ex) {
Log.e(TAG, "fail to request location update, ignore", ex);
} catch (IllegalArgumentException ex) {
Log.e(TAG, "network provider does not exist, " + ex.getMessage());
}
try {
_locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE, _locationListeners[0]);
} catch (java.lang.SecurityException ex) {
Log.e(TAG, "fail to request location update, ignore", ex);
} catch (IllegalArgumentException ex) {
Log.e(TAG, "gps provider does not exist " + ex.getMessage());
}
}
@Override
public void onDestroy() {
Log.d(TAG, "onDestroy");
super.onDestroy();
if (_locationManager != null) {
for (int i = 0; i < _locationListeners.length; i++) {
try {
_locationManager.removeUpdates(_locationListeners[i]);
} catch (Exception ex) {
Log.e(TAG, "fail to remove location listners, ignore", ex);
}
}
}
}
private void initializeLocationManager() {
Log.d(TAG, "initializeLocationManager");
if (_locationManager == null) {
_locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
}
}
}
HttpURLCon :
public class HttpURLCon {
private static final String TAG = "HttpURLCon";
static void send(String s) {
Log.d(TAG, "Sending HTTP GET request");
new AsyncSend(s).execute();
}
private static class AsyncSend extends AsyncTask {
String data;
public AsyncSend(String data) {
this.data = data;
}
@Override
protected Object doInBackground(Object[] objects) {
try {
String url = "SERVER URL" + data;
HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
int responseCode = con.getResponseCode();
Log.d(TAG, "Response Code : " + responseCode);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
}
为什么HttpURLCon.send(“ location” + location)不起作用?