Android位置服务不起作用

时间:2012-04-22 19:38:46

标签: android geolocation

接下来:我花了一整夜的时间搜索互联网,特别是StackOverflow,试图找出为什么我无法在Android中使用基本的定位服务。我已尝试过各种模拟环境,从Android 1.6到Android 4,所有模拟GPS服务,以及实际的Android 2.2.2设备。位置总是返回null,我已经尝试了至少七个不同的可下载示例项目,逐字使用它们,并获得相同的结果。

显然,我错过了一些关键的东西。如果有人能指出我做错了什么,我将不胜感激。

com.mytestproject.android:

package com.mytestproject.android;
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;
import android.widget.Toast;
import com.mytestproject.android.R;
public class MyTestProject extends Activity {
private TextView mytext;
private LocationManager locmgr = null;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mytext = (TextView) findViewById(R.b.mytext);

    //grab the location manager service
    locmgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    mytext.setText("waiting for location");
}

//Start a location listener
LocationListener onLocationChange=new LocationListener() {
    public void onLocationChanged(Location loc) {
        //sets and displays the lat/long when a location is provided
        String latlong = "Lat: " + loc.getLatitude() + " Long: " + loc.getLongitude();   
        mytext.setText(latlong);
    }

    public void onProviderDisabled(String provider) {
    // required for interface, not used
    }

    public void onProviderEnabled(String provider) {
    // required for interface, not used
    }

    public void onStatusChanged(String provider, int status,
    Bundle extras) {
    // required for interface, not used
    }
};

//pauses listener while app is inactive
@Override
public void onPause() {
    super.onPause();
    locmgr.removeUpdates(onLocationChange);
}

//reactivates listener when app is resumed
@Override
public void onResume() {
    super.onResume();
    locmgr.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,10000.0f,onLocationChange);
}
}

main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/mytext"  
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>

的AndroidManifest.xml:

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

<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.CONTROL_LOCATION_UPDATES"></uses-permission>

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".MyTestProject"
        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>

2 个答案:

答案 0 :(得分:0)

将此添加到onCreate()函数

locmgr.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,10000.0f,onLocationChange);

答案 1 :(得分:0)

如果甚至没有调用onLocationChanged(),即使是示例项目,我也不认为“location is always null”是对问题的准确描述。仅当位置更改时才会调用onLocationChanged()。所以听起来你是(a)在使用模拟器时没有注入模拟位置或以其他方式驱动位置引擎,以及(b)在真实设备上没有足够的移动。我建议将一个示例项目加载到真实设备上,走到外面,并移动一些(几十米)。在onLocationChanged()方法(在示例项目中)中放置一些面包屑,以验证它是否被调用。