开发者

Why won't this GPS-related Android code work?

I try to run this code on emulator:

p开发者_运维知识库ackage andr.andr;

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.widget.Toast;

public class Gpstest2Activity extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    LocationListener mlocListener = new MyLocationListener();
    mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
}
public class MyLocationListener implements LocationListener
{
    public void onLocationChanged(Location loc)
    {
        loc.getLatitude();
        loc.getLongitude();
        String Text = "My current location is: " +
        "Latitud = " + loc.getLatitude() +
        "Longitud = " + loc.getLongitude();
        Toast.makeText( getApplicationContext(),Text,Toast.LENGTH_SHORT).show();
    }
    public void onProviderDisabled(String provider)
    {
        Toast.makeText( getApplicationContext(),"Gps Disabled",Toast.LENGTH_SHORT ).show();
    }
    public void onProviderEnabled(String provider)
    {
        Toast.makeText( getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show();
    }
    public void onStatusChanged(String provider, int status, Bundle extras)
    {
    }
}
}

and here is my manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="andr.andr"
  android:versionCode="1"
  android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission> 
<uses-permission android:name="android.permission.ACCESS_LOCATION"></uses-permission> 
<uses-permission android:name="android.permission.ACCESS_GPS"></uses-permission> 
<uses-permission android:name="android.permission.ACCESS_ASSISTED_GPS"></uses-permission> 
<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".Gpstest2Activity"
              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>

My problem is that, this code just don't run. I tried to send gps data through "DDMS" and "geo fix" but there is nothing happening. Sometime emulator just turn off. Do anyone know what is wrong with that code? Thanks for any help.


I don't see anything wrong with the code. Have you enabled GPS on the emulator? Have you checked logcat for errors?


If you are running in an emulator higher that Google Maps level 8 API, you will have problems passing locations using geo fix. What type are you using?


Run in device and then check will it display same error. If so then change your code otherwise it will work. I think your problem is that you are test it in emulator.

Emulator is not able to get location using GPS.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜