FusedAPI可在android Oreo及更高版本上使用,但不适用于android 6.0(API23)

时间:2019-04-27 18:17:13

标签: android fusedlocationproviderapi android-version

我正在制作一个小型位置跟踪应用,该应用使用FUSEDAPI记录位置并将其发送到Firebase,并使用Services在后台运行。它在android O及以上版本上运行良好,但我不知道要使其在android棉花糖上运行所需的必要代码更改。

我正在为Android棉花糖使用标准startService(),为oreo使用前景服务。不调用onLocationChanged回调。我已经为oreo复制了相同的代码,请忽略“通知”构建器代码。

public class myMarshmallowService extends Service implements
        GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,
        LocationListener {
    String myUser = "";
    DatabaseReference mRef=FirebaseDatabase.getInstance("MYDATABASEURL").getReference();
    private static final String TAG = myService.class.getSimpleName();
    GoogleApiClient mLocationClient;
    LocationRequest mLocationRequest = new LocationRequest();

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        FirebaseAuth mAuth= FirebaseAuth.getInstance();
        if(mAuth.getCurrentUser().getUid()!=null)
        {
            myUser = mAuth.getCurrentUser().getUid();
        }
        else
        {
            Toast.makeText(this, "ERROR, RESTART", Toast.LENGTH_SHORT).show();
        }

        mLocationClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
        mLocationRequest.setInterval(4000);
        mLocationRequest.setFastestInterval(2000);
        int priority = LocationRequest.PRIORITY_HIGH_ACCURACY;
        mLocationRequest.setPriority(priority);
        mLocationClient.connect();
        return START_NOT_STICKY;
    }

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

    private void sendLocationToFirebase(Location location) {
        Date currentTime = Calendar.getInstance().getTime();
        mRef.child(myUser).child(currentTime.toString()).child("latitude").setValue(location.getLatitude());
        mRef.child(myUser).child(currentTime.toString()).child("longitude").setValue(location.getLongitude());
    }

    @Override
    public void onConnected(Bundle dataBundle) {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        if(mLocationClient.isConnected()) {
            LocationServices.FusedLocationApi.requestLocationUpdates(mLocationClient, mLocationRequest, this);
        }
    }

    @Override
    public void onLocationChanged(Location location) {
        sendLocationToFirebase(location);
        Toast.makeText(this, "Location Sent", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
    }

    @Override
    public void onConnectionSuspended(int i) {
        Log.d(TAG, "Connection suspended");
    }
}

我是android的新手,这是我的第一个项目。

0 个答案:

没有答案