位置管理器不工作的Andr​​oid 4.0.2模拟器 [英] Location Manager not working in Android 4.0.2 Emulator

查看:161
本文介绍了位置管理器不工作的Andr​​oid 4.0.2模拟器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用位置管理器在我的应用程序GPS跟踪。使用安卓 2.2,2.3.3仿真器和设备,而该应用程序将返回恰当的位置。但是,奇怪的是我所测试的应用程序中的仿真器4.0.2 。它返回部队密切误差 05-28 15:13:46.584:E / AndroidRuntime(4458):在com.package.TestRun.registerLocationListeners(TestRun.java:191)的logcat 。我已经实现如下的code:

I am using Location Manager for GPS tracking in my application. The app returns apt location while using in Android 2.2,2.3.3 emulators and devices. But, Strangely i tested the app in Emulator 4.0.2. It returns force close error as 05-28 15:13:46.584: E/AndroidRuntime(4458): at com.package.TestRun.registerLocationListeners(TestRun.java:191) in logcat. I have implemented the code as below:

    LocationManager lm;
    LocationManager lmNet;
    private void registerLocationListeners() {
    lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    if (myGPSLocationListener == null || mynetworkprovider == null)
    {
        createLocationListeners();  
    }  
    lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER ,0,0,mynetworkprovider);
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER ,60000,0,myGPSLocationListener);

}
private void createLocationListeners() {
    mynetworkprovider = new LocationListener() {  
       public void onLocationChanged(Location argLocation) {
           if (argLocation != null) {
            gps[0] = argLocation.getLatitude(); 
            gps[1] = argLocation.getLongitude(); 
            //Toast.makeText( getApplicationContext(),"Network provider Lat =" + gps[0] + " Lon = " + gps[1] , Toast.LENGTH_SHORT).show();
            }
                //Toast.makeText( getApplicationContext(),"On Location change" , Toast.LENGTH_SHORT).show();
            }
            public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub
            //  Toast.makeText( getApplicationContext(),"Disable" , Toast.LENGTH_SHORT).show();
            }
            public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub
            //  Toast.makeText( getApplicationContext(),"enabled" , Toast.LENGTH_SHORT).show();
            }
            public void onStatusChanged(String provider,
             int status, Bundle extras) {
            // TODO Auto-generated method stub
                //Toast.makeText( getApplicationContext(),"Status change" , Toast.LENGTH_SHORT).show();

            }
            };

            myGPSLocationListener = new LocationListener() {  
                   public void onLocationChanged(Location argLocation) {
                       if (argLocation != null) {
                        gps[0] = argLocation.getLatitude(); 
                        gps[1] = argLocation.getLongitude(); 
                        //Toast.makeText( getApplicationContext(),"GPS Lat =" + gps[0] + " Lon = " + gps[1] , Toast.LENGTH_SHORT).show();
                        }
                            //Toast.makeText( getApplicationContext(),"On Location change" , Toast.LENGTH_SHORT).show();
                        }
                        public void onProviderDisabled(String provider) {
                        // TODO Auto-generated method stub
                            //Toast.makeText( getApplicationContext(),"Disable" , Toast.LENGTH_SHORT).show();
                            //launchGPS();
                        }
                        public void onProviderEnabled(String provider) {
                        // TODO Auto-generated method stub
                            //Toast.makeText( getApplicationContext(),"enabled" , Toast.LENGTH_SHORT).show();
                        }
                        public void onStatusChanged(String provider,
                         int status, Bundle extras) {
                        // TODO Auto-generated method stub
                            //Toast.makeText( getApplicationContext(),"Status change" , Toast.LENGTH_SHORT).show();

                        }
            };
}

更新:这是我的logcat:

UPDATE: Here is my logcat :

   05-28 15:42:46.375: E/AndroidRuntime(5585): FATAL EXCEPTION: main
   05-28 15:42:46.375: E/AndroidRuntime(5585): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.package/com.package.TestRun}: java.lang.IllegalArgumentException: provider=network
   05-28 15:42:46.375: E/AndroidRuntime(5585):  at  android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
   05-28 15:42:46.375: E/AndroidRuntime(5585):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
   05-28 15:42:46.375: E/AndroidRuntime(5585):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
   05-28 15:42:46.375: E/AndroidRuntime(5585):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
   05-28 15:42:46.375: E/AndroidRuntime(5585):  at android.os.Handler.dispatchMessage(Handler.java:99)
   05-28 15:42:46.375: E/AndroidRuntime(5585):  at android.os.Looper.loop(Looper.java:137)
   05-28 15:42:46.375: E/AndroidRuntime(5585):  at android.app.ActivityThread.main(ActivityThread.java:4340)
   05-28 15:42:46.375: E/AndroidRuntime(5585):  at java.lang.reflect.Method.invokeNative(Native Method)
   05-28 15:42:46.375: E/AndroidRuntime(5585):  at java.lang.reflect.Method.invoke(Method.java:511)
   05-28 15:42:46.375: E/AndroidRuntime(5585):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
   05-28 15:42:46.375: E/AndroidRuntime(5585):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
   05-28 15:42:46.375: E/AndroidRuntime(5585):  at dalvik.system.NativeStart.main(Native Method)
   05-28 15:42:46.375: E/AndroidRuntime(5585): Caused by: java.lang.IllegalArgumentException: provider=network
   05-28 15:42:46.375: E/AndroidRuntime(5585):  at android.os.Parcel.readException(Parcel.java:1331)
   05-28 15:42:46.375: E/AndroidRuntime(5585):  at android.os.Parcel.readException(Parcel.java:1281)
   05-28 15:42:46.375: E/AndroidRuntime(5585):  at android.location.ILocationManager$Stub$Proxy.requestLocationUpdates(ILocationManager.java:646)
   05-28 15:42:46.375: E/AndroidRuntime(5585):  at android.location.LocationManager._requestLocationUpdates(LocationManager.java:582)
   05-28 15:42:46.375: E/AndroidRuntime(5585):  at android.location.LocationManager.requestLocationUpdates(LocationManager.java:446)
   05-28 15:42:46.375: E/AndroidRuntime(5585):  at com.package.TestRun.registerLocationListeners(TestRun.java:191)
   05-28 15:42:46.375: E/AndroidRuntime(5585):  at com.package.TestRun.onCreate(TestRun.java:135)
   05-28 15:42:46.375: E/AndroidRuntime(5585):  at android.app.Activity.performCreate(Activity.java:4465)
    05-28 15:42:46.375: E/AndroidRuntime(5585):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
    05-28 15:42:46.375: E/AndroidRuntime(5585):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)

是否有位置管理器的Andr​​oid 4.0 API的更新?请分享观点/解决方案的链接,如果任何人面临同样的问题。

Is there any update for Location Manager in Android 4.0 API? Please share views/solution links if anyone faced the same problem.

推荐答案

这是一个问题。看

它说:

不要直接使用提供商字符串,因为它不能保证它会存在的在运行时。

Do not use direct provider strings, as it is not guaranteed that it will exist at runtime.

使用仅提供商你从获得 LocationManager ,像这样的:

Use only providers that you get from the LocationManager, like this:

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

    Criteria criteria = new Criteria();
    criteria.setAccuracy( Criteria.ACCURACY_COARSE );
    String provider = locationManager.getBestProvider( criteria, true );

    if ( provider == null ) {
        Log.e( TAG, "No location provider found!" );
        return;
    }

    lastLocation = locationManager.getLastKnownLocation(provider);

locationmanager 另一个有用的方法是 LocationManager.getAllProviders()

Another useful method in locationmanager is LocationManager.getAllProviders()

/**
 * Returns a list of the names of all known location providers.  All
 * providers are returned, including ones that are not permitted to be
 * accessed by the calling activity or are currently disabled.
 *
 * @return list of Strings containing names of the providers
 */

这篇关于位置管理器不工作的Andr​​oid 4.0.2模拟器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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