Java android使用位置->;与x27之间的结果不同;真手机';和';模拟器';
这是我在android中使用位置教程的全部代码。\n 我有个问题。我的模拟器产生了我的想法。\n 但是,我的真实手机(华为nova lite2)上没有显示任何内容。\n \n 第二,为什么在0拒绝权限的情况下调用ondenied?我看到吐司既“批准:1”又“拒绝:0”。 我错过了什么Java android使用位置->;与x27之间的结果不同;真手机';和';模拟器';,java,android,android-studio,location,Java,Android,Android Studio,Location,这是我在android中使用位置教程的全部代码。\n 我有个问题。我的模拟器产生了我的想法。\n 但是,我的真实手机(华为nova lite2)上没有显示任何内容。\n \n 第二,为什么在0拒绝权限的情况下调用ondenied?我看到吐司既“批准:1”又“拒绝:0”。 我错过了什么 package org.techtown.location; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompat
package org.techtown.location;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.pedro.library.AutoPermissions;
import com.pedro.library.AutoPermissionsListener;
public class MainActivity extends AppCompatActivity implements AutoPermissionsListener {
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startLocationService();
}
});
AutoPermissions.Companion.loadAllPermissions(this, 101);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
AutoPermissions.Companion.parsePermissions(this, requestCode, permissions, this);
}
@Override
public void onDenied(int i, String[] strings) {
Toast.makeText(getApplicationContext(), "permission denied: "+strings.length, Toast.LENGTH_SHORT).show();
}
@Override
public void onGranted(int i, String[] strings) {
Toast.makeText(getApplicationContext(), "permission granted: "+strings.length, Toast.LENGTH_SHORT).show();
}
public void startLocationService() {
LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
try {
Location location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
String message = "last known location->\nLatitude: " + latitude + "\nLongitude: " + longitude;
textView.setText(message);
}
GPSListener gpsListener = new GPSListener();
long minTime = 10000;
float minDistance = 0;
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime, minDistance, gpsListener);
Toast.makeText(getApplicationContext(), "requested my location", Toast.LENGTH_SHORT).show();
} catch (SecurityException e) {
e.printStackTrace();
}
}
class GPSListener implements LocationListener {
@Override
public void onLocationChanged(@NonNull Location location) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
String message = "last known location->\nLatitude: " + latitude + "\nLongitude: " + longitude;
textView.setText(message);
}
@Override
public void onProviderEnabled(@NonNull String provider) {
}
@Override
public void onProviderDisabled(@NonNull String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
}