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

Posted

技术标签:

【中文标题】定位服务数据不适用于某些手机,但适用于 Pixel 3【英文标题】:Location Service data not working on certain phones but working on 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。有没有人有任何可以阻止这种情况发生的限制的经验?我知道这没什么可做的,但希望如果您在看到这种行为之前能够为我指明正确的方向。

【问题讨论】:

【参考方案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());
        
    );

【讨论】:

fusedLocation 管理器是否使用 GPS,即它可以在没有数据访问的情况下工作吗?谢谢 是的,见developer.android.com/training/location/request-updates

以上是关于定位服务数据不适用于某些手机,但适用于 Pixel 3的主要内容,如果未能解决你的问题,请参考以下文章

MagicalRecord - 速记适用于某些实体,但不适用于其他实体

验证系统 它适用于某些输入,但不适用于其他输入 使用 jQuery

iOS UIActionSheet 回调适用于模拟器,但不适用于手机

VideoJS 适用于 safari 但不适用于 chrome 中的某些带有 CORS 的视频

Blazor PWA 适用于桌面浏览器,但不适用于智能手机

React 本机 Axios 发布请求适用于 iOS 模拟器,但不适用于物理手机