的LocationManager requestLocationUpdates不工作 [英] LocationManager requestLocationUpdates not working

查看:260
本文介绍了的LocationManager requestLocationUpdates不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个令人毛骨悚然的问题。我试图让我的仿真器的位置。它的工作很好,当我得到我的仿真器的位置。但是,当我改变我的位置坐标没有任何反应。 GPS 工作正常。

下面是我的code

Main.java

 进口android.app.Activity;
进口android.app.AlertDialog;
进口android.app.AlertDialog.Builder;
进口android.content.DialogInterface;
进口android.content.DialogInterface.OnClickListener;
进口android.content.Intent;
进口android.location.Location;
进口android.location.LocationListener;
进口android.location.LocationManager;
进口android.os.Bundle;进口android.util.Log;
进口android.view.Menu;
进口android.widget.TextView;公共类主要活动扩展{
TextView的tvStatus;
LM的LocationManager;
布尔gpsEnabled;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
    tvStatus =(的TextView)findViewById(R.id.textView1);    LM =(的LocationManager)getSystemService(LOCATION_SERVICE);
    如果(lm.isProviderEnabled(LocationManager.GPS_PROVIDER)){
        Log.d(,GPS是活动);
        位置L = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        tvStatus.setText(l.getLatitude()+,+ l.getLongitude());
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,
                新LocationListener的(){                    @覆盖
                    公共无效onStatusChanged(字符串为arg0,ARG1 INT,
                            捆绑ARG2){
                        // TODO自动生成方法存根                    }                    @覆盖
                    公共无效onProviderEnabled(字符串为arg0){
                        // TODO自动生成方法存根                    }                    @覆盖
                    公共无效onProviderDisabled(字符串为arg0){
                        // TODO自动生成方法存根                    }                    @覆盖
                    公共无效onLocationChanged(位置为arg0){
                        tvStatus.setText(GPS功能的+ gpsEnabled
                                +\\ nLatitude:+ arg0.getLatitude()
                                +\\ nLongitude:+ arg0.getLongitude());
                    }
                });
    }}    }

的Manifest.xml

 <?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.example.enabelinglocationprovider
    安卓版code =1
    机器人:=的versionName1.0>    <用途-SDK
        安卓的minSdkVersion =5
        机器人:targetSdkVersion =10/>
    <使用许可权的android:NAME =android.permission.ACCESS_FINE_LOCATION/>
    <使用许可权的android:NAME =android.permission.ACCESS_MOCK_LOCATION/>
    <使用许可权的android:NAME =android.permission.ACCESS_COARSE_LOCATION/>
    <使用许可权的android:NAME =android.permission.ACCESS_NETWORK_STATE/>
    <使用许可权的android:NAME =android.permission.INTERNET对/>    <应用
        机器人:allowBackup =真
        机器人:图标=@绘制/ ic_launcher
        机器人:标签=@字符串/ APP_NAME
        机器人:主题=@风格/ AppTheme>
        <活动
            机器人:名字=com.example.enabelinglocationprovider.Main
            机器人:标签=@字符串/ APP_NAME>
            &所述;意图滤光器>
                <作用机器人:名字=android.intent.action.MAIN/>                <类机器人:名字=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;
    < /用途>< /清单>


解决方案

三星手机都有这个问题。有破解了这一点。你需要踢屁股的的LocationManager来从地图缓存中的最新位置。

  God.locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,
            新LocationListener的(){
                @覆盖
                公共无效onStatusChanged(字符串提供商,INT地位,捆绑演员){
                }                @覆盖
                公共无效onProviderEnabled(字符串提供商){
                }                @覆盖
                公共无效onProviderDisabled(字符串提供商){
                }                @覆盖
                公共无效onLocationChanged(决赛地点){
                }
            });
    currentLocation = God.locationManager
            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

呼叫 getLastKnownLocation 踢后 requestLocationUpdates 0的。

I Have This Creepy problem. i am trying to get the location of my emulator. it working fine when i get the location of my emulator. but when i change my location coordinates nothing happens. gps is working fine.

Here is my code

Main.java

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

import android.util.Log;
import android.view.Menu;
import android.widget.TextView;

public class Main extends Activity {
TextView tvStatus;
LocationManager lm;
boolean gpsEnabled;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tvStatus = (TextView) findViewById(R.id.textView1);

    lm = (LocationManager) getSystemService(LOCATION_SERVICE);
    if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        Log.d("", "GPS is Active");
        Location l = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        tvStatus.setText(l.getLatitude()+" , "+l.getLongitude());
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
                new LocationListener() {

                    @Override
                    public void onStatusChanged(String arg0, int arg1,
                            Bundle arg2) {
                        // TODO Auto-generated method stub

                    }

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

                    }

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

                    }

                    @Override
                    public void onLocationChanged(Location arg0) {
                        tvStatus.setText("GPS ENABLED: " + gpsEnabled
                                + "\nLatitude: " + arg0.getLatitude()
                                + "\nLongitude: " + arg0.getLongitude());
                    }
                });
    }

}

    }

Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.enabelinglocationprovider"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="5"
        android:targetSdkVersion="10" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.enabelinglocationprovider.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>

</manifest>

解决方案

Samsung phones have this problem. There is hack to this. You need to kick the locationmanager on the butt to get the latest location from the maps cache.

God.locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
            new LocationListener() {
                @Override
                public void onStatusChanged(String provider, int status, Bundle extras) {
                }

                @Override
                public void onProviderEnabled(String provider) {
                }

                @Override
                public void onProviderDisabled(String provider) {
                }

                @Override
                public void onLocationChanged(final Location location) {
                }
            });
    currentLocation = God.locationManager
            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

Call getLastKnownLocation after kicking the requestLocationUpdates with 0's.

这篇关于的LocationManager requestLocationUpdates不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆