结果展示
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 {
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) {
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 ( ) ;
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>