简体   繁体   English

定位服务数据不适用于某些手机,但适用于 Pixel 3

[英]Location Service data not working on certain phones but working on Pixel 3

I have a location service:我有定位服务:

public class LocationService extends Service implements Serializable {

    public static final String TAG = "LocationService";
    private LocationListener locationListener;
    private LocationManager locationManager;
    public static boolean locationUpdateSent = false;


    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public void onCreate() {

        super.onCreate();
        startMyOwnForeground();
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);


        locationListener = new LocationListener() {
            @RequiresApi(api = Build.VERSION_CODES.O)
            @Override
            public void onLocationChanged(Location location) {
                buildLocationEvent(location);
            }

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

            }

            @Override
            public void onProviderEnabled(String provider) {

            }

            @Override
            public void onProviderDisabled(String provider) {
                Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(i);
            }
        };

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

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);
        return START_STICKY;
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    private void startMyOwnForeground(){
        String NOTIFICATION_CHANNEL_ID = "LocationChannel";
        String channelName = "Location Service";
        NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_HIGH);
        chan.setLightColor(Color.GRAY);
        chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        assert manager != null;
        manager.createNotificationChannel(chan);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
        Notification locNotification = notificationBuilder.setOngoing(true)
                .setContentTitle("Location is running in background")
                .setPriority(NotificationManager.IMPORTANCE_HIGH)
                .setCategory(Notification.CATEGORY_SERVICE)
                .setSmallIcon(android.R.drawable.ic_menu_mylocation)
                .build();
        startForeground(1, locNotification);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(locationManager != null){
            locationManager.removeUpdates(locationListener);
        }
        stopSelf();
        stopForeground(true);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }



    @RequiresApi(api = Build.VERSION_CODES.O)
    public void buildLocationEvent(Location location){
        //Do Something


    }


}

This works fine on Pixel 3A and on a second Pixel, however it does not work on a galaxy S10, A5 and a Pixel XL.这适用于 Pixel 3A 和第二个 Pixel,但不适用于 Galaxy S10、A5 和 Pixel XL。 Has anyone any experience of any restrictions which would stop this happening?有没有人有任何可以阻止这种情况发生的限制的经验? I know it's not much to go on but hopefully if you have seen this behaviour before you might be able to point me in the right direction.我知道 go 的作用并不大,但希望如果您在看到这种行为之前能够为我指明正确的方向。

use google fusedlocation client使用 google fusedlocation 客户端

public class fusedLocation {
Context act;
FusedLocationProviderClient mFusedLocationClient;
Location myLocation=null;
static final int REQUEST_PERMS = 1;
public interface LocationChangListener{
void OnlocationChanged(Location current_location);

 }
LocationChangListener main_handler=new LocationChangListener() {
    @Override
    public void OnlocationChanged(Location current_location) {

    }
};
public fusedLocation(final Activity act,LocationChangListener handler_) {
    this.act = act;
    this.main_handler=handler_;
    mFusedLocationClient=LocationServices.getFusedLocationProviderClient(act);
    final String permissions[] = {Manifest.permission.ACCESS_FINE_LOCATION,        Manifest.permission.ACCESS_COARSE_LOCATION};
    if (ActivityCompat.checkSelfPermission(act, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
        update_tm.schedule(new TimerTask() {
            @Override
            public void run() {


               act.runOnUiThread(new Runnable() {
                   @Override
                   public void run() {
                       getLastLocation();
                   }
               });

            }
        },100,10000);
    } else {
        ActivityCompat.requestPermissions(act, permissions, REQUEST_PERMS);
    }

}
Timer update_tm=new Timer();
private void getLastLocation() {
    mFusedLocationClient.getLastLocation().addOnCompleteListener(new   OnCompleteListener<Location>() {
        @Override
        public void onComplete(@NonNull Task<Location> task) {
            Location location = task.getResult();
            if (location == null) {
                requestNewLocationData();
                Log.e("Main", " location is null men");
            } else {
                Log.e("my coords==> ", "lats " + location.getLatitude() + " longs " + location.getLongitude());
                main_handler.OnlocationChanged(location);
            }
        }
    });
}

private void requestNewLocationData() {
    LocationRequest mLocationRequest = new LocationRequest();
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationRequest.setInterval(0);
    mLocationRequest.setFastestInterval(0);
    mLocationRequest.setNumUpdates(1);

    mFusedLocationClient = LocationServices.getFusedLocationProviderClient(act);
    mFusedLocationClient.requestLocationUpdates(
            mLocationRequest, mLocationCallback,
            Looper.myLooper()
    );
}
private LocationCallback mLocationCallback = new LocationCallback() {
    @Override
    public void onLocationResult(LocationResult locationResult) {
        Location mLastLocation = locationResult.getLastLocation();
        Log.e("my refreshed coords==> ", "lats " + mLastLocation.getLatitude() + " longs " + mLastLocation.getLongitude());
        main_handler.OnlocationChanged(mLastLocation);
    }
};

} }

Use the class as below使用 class 如下

fusedLocation fusedLocation = new fusedLocation(your_context, new fusedLocation.LocationChangListener() {
        @Override
        public void OnlocationChanged(Location current_location) {
        //Do as you please with the location    
Log.e("Position ","mypos_lat "+current_location.getLatitude()+" <==> mypos_long "+current_location.getLongitude());
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM