我希望有人可以帮助我解决在 Eclipse 中将模拟 GPS 数据发送到 Android AVD 时遇到的问题。如果我在真实设备上运行应用程序,一切正常,但是当我尝试在 eclipse 中使用内置模拟器时,什么都不会发生。我也尝试过远程登录到 AVD 并发出 geo fix 命令。下面是我正在使用的代码,虽然我认为如果它在真实设备上运行没有什么问题
清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.TestGoogleMaps"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:targetSdkVersion="10" android:minSdkVersion="8"/>
<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_MOCK_LOCATION" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".TestingGoogleMapsActivity"
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>
主.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"
>
</LinearLayout>
主要代码
package net.TestGoogleMaps;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class TestingGoogleMapsActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager locationManager;
String context = Context.LOCATION_SERVICE;
locationManager = (LocationManager)getSystemService(context);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
updateWithNewLocation(location);
locationManager.requestLocationUpdates(provider, 2000, 10,
locationListener);
}
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}
public void onProviderDisabled(String provider){
updateWithNewLocation(null);
}
public void onProviderEnabled(String provider){ }
public void onStatusChanged(String provider, int status,
Bundle extras){ }
};
public void updateWithNewLocation(Location location)
{
Context contextApp = getApplicationContext();
String text ;
int duration = 2000;
if(location == null)
{
text ="Null Location";
}
else
{
text = "Lat : " + location.getLatitude() + " Lon: " + location.getLongitude();
}
Toast toast = Toast.makeText(contextApp, text, duration);
toast.show();
}
}
在 eclipse 的 LogCat 选项卡中模拟 GPS 数据从 eclipse 到设备时,有时也会出现以下错误。
05-31 02:31:35.158: E/InputQueue-JNI(347): channel '406e6640 net.TestGoogleMaps/net.TestGoogleMaps.TestingGoogleMapsActivity (client)' ~ Publisher closed input channel or an error occurred. events=0x8
任何帮助将不胜感激!