TextFields 不更新
TextFields not updating
android 开发新手,一直在尝试按照 android studio 中的 gps 定位应用程序教程进行操作,但由于某种原因,文本字段没有更新。
这是我一直关注的教程:-
[Android Studio Tutorial - Build a GPS App]1
................................................ ..................................................... ..................................................... ..................................................... ..................................................... .....................
这是代码:-
public class MainActivity extends AppCompatActivity {
TextView longi,lat;
Switch share;
// google play service api
FusedLocationProviderClient fusedLocationProviderClient;
// location request service
LocationRequest locationRequest;
private static final int sETINTERVAL=30;
private static final int fASTESTINTERVAL=5;
public static final int rEQUESTCODE=1;
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
longi = findViewById(R.id.longi);
lat = findViewById(R.id.lat);
share = findViewById(R.id.share);
locationRequest = LocationRequest.create();
locationRequest.setInterval(1000 * sETINTERVAL);
locationRequest.setFastestInterval(1000 * fASTESTINTERVAL);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
share.setOnClickListener(v -> {
if(share.isChecked()){
share.setText("ON");
updateGPS();
}
else{
share.setText("OFF");
}
});
}
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode){
case rEQUESTCODE:
if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
updateGPS();
}
else{
Toast.makeText(this, "Requires location permission", Toast.LENGTH_SHORT).show();
finish();
}
break;
default:
throw new IllegalStateException("Unexpected value: " + requestCode);
};
}
@RequiresApi(api = Build.VERSION_CODES.M)
private void updateGPS(){
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(MainActivity.this);
if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
fusedLocationProviderClient.getLastLocation().addOnSuccessListener(this, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
updateUI(location);
}
});
}
else{
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},rEQUESTCODE);
}
}
private void updateUI(Location location){
lat.setText(String.valueOf(location.getLatitude()));
longi.setText(String.valueOf(location.getLongitude()));
}
}
尝试使用此代码:
@RequiresApi(api = Build.VERSION_CODES.M)
private void updateGPS() {
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(MainActivity.this);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
fusedLocationProviderClient.getLastLocation().addOnSuccessListener(this, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
updateUI(location);
}
});
} else {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, rEQUESTCODE);
}
}
在onCreate代码中写入
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
gradle:
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
dependencies {
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation 'com.google.android.gms:play-services-location:19.0.1'
}
android 开发新手,一直在尝试按照 android studio 中的 gps 定位应用程序教程进行操作,但由于某种原因,文本字段没有更新。
这是我一直关注的教程:- [Android Studio Tutorial - Build a GPS App]1
................................................ ..................................................... ..................................................... ..................................................... ..................................................... ..................... 这是代码:-
public class MainActivity extends AppCompatActivity {
TextView longi,lat;
Switch share;
// google play service api
FusedLocationProviderClient fusedLocationProviderClient;
// location request service
LocationRequest locationRequest;
private static final int sETINTERVAL=30;
private static final int fASTESTINTERVAL=5;
public static final int rEQUESTCODE=1;
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
longi = findViewById(R.id.longi);
lat = findViewById(R.id.lat);
share = findViewById(R.id.share);
locationRequest = LocationRequest.create();
locationRequest.setInterval(1000 * sETINTERVAL);
locationRequest.setFastestInterval(1000 * fASTESTINTERVAL);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
share.setOnClickListener(v -> {
if(share.isChecked()){
share.setText("ON");
updateGPS();
}
else{
share.setText("OFF");
}
});
}
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode){
case rEQUESTCODE:
if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
updateGPS();
}
else{
Toast.makeText(this, "Requires location permission", Toast.LENGTH_SHORT).show();
finish();
}
break;
default:
throw new IllegalStateException("Unexpected value: " + requestCode);
};
}
@RequiresApi(api = Build.VERSION_CODES.M)
private void updateGPS(){
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(MainActivity.this);
if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
fusedLocationProviderClient.getLastLocation().addOnSuccessListener(this, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
updateUI(location);
}
});
}
else{
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},rEQUESTCODE);
}
}
private void updateUI(Location location){
lat.setText(String.valueOf(location.getLatitude()));
longi.setText(String.valueOf(location.getLongitude()));
}
}
尝试使用此代码:
@RequiresApi(api = Build.VERSION_CODES.M)
private void updateGPS() {
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(MainActivity.this);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
fusedLocationProviderClient.getLastLocation().addOnSuccessListener(this, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
updateUI(location);
}
});
} else {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, rEQUESTCODE);
}
}
在onCreate代码中写入
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
gradle:
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
dependencies {
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation 'com.google.android.gms:play-services-location:19.0.1'
}