Android手机上的GPS定位器错误

时间:2013-12-05 14:49:03

标签: java android gps

我创建了一个应用程序,当我点击一个按钮时,它会给我我的gps坐标。在模拟器中它没有问题,但是当我在移动电话上尝试时,接下来有两种情况:当我激活GPS时,没有发生任何事情,当我没有连接GPS时,它说“Gps禁用”

到目前为止,这是我的代码: 这是.java文件:(Saver.java):

package com.example.lifesaver;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class Saver extends Activity {

Button b;

Location newLocation = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_saver);
b = (Button) findViewById(R.id.button1);


// We use LocationManager class to obtain GPS locations
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();

mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);


b.setOnClickListener(new OnClickListener(){
  public void onClick(View v)
  {
      if (newLocation != null) {

          String Text = "Current location is: " + "Latitud = "
                  + newLocation.getLatitude() + "Longitud = "
                  + newLocation.getLongitude();
          Toast.makeText(getApplicationContext(), Text,
                  Toast.LENGTH_SHORT).show();
      }
  }
}); 
}

//MyLocationListener class

public class MyLocationListener implements LocationListener
{

    public void onLocationChanged(Location loc)
    {
        newLocation = loc;
    }

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

.xml文件(activity_saver.xml):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Saver" >

<Button
android:id="@+id/button1"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/circle" 
android:onClick="onClick"/>

</RelativeLayout>

另外,我在AndroidManifest.xml文件中添加了这个:

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

2 个答案:

答案 0 :(得分:0)

您是否尝试以编程方式开启GPS?

它不适用于版本4.0以上。你必须手动打开它。

使用我的代码: -

Android - How to get the user current location every time based on sim Network

答案 1 :(得分:0)

您必须等待GPS才能获得修复。冷启动后几分钟内可能无法触发onLocationChanged。如果您在建筑物内,您可能从不获得修复。没有“立即通过GPS获取我的位置”这样的设施。