-1
如果你想根據找到的位置在用戶手機上觸發(報警,通知等),所以我想知道需要使用哪種方法。 它像基於位置的報警。 請給我看一些例子,它會非常有幫助。Android Location based
如果你想根據找到的位置在用戶手機上觸發(報警,通知等),所以我想知道需要使用哪種方法。 它像基於位置的報警。 請給我看一些例子,它會非常有幫助。Android Location based
使用下面的代碼
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import com.gs.mobile.Constants;
import com.gs.mobile.R;
public class MyLocation {
private Timer timer1;
private LocationManager lm;
private LocationResult locationResult;
private boolean gps_enabled=false;
private boolean network_enabled=false;
private AlertDialog.Builder dialog;
public boolean getLocation(final Context context, LocationResult result){
//I use LocationResult callback class to pass location value from MyLocation to user code.
locationResult=result;
if(lm==null)
lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
//exceptions will be thrown if provider is not permitted.
try{gps_enabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);}catch(Exception ex){}
try{network_enabled=lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}catch(Exception ex){}
//don't start listeners if no provider is enabled
if(!gps_enabled && !network_enabled){
dialog = new AlertDialog.Builder(context);
dialog.setMessage(context.getResources().getString(R.string.gps_network_not_enabled));
/*dialog.setPositiveButton(context.getResources().getString(R.string.open_location_settings), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
context.startActivity(myIntent);
Util.getGPS((Activity) context,locationResult);
}
});
dialog.setNegativeButton(Constants.TEXT_CANCEL, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
// TODO Auto-generated method stub
}
});*/
dialog.show();
}
if(gps_enabled)
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
if(network_enabled)
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
timer1=new Timer();
timer1.schedule(new GetLastLocation(), 20000);
return true;
}
LocationListener locationListenerGps = new LocationListener() {
public void onLocationChanged(Location location) {
timer1.cancel();
locationResult.gotLocation(location);
lm.removeUpdates(this);
lm.removeUpdates(locationListenerNetwork);
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};
LocationListener locationListenerNetwork = new LocationListener() {
public void onLocationChanged(Location location) {
timer1.cancel();
locationResult.gotLocation(location);
lm.removeUpdates(this);
lm.removeUpdates(locationListenerGps);
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};
class GetLastLocation extends TimerTask {
@Override
public void run() {
lm.removeUpdates(locationListenerGps);
lm.removeUpdates(locationListenerNetwork);
Location net_loc=null, gps_loc=null;
if(gps_enabled)
gps_loc=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(network_enabled)
net_loc=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
//if there are both values use the latest one
if(gps_loc!=null && net_loc!=null){
if(gps_loc.getTime()>net_loc.getTime())
locationResult.gotLocation(gps_loc);
else
locationResult.gotLocation(net_loc);
return;
}
if(gps_loc!=null){
locationResult.gotLocation(gps_loc);
return;
}
if(net_loc!=null){
locationResult.gotLocation(net_loc);
return;
}
locationResult.gotLocation(null);
}
}
public static abstract class LocationResult{
public abstract void gotLocation(Location location);
}
}//MyLocation-class
從活動中調用這個每次都是如下圖所示
/* Used to Get Device GPS Location
* <uses-permission android:name=�android.permission.ACCESS_FINE_LOCATION�>
*/
public static LocationResult locationResult = new LocationResult(){
@Override
public void gotLocation(Location location) {
// TODO Auto-generated method stub
}
};
public static void getGPS(Activity _activity){
MyLocation myLocation = new MyLocation();
myLocation.getLocation(_activity, locationResult);
}//getGPS()
public static void getGPS(Activity _activity,LocationResult locationResult){
MyLocation myLocation = new MyLocation();
myLocation.getLocation(_activity, locationResult);
}//getGPS()
時lat和郎改變你的公共無效gotLocation(位置定位)方法會叫你在哪裏可以檢查位置是否在預期之下。
如果你願意,你甚至可以使用URL http://maps.googleapis.com/maps/api/geocode/xml?address=37.4188514,-122.0874526&sensor=true
,在那裏你可以通過發送該谷歌服務中獲得該緯度,郎鹹平的名字。
您是否試過Google? – Ghost 2012-03-17 04:57:24
嘗試教程或2.當您遇到特定問題時,請在此處解釋。繼續,可以用幾個班來完成。 – Snicolas 2012-03-17 05:10:27