Java GPS Android-无法在实际手机上检索位置,但可以在模拟器上工作

Java GPS Android-无法在实际手机上检索位置,但可以在模拟器上工作,java,android,Java,Android,在实际手机或设备上测试手机时,我无法获取手机的纬度和经度,它总是返回null值,但当我在模拟器上测试手机并使用DDMS发送坐标时,它成功返回lat和long 这是我的班级: package locateodroid.james; import java.io.IOException; import java.util.List; import java.util.Locale; import android.content.BroadcastReceiver; import android.c

在实际手机或设备上测试手机时,我无法获取手机的纬度和经度,它总是返回null值,但当我在模拟器上测试手机并使用DDMS发送坐标时,它成功返回lat和long

这是我的班级:

package locateodroid.james;

import java.io.IOException;
import java.util.List;
import java.util.Locale;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.widget.EditText;
import android.widget.Toast;

public class SMSReceiver extends BroadcastReceiver{

    public SharedPreferences prefs;
    private String Keywords = "prefmode";
    private Double lat;
    private Double lon;
    private static final String FAR_MODE_KEY = "farmode";
    private Context con;

    @Override
    public void onReceive(Context context, Intent intent) {

        con = context;
        String FarModeKeyword= "";
        String message = "";
        String number = "";
        prefs = context.getSharedPreferences(Keywords, Context.MODE_PRIVATE);
        FarModeKeyword = prefs.getString(FAR_MODE_KEY, "");

        //---get the SMS message passed in---
        Bundle bundle = intent.getExtras();
        SmsMessage[] msgs = null;

        if (bundle != null)
        {
            //---retrieve the SMS message received---
            Object[] pdus = (Object[]) bundle.get("pdus");
            msgs = new SmsMessage[pdus.length];
            for (int i=0; i<msgs.length; i++)
            {
                msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
                message += msgs[i].getMessageBody().toString();
            }

            for (int i=0; i<msgs.length; i++)
            {
                msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
                number += msgs[i].getOriginatingAddress();
            }

        }


            getLocation();
            Toast.makeText(context,
                    lat + "_" + lon,
                    Toast.LENGTH_SHORT).show();
            sendSMS(number, lat + "_" + lon);


    }

    private void sendSMS(String phoneNumber, String message)
    {
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, null, null);
    }

    private getLocation ()
    {
        // Get the location manager
        LocationManager locationManager = (LocationManager)con.getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria ();
        Geocoder geoCoder = new Geocoder(con);
        LocationListener MyLocListener = new LocListener();
        List<String> providers = locationManager.getProviders(true);
        for (String provider : providers) {
            locationManager.requestLocationUpdates(provider, 0, 0, MyLocListener);
        }

        Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        lat = location.getLatitude();
        lon = location.getLongitude();

    }


    public class LocListener implements LocationListener
    {     
        @Override
          public void onLocationChanged(Location loc) {
              lat = loc.getLatitude();
              lon = loc.getLongitude();       
          }

        @Override
        public void onProviderDisabled(String arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

        }   
    }
}
package locateodroid.james;
导入java.io.IOException;
导入java.util.List;
导入java.util.Locale;
导入android.content.BroadcastReceiver;
导入android.content.Context;
导入android.content.Intent;
导入android.content.SharedReferences;
导入android.location.Address;
导入android.location.Criteria;
导入android.location.Geocoder;
导入android.location.location;
导入android.location.LocationListener;
导入android.location.LocationManager;
导入android.os.Bundle;
导入android.telephony.smsmsmanager;
导入android.telephony.sms消息;
导入android.widget.EditText;
导入android.widget.Toast;
公共类SMSReceiver扩展了BroadcastReceiver{
公共共享参考优先权;
私有字符串关键字=“prefmode”;
私人双lat;
私人双伦敦;
私有静态最终字符串FAR\u MODE\u KEY=“farmode”;
私人语境;
@凌驾
公共void onReceive(上下文、意图){
con=上下文;
字符串FarModeKeyword=“”;
字符串消息=”;
字符串编号=”;
prefs=context.getSharedReferences(关键字,context.MODE\u PRIVATE);
FarModeKeyword=prefs.getString(FAR\u MODE\u KEY,“”);
//---获取传入的SMS消息---
Bundle=intent.getExtras();
SmsMessage[]msgs=null;
if(bundle!=null)
{
//---检索收到的SMS消息---
Object[]pdus=(Object[])bundle.get(“pdus”);
msgs=新SMS消息[PDU.length];

对于(int i=0;i您是否已在清单中授予获取GPS坐标的所有权限?您是否已检查手机中的“使用GPS SettLite”选项是否已被选中?您是否尝试获取建筑物外的数据,因为GPS无法在建筑物内获得定位

如果所有这些问题的答案都是否定的,那么请先这样做