开发者

Can getBestProvider ever return null?

In the following code snippet:

 Criteria criteria = new Criteria();
 Criteria.setAccuracy(Criteria.ACCURACY_COARSE);
 criteria.setAltitudeRequired(false);
 criteria.setBearingRequired(false);        
 criteria.setCostAllowed(true);
 criteria开发者_StackOverflow社区.setPowerRequirement(Criteria.NO_REQUIREMENT);         
 String provider = DP.lm.getBestProvider(criteria, true);

I've tried to keep the criteria as broad as possible to get any GPS location provider available at all. I need to detect the case when none are available even then.

How can this be handled? Will the getBestProvider call ever return null in such a case?


The documentation says that:

Returns the name of the provider that best meets the given criteria. Only providers that are permitted to be accessed by the calling activity will be returned. If several providers meet the criteria, the one with the best accuracy is returned. If no provider meets the criteria, the criteria are loosened in the following sequence: power requirement, accuracy, bearing, speed, altitude

Note that the requirement on monetary cost is not removed in this process.

So it is still possible that you cant find a provider that fulfills all your requirements. In this case, null is returned. This is not in the documentation, but I can valid it from my personal experience.

In order to avoid that, you can address a Provider directly, if the method getBestProvider returns null. So you have a backup.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜