Localbroadcastmanager无法正常工作。获得经纬度

时间:2018-01-07 13:01:57

标签: android google-maps google-api localbroadcastmanager

我的应用是关于获取后台服务的位置。但是,它无法返回纬度和经度。这是代码。

服务方:

public class Map extends Service implements
    GoogleApiClient.ConnectionCallbacks, 
    GoogleApiClient.OnConnectionFailedListener,
    LocationListener {

private static final String TAG = Map.class.getSimpleName();
GoogleApiClient mLocationClient;
LocationRequest mLocationRequest = new LocationRequest();


public static final String ACTION_LOCATION_BROADCAST = Map.class.getName() + "LocationBroadcast";
public static final String EXTRA_LATITUDE = "extra_latitude";
public static final String EXTRA_LONGITUDE = "extra_longitude";


@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    mLocationClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();

    mLocationRequest.setInterval(10000);
    mLocationRequest.setFastestInterval(5000);


    int priority = LocationRequest.PRIORITY_HIGH_ACCURACY; 


    mLocationRequest.setPriority(priority);
    mLocationClient.connect();


    return START_STICKY;
}

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


@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) {

        Log.d(TAG, "== Error On onConnected() Permission not granted");

        return;
    }
    LocationServices.FusedLocationApi.requestLocationUpdates(mLocationClient, mLocationRequest, this);
    Log.d(TAG, "Connected to Google API");
}


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


@Override
public void onLocationChanged(Location location) {
    Log.d(TAG, "Location changed");


    if (location != null) {
        Log.d(TAG, "== location != null");


        sendMessageToUI(String.valueOf(location.getLatitude()), String.valueOf(location.getLongitude()));
    }
}

private void sendMessageToUI(String lat, String lng) {

    Log.d(TAG, "Sending info...");

    Intent intent = new Intent(ACTION_LOCATION_BROADCAST);
    intent.putExtra(EXTRA_LATITUDE, lat);
    intent.putExtra(EXTRA_LONGITUDE, lng);
    LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    Log.d(TAG, "Failed to connect to Google API");

}

}

MainActivity方面:

public class MainActivity extends AppCompatActivity {

private static final String TAG = MainActivity.class.getSimpleName();


private static final int REQUEST_PERMISSIONS_REQUEST_CODE = 34;


private boolean mAlreadyStartedService = false;
private FirebaseUser user;
private FirebaseAuth mAuth;
private DatabaseReference mDatabase;


private double longitude;
private double latitude;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mAuth = FirebaseAuth.getInstance();
    mDatabase = FirebaseDatabase.getInstance().getReference();

    user = mAuth.getCurrentUser();

    LocalBroadcastManager.getInstance(this).registerReceiver(
            bReceiver, new IntentFilter("ACTION_LOCATION_BROADCAST"));


}

private BroadcastReceiver bReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String latitude = intent.getStringExtra(Map.EXTRA_LATITUDE);
        String longitude = intent.getStringExtra(Map.EXTRA_LONGITUDE);

        dd.setText("HI");

        if (latitude != null && longitude != null) {
            mAuth = FirebaseAuth.getInstance();
            user = mAuth.getCurrentUser();

            if (!String.valueOf(latitude).equals(""))
                mDatabase.child("users").child(user.getUid()).child("latitude").setValue(latitude);
            if (!String.valueOf(longitude).equals(""))
                mDatabase.child("users").child(user.getUid()).child("longitude").setValue(longitude);

            cc.setText("Latitude:" + latitude + " Longitude: " + longitude);
        }
    }
};


private void startStep3() {

        Intent intent = new Intent(this, Map.class);
        this.startService(intent);
        ff.setText("3ok");
        mAlreadyStartedService = true;
        }

@Override
public void onResume() {
    super.onResume();
    startStep3();
    ee.setText("Start");
}


@Override
public void onDestroy() {

    stopService(new Intent(this, Map.class));
    mAlreadyStartedService = false;



    super.onDestroy();
}

}

AndroidManifest:

<service android:name=".Map" />

它可以在页面上显示“开始”和“3ok”但无法获取位置。它显示“TextView”而不是位置信息。我错过了任何代码,问题出在哪里?这是使用服务的正确方法吗?谢谢。

0 个答案:

没有答案