结果展示
MainActivity
package com. example. locationmanager ;
import androidx. annotation. NonNull ;
import androidx. appcompat. app. AppCompatActivity ;
import android. annotation. SuppressLint ;
import android. content. Context ;
import android. location. Location ;
import android. location. LocationListener ;
import android. location. LocationManager ;
import android. os. Bundle ;
import android. view. View ;
import android. widget. Button ;
import kotlinx. coroutines. test. TestCoroutineContextKt ;
public class MainActivity extends AppCompatActivity {
private Button locationButton = null ;
@Override
protected void onCreate ( Bundle savedInstanceState) {
super . onCreate ( savedInstanceState) ;
setContentView ( R . layout. activity_main) ;
locationButton = ( Button ) findViewById ( R . id. locationButtonId) ;
locationButton. setOnClickListener ( new ButtonListener ( ) ) ;
}
private class ButtonListener implements View. OnClickListener {
@SuppressLint ( "MissingPermission" )
@Override
public void onClick ( View v) {
LocationManager locationManager = ( LocationManager ) MainActivity . this . getSystemService ( Context . LOCATION_SERVICE) ;
locationManager. requestLocationUpdates ( LocationManager . GPS_PROVIDER, 0 , 0 , new TestLocationListener ( ) ) ;
}
}
private class TestLocationListener implements LocationListener {
@Override
public void onLocationChanged ( Location location) {
System . out. println ( location. getLongitude ( ) ) ;
System . out. println ( location. getLatitude ( ) ) ;
}
@Override
public void onFlushComplete ( int requestCode) {
LocationListener . super . onFlushComplete ( requestCode) ;
}
@Override
public void onStatusChanged ( String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled ( @NonNull String provider) {
LocationListener . super . onProviderEnabled ( provider) ;
}
@Override
public void onProviderDisabled ( @NonNull String provider) {
LocationListener . super . onProviderDisabled ( provider) ;
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
< manifest xmlns: android= " http://schemas.android.com/apk/res/android"
xmlns: tools= " http://schemas.android.com/tools"
package = " com.example.locationmanager" >
< application
android: allowBackup= " true"
android: dataExtractionRules= " @xml/data_extraction_rules"
android: fullBackupContent= " @xml/backup_rules"
android: icon= " @mipmap/ic_launcher"
android: label= " @string/app_name"
android: roundIcon= " @mipmap/ic_launcher_round"
android: supportsRtl= " true"
android: theme= " @style/Theme.LocationManager"
tools: targetApi= " 31" >
< activity
android: name= " .MainActivity"
android: exported= " true" >
< intent-filter>
< action android: name= " android.intent.action.MAIN" />
< category android: name= " android.intent.category.LAUNCHER" />
</ intent-filter>
</ activity>
</ application>
< uses-permission android: name= " android.permission.ACCESS_FINE_LOCATION" />
</ manifest>
activity_main.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= " 绑定监听器"
tools: ignore= " MissingConstraints" />
</ androidx.constraintlayout.widget.ConstraintLayout>