我有一个带有 ID 的 lat 和 long 数据库,并已使用 json 检索到该数据库,并希望将邻近警报应用于数组中的地理位置。我不确定数组是否被正确传递,或者接近 alert.java 是否被正确调用
这是数据库检索
public class retrieveDB extends AsyncTask<Void, Integer, Void> {
@Override
protected Void doInBackground(Void... params) {
String result = "";
InputStream is = null;
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://cs1.ucc.ie/~am32/getDB.php");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}
catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
is.close();
result=sb.toString();
Log.i("json string", result);
}
catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
//parse json data
try{
JSONArray jArray = new JSONArray(result);
JSONObject json_data = null;
System.out.println("Length"+ jArray.length());
Log.d("DB","Length"+jArray.length());
for(int i=0; i<jArray.length(); i++){
json_data = jArray.getJSONObject(i);
int id = json_data.getInt("ID") ;
//String title = json_data.getString("Title");
double latitude = json_data.getDouble("Lat");
double longitude = json_data.getDouble("Lon");
//Adds proximity to POI's
ProxAlert inst = new ProxAlert();
inst.addProximityAlert(latitude,longitude, id);
//
inst.saveCoordinatesInPreferences((float)latitude, (float)longitude);
//prints to logCat
System.out.println(id+"&"+latitude+"&"+longitude);
}
}
catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
Log.e("log_tag","Failed data as:\n"+result);
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
// invoked on UI thread publishProgress(Progress...).
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(Void result) {
// result of background comp is passed to this step its invoked on the ui thread
super.onPostExecute(result);
}
}
和我的接近警报课
public class ProxAlert extends Activity {
private static final long POINT_RADIUS = 10;
private static final long PROX_ALERT_EXPIRATION = -1;
private static final String POINT_LATITUDE_KEY = "POINT_LATITUDE_KEY";
private static final String POINT_LONGITUDE_KEY = "POINT_LONGITUDE_KEY";
private LocationManager locationManager;
private static final String PROX_ALERT_INTENT= "com.example.try0.ProximityIntentReceiver";
void addProximityAlert (double latitude, double longitude, int id){
Bundle extras = new Bundle();
extras.putInt("title", id);
extras.putDouble("lat", latitude);
extras.putDouble("lon", longitude);
Intent intent = new Intent(PROX_ALERT_INTENT + id);
//intent.putExtra("alert", "it works");
intent.putExtra(PROX_ALERT_INTENT, extras);
PendingIntent proximityIntent = PendingIntent.getBroadcast(this, id, intent, PendingIntent.FLAG_CANCEL_CURRENT);
//ProximityIntent will be used to generate an Intent to fire when entry to or exit from the alert region is detected
locationManager.addProximityAlert(latitude,longitude,POINT_RADIUS,PROX_ALERT_EXPIRATION,proximityIntent);
IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT + id);
registerReceiver(new ProximityIntentReciever(), filter);
}
void saveCoordinatesInPreferences(float currentlatitude, float currentlongitude){
SharedPreferences prefs = this.getSharedPreferences(getClass().getSimpleName(), Context.MODE_PRIVATE);
SharedPreferences.Editor prefsEditor = prefs.edit();
prefsEditor.putFloat(POINT_LATITUDE_KEY, currentlatitude);
prefsEditor.putFloat(POINT_LONGITUDE_KEY, currentlongitude);
prefsEditor.commit();
}
Location retrievelocationFromPreferences(){
SharedPreferences prefs = this.getSharedPreferences(getClass().getSimpleName(), Context.MODE_PRIVATE);
Location location = new Location("POINT_LOCATION");
location.setLatitude(prefs.getFloat(POINT_LATITUDE_KEY, 0));
location.setLongitude(prefs.getFloat(POINT_LONGITUDE_KEY, 0));
return location;
}
}
我真的很感谢一些帮助它一直盯着和困惑