0

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

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

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);
}
}

主.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>
4

2 回答 2

0

将此添加到onCreate()函数中,

locmgr.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,10000.0f,onLocationChange);
于 2012-04-22T19:42:39.090 回答
0

如果onLocationChanged()甚至没有被调用,即使是示例项目,那么我认为“位置始终为空”并不是对问题的准确描述。onLocationChanged()仅在位置更改时调用。所以听起来你(a)在使用模拟器时没有注入模拟位置或以其他方式驱动位置引擎,并且(b)在真实设备上移动得不够多。我建议将一个示例项目加载到真实设备上,到外面去,然后移动一些(几 10 米)。在方法中(在示例项目中)放置一些面包屑onLocationChanged()以验证它是否被调用。

于 2012-04-22T20:35:45.533 回答