2012-03-07 106 views
2

它会在模拟器上运行,但不会显示位置。 gps也在设置中打开。我从教程中获得了代码,它为大量的人工智能提供了工作,但no1却遇到了我的问题。如果有人能告诉我为什么,我将不胜感激。哦,即时通讯在Android 2.2GPS代码没有错误,但没有显示位置

运行这是我的代码:

package gps.attempt; 

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

public class GpsAttemptActivity extends Activity implements LocationListener { 

/* this class implements LocationListener, which listens for both 
* changes in the location of the device and changes in the status 
* of the GPS system. 
* */ 

static final String tag = "Main"; // for Log 

TextView txtInfo; 
LocationManager lm; 
StringBuilder sb; 
int noOfFixes = 0; 

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

    /* get TextView to display the GPS data */ 
    txtInfo = new TextView(this); 

    /* the location manager is the most vital part it allows access 
    * to location and GPS status services */ 
    lm = (LocationManager) getSystemService(LOCATION_SERVICE); 
} 

@Override 
protected void onResume() { 
    /* 
    * onResume is is always called after onStart, even if the app hasn't been 
    * paused 
    * 
    * add location listener and request updates every 1000ms or 10m 
    */ 
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10f, this); 
    super.onResume(); 
} 

@Override 
protected void onPause() { 
    /* GPS, as it turns out, consumes battery like crazy */ 
    lm.removeUpdates(this); 
    super.onResume(); 
} 

@Override 
public void onLocationChanged(Location location) { 
    Log.v(tag, "Location Changed"); 

    sb = new StringBuilder(512); 

    noOfFixes++; 

    /* display some of the data in the TextView */ 

    sb.append("No. of Fixes: "); 
    sb.append(noOfFixes); 
    sb.append('\n'); 
    sb.append('\n'); 

    sb.append("Londitude: "); 
    sb.append(location.getLongitude()); 
    sb.append('\n'); 

    sb.append("Latitude: "); 
    sb.append(location.getLatitude()); 
    sb.append('\n'); 

    sb.append("Altitiude: "); 
    sb.append(location.getAltitude()); 
    sb.append('\n'); 

    sb.append("Accuracy: "); 
    sb.append(location.getAccuracy()); 
    sb.append('\n'); 

    sb.append("Timestamp: "); 
    sb.append(location.getTime()); 
    sb.append('\n'); 

    txtInfo.setText(sb.toString()); 
} 

@Override 
public void onProviderDisabled(String provider) { 
    /* this is called if/when the GPS is disabled in settings */ 
    Log.v(tag, "Disabled"); 

    /* bring up the GPS settings */ 
    Intent intent = new Intent(
      android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
    startActivity(intent); 
} 

@Override 
public void onProviderEnabled(String provider) { 
    Log.v(tag, "Enabled"); 
    Toast.makeText(this, "GPS Enabled", Toast.LENGTH_SHORT).show(); 

} 

@Override 
public void onStatusChanged(String provider, int status, Bundle extras) { 
    /* This is called when the GPS status alters */ 
    switch (status) { 
    case LocationProvider.OUT_OF_SERVICE: 
     Log.v(tag, "Status Changed: Out of Service"); 
     Toast.makeText(this, "Status Changed: Out of Service", 
       Toast.LENGTH_SHORT).show(); 
     break; 
    case LocationProvider.TEMPORARILY_UNAVAILABLE: 
     Log.v(tag, "Status Changed: Temporarily Unavailable"); 
     Toast.makeText(this, "Status Changed: Temporarily Unavailable", 
       Toast.LENGTH_SHORT).show(); 
     break; 
    case LocationProvider.AVAILABLE: 
     Log.v(tag, "Status Changed: Available"); 
     Toast.makeText(this, "Status Changed: Available", 
       Toast.LENGTH_SHORT).show(); 
     break; 
    } 
} 

@Override 
protected void onStop() { 
    /* may as well just finish since saving the state is not important for this toy app */ 
    finish(); 
    super.onStop(); 
} 

}

这是我的清单:

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

<uses-sdk android:minSdkVersion="8" /> 

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

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

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

+0

哦和logcat的是空 – user1234167 2012-03-07 15:49:55

+0

你如何通过你的GPS位置到模拟器? – 2012-03-07 15:50:31

+0

Ru在谷歌AVD模拟器中运行?你是否想在清单中添加任何使用libarary!? – 2012-03-07 15:52:30

回答

4

你的问题是(至少)你的TextView不可见。您应该在res/layout/main.xml中添加一个TextView并使用该文本显示文本。例如:

/* get TextView to display the GPS data from Layout*/ 
txtInfo = (TextView) findViewById(R.id.textView1); 

和TextView的XML中看起来像

<TextView android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

编辑:作为NickT指出,你也有你的onPause()

EDIT2改变super.onResume()super.onPause():不需要使用StringBuilder来构建您的字符串。您可以放心地做

String text = "No. of Fixes: " + 
    noOfFixes + 
    '\n' + 
    // and so on 
    '\n'; 
txtInfo.setText(text); 

Java编译器会自动使用StringBuilder,但你的代码看起来更干净。见例如when to use StringBuilder in java

+0

发生了什么事吗? – 2012-03-07 15:57:29

+0

看到这行说'txtInfo = new TextView(this);'?如果你不以某种方式将它添加到布局中,它不可见 – zapl 2012-03-07 15:58:47

+0

+1你是对的。 – 2012-03-07 16:00:24

2

在模拟器使用GPS您需要使用geo fix命令手动传递位置。

本指南How to enable fake GPS on Android和这SO question应该帮助你得到它的工作。

+0

或从DDMS角度的'模拟器控制'窗格中传递它 – NickT 2012-03-07 16:04:26

+0

@NickT感谢您做到了这一点更容易:) – user1234167 2012-03-07 18:02:09

0

//添加这个在你的清单中请确保您的AVD是谷歌API的

<uses-library android:name="com.google.android.maps" /> 
+0

这对GPS没有帮助。它将允许您在应用内显示Google地图。 – zapl 2012-03-07 16:00:43

相关问题