Java 位置管理器在android 4.4.4中不工作

Java 位置管理器在android 4.4.4中不工作,java,android,gps,location,Java,Android,Gps,Location,Android业余爱好者和第一次发布在这里。 我正在开发一个应用程序,获取用户的GPS位置,然后发送到Mysql数据库。在安卓7.1(三星note 5)上,它工作得很好,但当我尝试在安卓4.2.2(三星J1)上使用时,位置不会更新 在my MapsActivity.java下面 public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback { private GoogleMap mMa

Android业余爱好者和第一次发布在这里。 我正在开发一个应用程序,获取用户的GPS位置,然后发送到Mysql数据库。在安卓7.1(三星note 5)上,它工作得很好,但当我尝试在安卓4.2.2(三星J1)上使用时,位置不会更新

在my MapsActivity.java下面

public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    LocationManager locationManager;
    LocationListener locationListener;

    final int INTERVAL = 0; /* milliseconds */
    final int DISTANCE = 0; /* meters */

    //after asking the user for permission
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        // to check if the request code is the same as we used, in this case 1
        if (requestCode == 1) {
            //if true, then it means we have the permission
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, INTERVAL, DISTANCE, locationListener); //every 15 seconds or 50 meters
                }
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

    }

    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {
        //change the title on action bar to have the name of the Bus
        setTitle("Bus 1 - CMC");

        mMap = googleMap;


        //add Marker at VM Head Offices - as we waiting for the current location to be updated
        LatLng maputo = new LatLng(-25.9758904,32.5805966);
        mMap.addMarker(new MarkerOptions().position(maputo).title("Vodacom Head Office"));
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(maputo, 15));

        //get the device location
        locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {

                isNetworkAvailable(MapsActivity.this);

                //for better accuracy
                int suitableMeter = 10; // adjust your need
                if (location.hasAccuracy()  && location.getAccuracy() <= suitableMeter) {
                    // This is your most accurate location.
                    //Updated last updated location time text
                    String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
                    TextView textViewLocation = (TextView) findViewById(R.id.textViewLastUpdated);
                    textViewLocation.setText("Location last updated: " + currentDateTimeString);


                    LatLng busLocation = new LatLng(location.getLatitude(),location.getLongitude());

                    //TODO
                    //check if user has internet connection, if not then show dialog and ask to activate it

                    //send data to database
                    sendLocation(location.getLatitude(), location.getLongitude(), currentDateTimeString);

                    //clear the previous markers
                    mMap.clear();
                    mMap.addMarker(new MarkerOptions().position(busLocation).title("Current Location"));
                    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(busLocation, mMap.getCameraPosition().zoom));

                }
            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
                switch (status) {
                    case LocationProvider.AVAILABLE:
    //                        Toast.makeText(MapsActivity.this, "GPS is available.", Toast.LENGTH_SHORT).show();
                        break;
                    case LocationProvider.OUT_OF_SERVICE:
                        Toast.makeText(MapsActivity.this, "GPS is out of Service.", Toast.LENGTH_SHORT).show();
                        break;
                    case LocationProvider.TEMPORARILY_UNAVAILABLE:
                        Toast.makeText(MapsActivity.this, "GPS temporarily unavailable.", Toast.LENGTH_SHORT).show();
                        break;
                }
            }

            @Override
            public void onProviderEnabled(String provider) {
                Toast.makeText(MapsActivity.this, "Provider is enabled.", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onProviderDisabled(String provider) {
                //TODO show dialog or pop up to open
                Toast.makeText(MapsActivity.this, "Provider is disabled.", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(intent);
            }
        };

        //If device is running sdk<23 Marshmellow, no need to request permission
        if (Build.VERSION.SDK_INT < 23) {
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, INTERVAL, DISTANCE, locationListener);
        } else  {
            // if don't have permission. ask for permission
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                //ask for permission
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
            } else {
                // we have permission
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, INTERVAL, DISTANCE, locationListener);
            }
        }
    }


    // To add the action bar
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    //  Responding to Android Action Bar Events
    @Override
    public boolean onOptionsItemSelected(MenuItem item) { switch(item.getItemId()) {
        case R.id.editBusName:
            Toast.makeText(this, "To create page", Toast.LENGTH_LONG).show();
            return(true);
        case R.id.reportBug:
            Intent intent2 = new Intent(getApplicationContext(), ReportBug.class);
            startActivity(intent2);
            return(true);
        case R.id.aboutApp:
            Intent intent3 = new Intent(getApplicationContext(), AboutApp.class);
            startActivity(intent3);
            return(true);
        case R.id.adminOptions:
            Toast.makeText(this, "To create page", Toast.LENGTH_LONG).show();
            return(true);
    }
        return(super.onOptionsItemSelected(item));
    }
}
公共类MapsActivity扩展了AppCompatActivity在MapReadyCallback上的实现{
私有谷歌地图;
地点经理地点经理;
LocationListener LocationListener;
最终整数间隔=0;/*毫秒*/
最终整数距离=0;/*米*/
//在请求用户许可后
@凌驾
public void onRequestPermissionsResult(int-requestCode,@NonNull-String[]permissions,@NonNull-int[]grantResults){
super.onRequestPermissionsResult(请求代码、权限、授权结果);
//检查请求代码是否与我们使用的相同,在本例中为1
if(requestCode==1){
//如果是真的,那就意味着我们得到了许可
if(grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION\u已授予){
if(ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS\u FINE\u LOCATION)==PackageManager.permission\u已授予&&ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS\u LOCATION)!=PackageManager.permission\u已授予){
locationManager.RequestLocationUpdate(locationManager.GPS_提供程序、间隔、距离、locationListener);//每15秒或50米
}
}
}
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_映射);
//获取SupportMapFragment,并在地图准备好使用时收到通知。
SupportMapFragment mapFragment=(SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map);
getMapAsync(这个);
}
/**
*一旦可用,就可以操纵贴图。
*当映射准备好使用时,将触发此回调。
*这是我们可以添加标记或线条、添加侦听器或移动摄影机的地方。在这种情况下,
*我们只是在澳大利亚悉尼附近加了一个标记。
*如果设备上未安装Google Play服务,系统将提示用户安装
*它位于SupportMapFragment内。此方法仅在用户
*已安装Google Play服务并返回应用程序。
*/
@凌驾
4月1日公开作废(谷歌地图谷歌地图){
//将操作栏上的标题更改为总线名称
setTitle(“总线1-CMC”);
mMap=谷歌地图;
//在VM总部添加标记-等待更新当前位置
LatLng马普托=新LatLng(-25.9758904,32.5805966);
mMap.addMarker(新MarkerOptions().position(马普托).title(“沃达康总部”);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(马普托,15));
//获取设备位置
locationManager=(locationManager)this.getSystemService(Context.LOCATION\u服务);
locationListener=新locationListener(){
@凌驾
已更改位置上的公共无效(位置){
isNetworkAvailable(MapsActivity.this);
//为了更准确
int suitableMeter=10;//调整您的需要

if(location.hasAccurance()&&location.getAccurance()您确定三星J1上的位置设置设置为高精度或仅GPS?请注意,您仅请求GPS位置,因此如果您的位置设置设置为省电,您的代码将无法获取位置。您是否收到错误?请尝试使用
网络\u提供程序
或在中请求位置更新代替GPS_PROVIDER.Hi,我没有收到任何错误。GPS已打开,它在google maps应用程序上工作。当我使用融合的提供程序时,它工作,但它不会不时刷新位置,而且不准确。您确定您的位置设置在三星J1上设置为高精度还是仅GPS?请注意,您仅在g GPS位置,因此如果您的位置设置设置为省电,您的代码将无法获取位置。您是否收到错误?尝试使用
网络\u提供商
请求位置更新,或者使用GPS\u提供商,或者不使用GPS\u提供商。嗨,我没有收到任何错误。GPS已打开,它在谷歌地图应用程序上工作。当我使用融合提供商时,它工作正常但它不会不时刷新位置,而且也不是那么准确。