簡體   English   中英

定位服務數據不適用於某些手機,但適用於 Pixel 3

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

我有定位服務:

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


    }


}

這適用於 Pixel 3A 和第二個 Pixel,但不適用於 Galaxy S10、A5 和 Pixel XL。 有沒有人有任何可以阻止這種情況發生的限制的經驗? 我知道 go 的作用並不大,但希望如果您在看到這種行為之前能夠為我指明正確的方向。

使用 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);
    }
};

}

使用 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