Android - 未收到当前位置

时间:2015-08-25 15:56:11

标签: java android location android-location

我正在尝试使用以下代码集来获取用户的当前位置,但是它没有收到任何内容。任何人都可以看到我做错了什么。

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

        }

我没有看到任何敬酒被展示。

我在logcat中也遇到以下错误,但不确定它们是否相关。

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?

我在清单文件中拥有以下权限:

<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" />

干杯

1 个答案:

答案 0 :(得分:0)

真的很简单的解决方案。我只是没有打电话

mGoogleApiClient.connect();