onLocationChanged方法不会调用

时间:2014-05-21 12:01:16

标签: android location

我经常搜索并在stackoverflow中看到很多帖子,但没有回答,或者说不清楚和复杂。它很简单我想在我从模拟器控件发送经度和纬度时调用onLocationChanged()方法,所以我在main.xml文件中编写了这段代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.mrg.findlocation.Main$PlaceholderFragment" >

    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

并将此代码放在Main.java文件中:

package com.mrg.findlocation;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;


public class Main extends Activity {

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

          final TextView text = (TextView)findViewById(R.id.textview);

          // Acquire a reference to the system Location Manager
          LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

          // Define a listener that responds to location updates
          LocationListener locationListner = new LocationListener() {

           @Override
           public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

           }

           @Override
           public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

           }

           @Override
           public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

           }

           @Override
           public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
            double latti = location.getLatitude();
            double longi = location.getLongitude();

            Log.d("MRG","It worked");
            text.setText("latti :"+latti+"\n"+"longi :"+longi);
           }
          };
          // Register the listener with the Location Manager to receive location updates
          locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListner);
    }

}

在清单文件中:

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

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.mrg.findlocation.Main"
            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>

然后我从模拟器控件发送两个值没有发生: enter image description here

有没有人可以永远解决这个问题?我粘贴的代码很容易复制,并且它不包含谷歌地图或其他代码

修改

此代码适用于真实设备

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListner);

此代码适用于模拟器

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListner);

2 个答案:

答案 0 :(得分:1)

我认为问题在于您是从网络提供商处请求更新,而模拟器是使用GPS提供商。

尝试更改:

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListner);

使用:

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListner);

答案 1 :(得分:-2)

尝试在您的活动类

上实现LocationListener
public class Main extends Activity implements LocationListener { 

@Override
public void onLocationChange(Location location){
// do something
  }
}