簡體   English   中英

如何檢索設備的位置

[英]How to retrieve the location of a device

我想檢索設備的經度和緯度,並僅使用獲取的數據編輯文本視圖。這是我離開的地方,因為我不知道該怎么做:

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清單中包含了所有必需的權限(粗略的位置以及精確的位置和Internet權限)。 我想念什么?

  • 確保添加權限;
 <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(); } } }); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM