【问题标题】:Location Service data not working on certain phones but working on Pixel 3定位服务数据不适用于某些手机,但适用于 Pixel 3
【发布时间】:2020-10-05 08:15:06
【问题描述】:

我有定位服务:

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。有没有人有任何可以阻止这种情况发生的限制的经验?我知道这没什么可做的,但希望如果您在看到这种行为之前能够为我指明正确的方向。

【问题讨论】:

    标签: java android android-service android-location


    【解决方案1】:

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

    }

    使用如下类

    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());
            }
        });
    

    【讨论】:

    猜你喜欢
    • 2015-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多