我想要检索设备的经度和纬度,然后使用我获得的数据编辑textview。 这是我离开的地方,因为我不知道该怎么做:
public class Login_activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_activity);
final TextView locationText = (TextView) findViewById(R.id.textView);
final Button locationRetrieveButton = (Button) findViewById(R.id.button);
final Location locationConst = new Location(LocationManager.GPS_PROVIDER);
final double locationX = locationConst.getLatitude();
final double locationY = locationConst.getLongitude();
final View.OnTouchListener retrieveButtonClicked = new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
locationText.setText("FirstX:"+locationX+"\nFirstY="+locationY+"\nSecondX=\nSecondY=\n"+locationConst.getProvider());
return false;
}
};
locationRetrieveButton.setOnTouchListener(retrieveButtonClicked);
}
当我点击“locationRetrieveButton”时,locationX和locationY变量只返回0.0和0.0,GPS_PROVIDER为“gps”。 我已经在android清单中包含了所有需要的权限(粗略的位置和精确的位置以及互联网权限)。 我错过了什么?
答案 0 :(得分:0)
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="com.projectname.project.permission.MAPS_RECEIVE" /> <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
在OnCreate方法中;
locationRetrieveButton.setOnTouchListener(retrieveButtonClicked);
Location location = null;
LocationManager locationManager=null;
final View.OnTouchListener retrieveButtonClicked = new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
try {
locationManager = (LocationManager) getContext()
.getSystemService(Context.LOCATION_SERVICE);
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (isGPSEnabled || isNetworkEnabled) { // Internet Or Gps Control
if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
} //Check Permission
if (isGPSEnabled) { //Check Gps Enabled
if (location == null) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000 , 10,
new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onLocationChanged(final Location location) {
}
});
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
}}
//Check Network Enabled
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
2000,
10, new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onLocationChanged(final Location location) {
}
});
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
}
}
else {
AlertDialog alertMessage = new AlertDialog.Builder(getActivity()).create();
alertMessage.setTitle("Message");
alertMessage.setMessage("Please Open Internet Or Gps");
alertMessage.show();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
});