hilary3113 2011-03-07 14:45
浏览 269
已采纳

我写的Android位置定位不能定位

我写了一个位置定项目,在模拟器上可以定位,但装到手机上,就不能定位了,请问是怎么回事?我已打开GPS,配置文件件里也配置了代码如下

import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;

public class Main 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);
        
        String provider = LocationManager.GPS_PROVIDER;
        String provider2 = LocationManager.NETWORK_PROVIDER;
        Location location = locationManager.getLastKnownLocation(provider);
        updateWithNewLocation(location);
    }
    private void updateWithNewLocation(Location location){
        String latLongString;
        TextView myLocationText;
        myLocationText =(TextView) findViewById(R.id.tv);
        
        if(location != null){
            double lat = location.getLatitude();
            double lng = location.getLongitude();
            latLongString = "Lat: "+lat+"\nLong: "+lng;
        }else{
            latLongString = "No location found";
        }
        myLocationText.setText("Your Current Position is: \n"+ latLongString);
    }
}

 配置文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.action"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Main"
                  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>
  <uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest> 

 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/tv"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
</LinearLayout>
  • 写回答

3条回答 默认 最新

  • zhiwei 2011-03-08 14:45
    关注

    看过楼主的代码,理论上没问题。

    但是,要注意的是,真机上的GPS定位需要环境有卫星信号,要不然就是一直在获取坐标而已。你可以看看状态栏上那卫星的图标是否一闪一闪的,它不闪了就证明及其能连上卫星,可以获取定位信息了。所以GPS一般要在室外空旷的地方才能工作正常,这还跟你机器上的芯片性能挂钩。

    另外,定位还可以通过网络进行,具体的原理不说了,自己上网查去。试下使用String provider2 = LocationManager.NETWORK_PROVIDER; 这个provider,但要在xml里配上网络访问的权限。这种定位的方式,只要网络畅通,一般5秒就会获得坐标信息,但是精度要比基于GPS的差许多。

    建议为location加上一个监听器,每当坐标改变时都去触发事件,这样才方便测试。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示