Android GPS基础
程序员文章站
2022-06-29 14:31:50
...
package org.example.locationtest; import java.util.List; import android.app.Activity; import android.location.Criteria; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.location.LocationProvider; import android.os.Bundle; import android.widget.TextView; public class LocationTest extends Activity implements LocationListener { // Define human readable names private static final String[] A = { "invalid", "n/a", "fine", "coarse" }; private static final String[] P = { "invalid", "n/a", "low", "medium", "high" }; private static final String[] S = { "out of service", "temporarily unavailable", "available" }; private LocationManager mgr; private TextView output; private String best; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mgr = (LocationManager) getSystemService(LOCATION_SERVICE); output = (TextView) findViewById(R.id.output); log("Location providers:"); dumpProviders(); Criteria criteria = new Criteria(); best = mgr.getBestProvider(criteria, true);//获取最好的提供者 log("\nBest provider is: " + best); // 注:这里取到的GPS值是目前存储在GPS上的Location值, // 如果刚刚打开GPS就用这个方法取Location取到的会是上一次代开GPS时取到的旧值。 // 等待GPS取得新值需要用LocationListener里的 onLocationChanged。 log("\nLocations (starting with last known):"); Location location = mgr.getLastKnownLocation(best); //通过最好的提供者获得GPS的方法 dumpLocation(location); } @Override protected void onResume() { super.onResume(); // Start updates (doc recommends delay >= 60000 ms) mgr.requestLocationUpdates(best, 15000, 1, this);//通过best启动(例如设置成LocationManager.GPS_PROVIDER就是通过gps启动),Location更新的设置是每隔15000ms更新一次,最后一个参数是LocationListener的一个引用,这个类需要实现 } @Override protected void onPause() { super.onPause(); // 停止GPS运行: mgr.removeUpdates(this); } //如果需要去到GPS启动后取到的Location,必须用这个! public void onLocationChanged(Location location) { dumpLocation(location); } // Provider被disable时触发此函数,比如GPS被关闭 public void onProviderDisabled(String provider) { log("\nProvider disabled: " + provider); } // Provider被enable时触发此函数,比如GPS被打开 public void onProviderEnabled(String provider) { log("\nProvider enabled: " + provider); } // Provider的转态在可用、暂时不可用和无服务三个状态直接切换时触发此函数 public void onStatusChanged(String provider, int status, Bundle extras) { log("\nProvider status changed: " + provider + ", status=" + S[status] + ", extras=" + extras); } /** Write a string to the output window */ private void log(String string) { output.append(string + "\n"); } /** Write information from all location providers */ private void dumpProviders() { List<String> providers = mgr.getAllProviders(); for (String provider : providers) { dumpProvider(provider); } } /** Write information from a single location provider */ private void dumpProvider(String provider) { LocationProvider info = mgr.getProvider(provider); StringBuilder builder = new StringBuilder(); builder.append("LocationProvider[") .append("name=") .append(info.getName()) .append(",enabled=") .append(mgr.isProviderEnabled(provider))//检测GPS是否已经启动: .append(",getAccuracy=") .append(A[info.getAccuracy() + 1]) .append(",getPowerRequirement=") .append(P[info.getPowerRequirement() + 1]) .append(",hasMonetaryCost=") .append(info.hasMonetaryCost()) .append(",requiresCell=") .append(info.requiresCell()) .append(",requiresNetwork=") .append(info.requiresNetwork()) .append(",requiresSatellite=") .append(info.requiresSatellite()) .append(",supportsAltitude=") .append(info.supportsAltitude()) .append(",supportsBearing=") .append(info.supportsBearing()) .append(",supportsSpeed=") .append(info.supportsSpeed()) .append("]"); log(builder.toString()); } /** Describe the given location, which might be null */ private void dumpLocation(Location location) { if (location == null) log("\nLocation[unknown]"); else // double latitude = location.getLatitude(); //经度 // double longitude = location.getLongitude(); //纬度 // double altitude = location.getAltitude(); //海拔 log("\n" + location.toString()); } }