微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

应用程序在模拟器中运行时的行为与在 android studio 中的真实设备上测试时的行为不同

如何解决应用程序在模拟器中运行时的行为与在 android studio 中的真实设备上测试时的行为不同

案例 - 我制作了一个基于位置的应用程序,该应用程序一打开就会显示用户的位置,并在一个应用程序中显示纬度、经度、高度、精度和国家/地区名称。文本视图。

问题 - 我的应用没有编译错误,并且在安卓模拟器上运行成功。 strong> 当给定虚拟位置时,它会显示所有上述位置的详细信息但是在我自己的手机上测试时它没有不在 textview 中显示任何内容 如何解决这个问题?

代码:- 主文件

public class MainActivity2 extends AppCompatActivity {

LocationManager locationManager;
LocationListener locationListener;

TextView AddresstextView,AccuracyTextView,LatitudeTextView,LongitudeTextView,AltitudeTextView;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    getSupportActionBar().hide();
    getwindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);


    locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

    locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            updateLocationInfo(location);
        }

        @Override
        public void onStatusChanged(String provider,int status,Bundle extras) {

        }

        @Override
        public void onProviderdisabled(@NonNull String provider) {

        }

        @Override
        public void onProviderEnabled(@NonNull String provider) {

        }
    };

    /** Code to ask  the user for location */
    if(Build.VERSION.SDK_INT < 23){
        startListening();
    }else{
        if (ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},1);
        } else {
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,locationListener);
            Location location = locationManager.getLastKNownLocation(LocationManager.GPS_PROVIDER);
            if (location != null) {
                updateLocationInfo(location);
            }
        }
    }

}// End OnCreate

/** To handle response of user i,e whether they said yes/no to request location  */
@Override
public void onRequestPermissionsResult(int requestCode,String[] permissions,int[] grantResults) {
    super.onRequestPermissionsResult(requestCode,permissions,grantResults);
    if(grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
        startListening();

    }
}//End of OnReqFunc

public void startListening (){
    if(ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
        locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    }
}
public  void updateLocationInfo(Location location){
    LatitudeTextView = findViewById(R.id.latitudeTextView);
    LongitudeTextView = findViewById(R.id.longitudeTextView);
    AccuracyTextView = findViewById(R.id.accuracyTextView);
    AltitudeTextView = findViewById(R.id.altitudeTextView);
    AddresstextView = findViewById(R.id.addresstextView);

    LatitudeTextView.setText("Latitude : " + location.getLatitude());
    LongitudeTextView.setText("Longitude : " + location.getLatitude());
    AccuracyTextView.setText("Accuracy : " + location.getAccuracy());
    AltitudeTextView.setText("Altitude : " + location.getAltitude());

    Geocoder geocoder = new Geocoder(getApplicationContext(),Locale.getDefault());

    try {
        String address = "Could not find address :(";

        List<Address> addressList = null;
        addressList = geocoder.getFromLocation(location.getLatitude(),location.getLongitude(),1);

    if(addressList != null && addressList.size() >0){
            address ="Address:\n";
            if(addressList.get(0).getThoroughfare() != null){
                address += addressList.get(0).getThoroughfare() + "\n";
            }
            if(addressList.get(0).getLocality() != null){
                  address += addressList.get(0).getLocality() + " ";
             }
             if(addressList.get(0).getPostalCode() != null){
                 address += addressList.get(0).getPostalCode() + " ";
             }
            if(addressList.get(0).getCountryName() != null){
              address += addressList.get(0).getCountryName() + "\n";
             }
        }
        AddresstextView.setText(address);
    } catch (IOException e) {
        e.printstacktrace();
    }




}

}//结束主

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。