LocationManager调用requestLocationUpdates 某些手机上打包APK无法运行,导致崩溃的问题

如下代码在某些机器上运行时,会直接导致应用崩溃退出

	LocationManager locMgr = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
	Location location = locMgr.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
	updateWithNewLocation(location);  
	locMgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 10, locationListener);

崩溃信息中会出现如下log

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{……}: 
java.lang.IllegalArgumentException: provider=network
	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
	at android.app.ActivityThread.access$600(ActivityThread.java:123)
	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
	at android.os.Handler.dispatchMessage(Handler.java:99)
	at android.os.Looper.loop(Looper.java:137)
	at android.app.ActivityThread.main(ActivityThread.java:4424)
	at java.lang.reflect.Method.invokeNative(Native Method)
	at java.lang.reflect.Method.invoke(Method.java:511)
	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
	at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException: provider=network
	at android.os.Parcel.readException(Parcel.java:1331)
	at android.os.Parcel.readException(Parcel.java:1281)
	at android.location.ILocationManager$Stub$Proxy.requestLocationUpdates(ILocationManager.java:646)
	at android.location.LocationManager._requestLocationUpdates(LocationManager.java:589)
	at android.location.LocationManager.requestLocationUpdates(LocationManager.java:453)
	at com.example.location.LocationActivity.onCreate(LocationActivity.java:26)
	at android.app.Activity.performCreate(Activity.java:4465)
	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
	……


PS:本人自己的log信息已丢失,如上log信息参考地址:

http://stackoverflow.com/questions/11490031/caused-by-java-lang-illegalargumentexception-provider-network

信息参考地址(点此打开链接)


如上log中,红色代码已经标出重要的log信息是这两行行

Caused by: java.lang.IllegalArgumentException: provider=network

……

at android.location.LocationManager.requestLocationUpdates(LocationManager.java:453)

……


由此 我们可以定位到源码中

locMgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 10, locationListener);
这一行出了问题,说是参数不对


然而这个错误并不是每部真机都会出现的

这就意味着那些会报这个错误的手机也许是没有 LocationManager.NETWORK_PROVIDER 这个定位功能


我们用如下代码打印一下可以找到该手机目前有效的provider

	List list = locMgr.getAllProviders();
	for(String c : list) {
		System.out.println("LocationManager provider:" + c);
	}

真机测试,就能从log里看到,当前手机确实是没有network这个provider的


要避免这个问题很简单,只要循环provider的list,发现有network就执行定位部分的代码

如果没有的话,要么换gps要么就算了不定位了

如果连gps也没有的话

还是别给他定位好了


如下是我的处理方法,笨笨的选择如果这部手机没有network的provider,我就不给他定位了

		locMgr = (LocationManager)s_instance.getSystemService(Context.LOCATION_SERVICE);
		List list = locMgr.getAllProviders();
		boolean bfind = false;
		for(String c : list) {
			System.out.println("LocationManager provider:" + c);
			if (c.equals(LocationManager.NETWORK_PROVIDER)){
				bfind = true;
				break;
			}
		}
		
		if (bfind) {
	        	Location location = locMgr.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
	        	updateWithNewLocation(location);  
	        	locMgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 10, locationListener);
		}


你可能感兴趣的:(cocos2d)