我正在学习使用 android 开发并尝试它,我想制作一个“接近警报”。当我接近某个点时,我需要敲响警报。
首先我做了一个循环来计算点和我们位置之间的距离,当它小于一个距离时,响警报。
没关系,但是...从逻辑上讲,当您发出警报时,您可以播放其他应用程序,所以我的发生了什么?
我需要使用 onPaused() 方法吗?也许创建一个线程?我迷失在这一刻,有人知道该怎么做吗?o 给我一些建议?
谢谢 =)
这是我的代码......现在使用 asyncTask,显然不起作用,一些帮助?
公共类 RunActivity 扩展 MapActivity {
private MapView map;
private MapController mapControl;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_run);
this.map = (MapView)findViewById(R.id.map);
this.map.setBuiltInZoomControls(true);
this.mapControl = map.getController();
GeoPoint centro = new GeoPoint((int)(40.463667 * 1E6), (int)(-3.749220 * 1E6));
this.mapControl.setCenter(centro);
this.mapControl.setZoom(10);
List<Overlay> capas = this.map.getOverlays();
MyLocationOverlay mOverlayLocation = new MyLocationOverlay(this, this.map);
mOverlayLocation.enableMyLocation();
mOverlayLocation.enableCompass();
capas.add(mOverlayLocation);
this.map.postInvalidate();
MiTareaAsincrona Ejemplo = new MiTareaAsincrona();
Ejemplo.execute(RunActivity.this);
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
class MiTareaAsincrona extends AsyncTask<Context, Double, Boolean> {
private boolean finish = false;
private double target_lat;
private double target_lon;
protected void onPreExecute() {
Bundle Datos = getIntent().getBundleExtra("Datos");
this.target_lat = Datos.getInt("LATITUD");
this.target_lon = Datos.getInt("LONGITUD");
}
protected Boolean doInBackground(Context... context) {
LocationManager manager = (LocationManager)context[0].getSystemService(Context.LOCATION_SERVICE);
LocationListener listener = new LocationListener() {
double actual_lat;
double actual_lon;
Bundle Datos = getIntent().getBundleExtra("Datos");
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onLocationChanged(Location location) {
actual_lat = location.getLatitude()*1E6;
actual_lon = location.getLongitude()*1E6;
if (distance(actual_lat, actual_lon, target_lat, target_lon) < Datos.getInt("DISTANCIA")) {
finish = true;
}
}
};
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
return finish;
}
protected void onProgressUpdate() { }
protected void onPostExecute(Boolean result) {
if (finish) {
Log.v("OJU", "MIARMA");
}
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*:: This function converts decimal degrees to radians :*/
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
private double distance(double lat1, double lon1, double lat2, double lon2) {
double theta = lon1 - lon2;
double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
dist = Math.acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515 * 1.609344 * 1000; // en metros
return (dist);
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*:: This function converts decimal degrees to radians :*/
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
private double deg2rad(double deg) {
return (deg * Math.PI / 180.0);
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*:: This function converts radians to decimal degrees :*/
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
private double rad2deg(double rad) {
return (rad * 180.0 / Math.PI);
}
}
}