我正在尝试做的 ::在日志中记录当前的纬度和经度值

我做了什么 ::我已将协调器推送到Android中的模拟器

我无法执行的操作 ::我无法在输出中看到日志值


MainActivity.java

public class MainActivity extends Activity {

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

        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        LocationListener ll = new Mylocationlistener();

        boolean isGPS = lm
                .isProviderEnabled(LocationManager.GPS_PROVIDER);

        // If GPS is not enable then it will be on
        if(!isGPS)
        {
            Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
            intent.putExtra("enabled", true);
            sendBroadcast(intent);
        }

        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
    }
    private class Mylocationlistener implements LocationListener {
        @Override
        public void onLocationChanged(Location location) {
            if (location != null) {
                Log.d("LOCATION CHANGED", location.getLatitude() + "");
                Log.d("LOCATION CHANGED", location.getLongitude() + "");
                float speed = location.getSpeed();
                double altitude = location.getAltitude();
                Toast.makeText(MainActivity.this,"Latitude = "+
                        location.getLatitude() + "" +"Longitude = "+ location.getLongitude(),Toast.LENGTH_LONG).show();

                Log.d("---Latitude - Log ",""+location.getLatitude());
                Log.d("---Latitude - Log ",""+location.getLongitude());
            }

        }

        @Override
        public void onProviderDisabled(String provider) {
        }

        @Override
        public void onProviderEnabled(String provider) {
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    }
}

更新 清单 ::

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.presentcityname"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.presentcityname.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

===============>>#1 票数:1

如果在模拟器中运行应用程序,则此方法中的onLocationChanged(Location location)您将获得的位置始终为null。

  ask by Devrath translate from so

未解决问题?本站智能推荐: