美文网首页
Android安卓开发 获取手机位置locationManage

Android安卓开发 获取手机位置locationManage

作者: 安徒生的童话慢慢融化 | 来源:发表于2018-02-28 20:19 被阅读520次
    package com.czh.locationtest;
    
    import android.content.Context;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import java.util.List;
    
    public class MainActivity extends AppCompatActivity {
        private TextView positionTextView;
        private LocationManager locationManager;
        private String provider;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            positionTextView = (TextView) findViewById(R.id.position_text_view);
            locationManager = (LocationManager) getSystemService(Context.
                    LOCATION_SERVICE);
            List<String> providerList = locationManager.getProviders(true);
            if (providerList.contains(LocationManager.GPS_PROVIDER)) {
                provider = LocationManager.GPS_PROVIDER;
            }else if (providerList.contains(LocationManager.NETWORK_PROVIDER)) {
                provider = LocationManager.NETWORK_PROVIDER;
            } else {
                Toast.makeText(this, "No location provider to use",
                        Toast.LENGTH_SHORT).show();
                return;
            }
            Location location = locationManager.getLastKnownLocation(provider);
            if (location != null) {
                showLocation(location);
            }
            locationManager.requestLocationUpdates(provider, 5000, 1,
                    locationListener);
        }
    
        LocationListener locationListener = 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(Location location) {
    // 更新当前设备的位置信息
                showLocation(location);
            }
        };
    
        protected void onDestroy() {
            super.onDestroy();
            if (locationManager != null) {
    // 关闭程序时将监听器移除
                locationManager.removeUpdates(locationListener);
            }
        }
    
        private void showLocation(Location location) {
            String currentPosition = "latitude is " + location.getLatitude() + "\n"
                    + "longitude is " + location.getLongitude();
            positionTextView.setText(currentPosition);
        }
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
    
            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }
    
            return super.onOptionsItemSelected(item);
        }
    
    }
    
    
    

    相关文章

      网友评论

          本文标题:Android安卓开发 获取手机位置locationManage

          本文链接:https://www.haomeiwen.com/subject/uagixftx.html