【问题标题】:LocationManager not working on Android phoneLocationManager 无法在 Android 手机上运行
【发布时间】:2013-09-25 07:49:03
【问题描述】:

我有问题,

当我在模拟器中测试它并在 DDMS 屏幕中编辑位置时,位置管理器工作得非常好,但是当我在 Samsung Galaxy SII 上测试它时,它的 nog 工作正常..

请帮帮我。

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

public class LbsGeocodingActivity extends Activity {

    private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 0; // in Meters
    private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds

    protected LocationManager locationManager;

    protected Button retrieveLocationButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        retrieveLocationButton = (Button) findViewById(R.id.retrieve_location_button);

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MINIMUM_TIME_BETWEEN_UPDATES, MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, new MyLocationListener());

        retrieveLocationButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                showCurrentLocation();
            }
        });        
    }    

    public String getMyPhoneNumber(){
        TelephonyManager mTelephonyMgr;
        mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); 
        return mTelephonyMgr.getLine1Number();
    }

    protected void showCurrentLocation() {

        Location location =   locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

        if (location != null) {
            String message = String.format("Current Location \n  Longitude: %1$s \n Latitude: %2$s \n %3$s ", location.getLongitude(), location.getLatitude(), getMyPhoneNumber());
            Toast.makeText(LbsGeocodingActivity.this, message, Toast.LENGTH_LONG).show();
        }

    }   

    private class MyLocationListener implements LocationListener {

    public void onLocationChanged(Location location) {
        String message = String.format(
                "New Location \n Longitude: %1$s \n Latitude: %2$s \n  %3$s ",
                location.getLongitude(), location.getLatitude(),   getMyPhoneNumber()
        );
        Toast.makeText(LbsGeocodingActivity.this, message,   Toast.LENGTH_LONG).show();
    }

    public void onStatusChanged(String s, int i, Bundle b) {
        Toast.makeText(LbsGeocodingActivity.this, "Provider status changed",
                Toast.LENGTH_LONG).show();
    }

    public void onProviderDisabled(String s) {
        Toast.makeText(LbsGeocodingActivity.this,
                "Provider disabled by the user. GPS turned off",
                Toast.LENGTH_LONG).show();
    }

    public void onProviderEnabled(String s) {
        Toast.makeText(LbsGeocodingActivity.this,
                "Provider enabled by the user. GPS turned on",
                Toast.LENGTH_LONG).show();
    }

}
}

在我拥有的权限中:

ACCES_COURSE_LOCATION ACCES_FINE_LOCATION

【问题讨论】:

  • “不工作”是什么意思?你有错误吗?高度总是0?你在海边工作吗?
  • 不,它只是不调用该函数。当我关闭 GPS 时,它会吐司。但它永远不会让 LocationManager 敬酒..
  • “不调用函数”,什么函数??更具体。
  • 好吧,如果我在手机上测试它,我得到的唯一 Toast 就是我打开和关闭 GPS 时。即使我引用了 location.getLongitude()、location.getLatitude()、getMyPhoneNumber(),它在移动设备上也不起作用。但是当我在模拟器上测试它时,它工作得很好。抱歉,我在 Android 编程方面不是那么好。我希望这就足够了
  • locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 这会返回null?

标签: android locationmanager


【解决方案1】:

也许您正在室内进行测试,但无法获取任何 GPS 信息。 尝试更改LocationManager.GPS_PROVIDER-->LocationManager.NETWORK_PROVIDER.

【讨论】:

  • 这么简单的解释,但我一直在努力弄清楚为什么突然这不起作用。
【解决方案2】:

这可能是您错误使用String.format() 的问题。有时这会导致奇怪的、特定于设备的问题。试试吧,

String lon = "" + location.getLongitude();
String lat = "" + location.getLatitude();
String num = getMyPhoneNumber();

String message = String.format(
    "New Location \n Longitude: %1$s \n Latitude: %2$s \n %3$s",
    lon,
    lat,
    num
);

你试过用这个吗?

LocationManager lm = (LocationManager)act.getSystemService(Context.LOCATION_SERVICE);
Criteria crit = new Criteria();
crit.setAccuracy(Criteria.ACCURACY_FINE);
String provider = lm.getBestProvider(crit, true);
Location loc = lm.getLastKnownLocation(provider);

【讨论】:

  • 不抱歉,屏幕上仍然没有出现 Toast。它可能是什么问题,它在模拟器上运行,但在手机上运行不......三星 Galaxy SII 上的 GPS 已打开
  • 那么这是否意味着Location location 正在返回null?你确定吗?
  • 嗯,不,我只是没有得到任何吐司。它没有返回任何东西。我如何测试它是否返回 null?
  • if (location == null) Log.v("Activity", "loc is null");
  • 我再次测试了它,并在其中登录。和你给我的第二部分代码。它仍然没有工作。如果您愿意,我可以将代码发送给您,以便您自己测试?
【解决方案3】:

我在运行 4.0.4 的 HTC 不可思议的 S 上使用 locationmanager 时遇到了类似的问题。 它只会在一段时间后停止在真实设备上触发位置更改。 在其他一些设备上它可以工作。 非常断断续续。

我的解决方案是惹恼 LocationManager 并使用 LocationClient pardiagm。 http://developer.android.com/reference/com/google/android/gms/location/LocationClient.html

自从引入 google play 服务以来,Locationmanager 在我的设备上一直是断断续续的。至于为什么会发生,我无法给出任何其他解释。

但是 LocationClient 每次都有效,而且它从位置管理中删除了愚蠢的“提供者”概念。

【讨论】:

    猜你喜欢
    • 2011-10-16
    • 1970-01-01
    • 1970-01-01
    • 2012-07-02
    • 2017-03-11
    • 2021-04-17
    • 1970-01-01
    • 1970-01-01
    • 2013-08-24
    相关资源
    最近更新 更多