android_mars老师_定位_获取最佳的provider

此代码示例展示了如何在Android应用中使用LocationManager的Criteria来获取设备的最佳位置提供者,考虑了精度和功耗要求。点击按钮会列出所有可用的providers并找出最佳provider。
摘要由CSDN通过智能技术生成

结果展示

结果展示

ManiActivity

package com.example.locationmanager2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.location.Criteria;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.util.Iterator;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    /*
    一般设备中的provider有三种,network、passive、gps
    获取最佳LocationProvider的方法是:Criteria.他可以设置一系列的查询条件,用于筛选合适的LocationProvider.
     */
    private Button locationButtonId = null;
    private Button bestProviderButtonId = null;
    private LocationManager locationManager = null;

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

        locationButtonId = (Button) findViewById(R.id.locationButtonId);
        bestProviderButtonId = (Button) findViewById(R.id.bestProviderButtonId);

        locationManager = (LocationManager) MainActivity.this.getSystemService(Context.LOCATION_SERVICE);
        locationButtonId.setOnClickListener(new locationButtonIdListener());
        bestProviderButtonId.setOnClickListener(new bestProviderButtonIdListener());
    }

    private class locationButtonIdListener implements View.OnClickListener{
        @Override
        public void onClick(View v){
            //获取所有providers-->network、passive、gps
            List<String> providers = locationManager.getAllProviders();
            for(Iterator iterator = providers.iterator();iterator.hasNext();){
                System.out.println("providers.iterator is ------->"+(String) iterator.next());
            }
        }
    }
    private class bestProviderButtonIdListener implements View.OnClickListener{
        @Override
        public void onClick(View v){
            Criteria criteria = new Criteria();
            //filter provider by conditions
            criteria.setAccuracy(Criteria.ACCURACY_FINE);
            criteria.setPowerRequirement(Criteria.POWER_LOW);
            criteria.setAltitudeRequired(false);
            criteria.setSpeedRequired(false);
            criteria.setCostAllowed(false);
            String provider = locationManager.getBestProvider(criteria,false);
            System.out.println("best provider is ------>"+provider);
        }
    }
}

activity.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/locationButtonId"
        android:text="测试当前设备的provider"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="MissingConstraints"
        android:layout_margin="10dp"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/bestProviderButtonId"
        android:text="最好的provider"
        app:layout_constraintTop_toBottomOf="@+id/locationButtonId"
        tools:ignore="MissingConstraints"
        android:layout_margin="10dp"/>

</androidx.constraintlayout.widget.ConstraintLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/locationButtonId"
        android:text="测试当前设备的provider"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="MissingConstraints"
        android:layout_margin="10dp"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/bestProviderButtonId"
        android:text="最好的provider"
        app:layout_constraintTop_toBottomOf="@+id/locationButtonId"
        tools:ignore="MissingConstraints"
        android:layout_margin="10dp"/>

</androidx.constraintlayout.widget.ConstraintLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值