简体   繁体   中英

Android - not receiving current location

I am trying to get the current location of the user with the below set of code, however it am not receiving anything. Can anyone see what I am doing wrong.

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mNavigationDrawerFragment = (NavigationDrawerFragment)


        getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
            mTitle = getTitle();

            // Set up the drawer.
            mNavigationDrawerFragment.setUp(
                    R.id.navigation_drawer,
                    (DrawerLayout) findViewById(R.id.drawer_layout));

            //buildGoogleApiClient();

            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .addApi(LocationServices.API)
                    .build();

        }

    @Override
        public void onConnected(Bundle bundle) {
           Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
                    mGoogleApiClient);
            if (mLastLocation != null) {

                String lat = String.valueOf(mLastLocation.getLatitude());
                String longg = String.valueOf(mLastLocation.getLongitude());

                Toast.makeText(MainActivity.this, "*************" + lat + "***************" + longg, Toast.LENGTH_LONG).show();
            }
        }

        @Override
        public void onConnectionSuspended(int i) {

            Toast.makeText(MainActivity.this, "NOOOOPE", Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onConnectionFailed(ConnectionResult connectionResult) {

            Toast.makeText(MainActivity.this, "NOOOOPE", Toast.LENGTH_SHORT).show();

        }

I am not seeing any of toast's being displayed.

I am also getting the following errors in the logcat, but not sure if they are related.

08-25 16:32:51.388    1406-1406/? E/NetworkScheduler.SchedulerReceiver﹕ Invalid parameter app
08-25 16:32:51.388    1406-1406/? E/NetworkScheduler.SchedulerReceiver﹕ Invalid package name : Perhaps you didn't include a PendingIntent in the extras?

I have the following permissions in the manifest file:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.INTERNET" />

Cheers

So simple solution really. I just didnt call

mGoogleApiClient.connect();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM