ฉันได้ปฏิบัติตามบทช่วยสอนนี้แล้ว http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/ สำหรับการแสดงป๊อปอัปหากไม่ได้เปิดใช้งาน GPS และรับตำแหน่งปัจจุบันของผู้ใช้
และนี่คือเครื่องรับออกอากาศที่รับฟังสภาวะปิดการใช้งาน GPS
private BroadcastReceiver mGpsSwitchStateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().matches("android.location.PROVIDERS_CHANGED")) {
gps = new GPSTracker(getActivity());
if(gps.canGetLocation()){
Log.d("Your Location", "latitude:" + gps.getLatitude() + ", longitude: " + gps.getLongitude());
Toast.makeText(getActivity(), "GPS Switch", Toast.LENGTH_SHORT).show();
if(gps.getLatitude() != 0 && gps.getLongitude() != 0 ){
final CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(gps.getLatitude(),gps.getLongitude())) // Sets the center of the map to selected place
.zoom(15) // Sets the zoom
.build(); //
gMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
Toast.makeText(getActivity(), "GPS Switch Lat: "+gps.getLatitude()+" Long: "+gps.getLongitude(), Toast.LENGTH_SHORT).show();
}
} else {
if(!gps.isSettingDialogShown())
gps.showSettingsAlert();
}
}
}
};
เมื่อฉันเปิดใช้งาน GPS ผ่านทางกล่องโต้ตอบการตั้งค่าหรือจากเมนูแบบเลื่อนลง คลาส GPSTracker รับบริการระบุตำแหน่งและตรวจสอบตำแหน่งปัจจุบันของผู้ใช้ในส่วนของโค้ดนี้
public Location getLocation() {
try {
locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network Enabled");
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS", "GPS Enabled");
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (SecurityException e) {
e.printStackTrace();
}
return location;
}
ปัญหา:
ประการแรก ฉันไม่รู้ว่าทำไมเครื่องรับการออกอากาศ ได้รับการดำเนินการสองครั้ง และประการที่สองเมื่ออยู่ในโหมดปกติ ค่า lat lng ในเครื่องรับยังคงเป็นศูนย์ แต่ในการดีบั๊กบางครั้งจะส่งกลับค่าของตำแหน่งปัจจุบันของผู้ใช้
คุณช่วยได้ไหมว่าฉันทำอะไรผิด?
requestLocationUpdates()
โดยที่คุณจะได้รับตำแหน่งในLocationListener
ของคุณ ข้อมูลนี้ครอบคลุมอยู่ในคำตอบที่ฉันเชื่อมโยง ดูขั้นตอน #2 และ #3 - person CommonsWare   schedule 31.03.2017