简体   繁体   English

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

[英]GPS Android - Cannot retrieve location on actual phone but works on emulator

I cannot the get the latitude and longitude of my phone when testing it on an actual phone or device, it always return null values, but when I test it on the emulator and send coordinates using DDMS, it succesfully returns the lat and long. 在实际的电话或设备上测试手机时,无法获得其纬度和经度,它始终返回值,但是在模拟器上进行测试并使用DDMS发送坐标时,它成功返回了经纬度。

Here is my class: 这是我的课:

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

        }   
    }
}

Thanks in advance! 提前致谢!

did you give all permission in manifest to get GPS co ordinate ? 您是否在清单中授予了所有获得GPS坐标的权限? have you check whether the option of "USE GPS Settelite" in your phone is already selected or not ? 您是否检查过手机中的“使用GPS Settelite”选项? Have You try getting data outside the building because GPS will not get a fix inside building. 您是否尝试过在建筑物外获取数据,因为GPS无法在建筑物内得到修复。

If answer of all these questions is no. 如果所有这些问题的答案都是“否”。 than please do that first . 比请先这么做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试在 Android 设备上检索位置...适用于模拟器但不适用于真实设备 - Attempting to retrieve location on Android Device... works on emulator but not real device Android - 无法在模拟器上获取gps位置 - Android - Unable to get the gps location on the emulator Android:Mediaplayer 可以在模拟器上运行,但不能在手机上运行 - Android: Mediaplayer works in emulator but not on phone 如何在Android上使此位置查找器(GPS)正常工作? - How to make this location finder (GPS) on android works? Android语音到文本可以在模拟器中运行,但不能在手机上运行 - Android speech to text works in emulator but not on phone Android:Internet可以在模拟器中运行,但不能在我的手机上运行 - Android: Internet works in emulator but not on my phone 是否可以通过电话在Android设备上发送GPS位置信息? - Is it possible to send gps location on an android device with a phone call? 无法检测到模拟器上的位置,Android Studios - Cannot detect location on emulator, Android Studios android-android studio模拟器中的SSL问题,可以在手机上正常工作 - android - SSL problems in android studio emulator, works fine on phone Android应用程序可以在真实手机上运行,​​但不能在模拟器上运行-Android开发吗? - Android app works on real phone but not on the emulator - Android Development?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM