安卓---获取位置信息

本程序演示获取位置服务信息,并把这些显示到主页面的一个TextView上。
java代码:
public class MainActivity extends AppCompatActivity {

    LocationManager LM;
    TextView show;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        show = (TextView) findViewById(R.id.show);
        //获取LocationManger对象
        //判断哪个资源可以使用就使用哪个资源
        LM = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        List<String> list=new ArrayList<String>();
        list=LM.getAllProviders();
        for(int i=0;i<list.size();i++){
            System.out.println("可使用的资源"+i+list.get(i));
            Location location=LM.getLastKnownLocation(list.get(i));
            if(location!=null){
                update(location);
                return ;

            }else{

                System.out.println(list.get(i)+"为空");
            }

        }






        //设置每隔3秒读取一次GPS信息
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        LM.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 8, new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                //当GPS信息发生变更的时候执行
                update(location);
            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {

            }

            @Override
            public void onProviderEnabled(String provider) {

                if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return;
                }

                //当GPS provider可以使用的时候更新位置
                update(LM.getLastKnownLocation(provider));
            }

            @Override
            public void onProviderDisabled(String provider) {
                update(null);
            }
        });
    }

    private void update(Location location) {
        if(location!=null){
            StringBuilder strbl=new StringBuilder();
            strbl.append("实时位置信息:\n");
            strbl.append("经度;    ");
            strbl.append(location.getLongitude());
            strbl.append("\n纬度:    ");
            strbl.append(location.getLatitude());
            strbl.append("\n高度:    ");
            strbl.append(location.getAltitude());
            strbl.append("\n速度:    ");
            strbl.append(location.getSpeed());
            strbl.append("\n方向:    ");
            strbl.append(location.getBearing());
            show.setText(strbl.toString());


        }else{

            show.setText("传入对象为空");
        }
    }


}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="maptest.zyf.com.getlocation.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/show"
        />
</RelativeLayout>

配置权限:


  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

问题探讨:

在写程序的过程中,发现需要对权限进行判断,拥有权限才能继续,但是这个判断是自动加载的,就像平时发现问的时候一样,按alt+enter就可以自动加载了。同时,在写程序的时候,需要对当前可以使用的provider进行判断,可以使用再提交处理!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值