getBestProvider总是返回null问题的解决之道

本文介绍了解决Android应用中LocationManager.getBestProvider方法返回null的问题,主要原因是权限配置错误。通过调整权限配置的位置,确保其放置在</application>标签外,可以有效解决此问题。

当getBestProvider总是返回null时, 可能是权限的问题,注意权限语句(uses-permission android:name="android.permission.INTERNET" ...)要放在</application> 之外。

 

正确如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="Google.GMap"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
     <uses-library android:name="com.google.android.maps" />
        <activity android:name="Location"
                  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>
   
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-sdk android:minSdkVersion="8" />
   
</manifest>

逐行解析代码,不要修改和遗漏源代码,去除行号: 1905 /** 1906 * Returns a list of the names of available location providers. If {@code enabledOnly} is false, 1907 * this is functionally the same as {@link #getAllProviders()}. 1908 * 1909 * @param enabledOnly if true then only enabled providers are included 1910 * @return list of provider names 1911 */ 1912 public @NonNull List<String> getProviders(boolean enabledOnly) { 1913 try { 1914 return mService.getProviders(null, enabledOnly); 1915 } catch (RemoteException e) { 1916 throw e.rethrowFromSystemServer(); 1917 } 1918 } 1919 1920 /** 1921 * Returns a list of the names of available location providers that satisfy the given criteria. 1922 * 1923 * @param criteria the criteria that providers must match 1924 * @param enabledOnly if true then only enabled providers are included 1925 * @return list of provider names 1926 * 1927 * @throws IllegalArgumentException if criteria is null 1928 * 1929 * @deprecated Criteria based APIs are deprecated, prefer to select a provider explicitly. 1930 */ 1931 @Deprecated 1932 public @NonNull List<String> getProviders(@NonNull Criteria criteria, boolean enabledOnly) { 1933 Preconditions.checkArgument(criteria != null, "invalid null criteria"); 1934 1935 try { 1936 return mService.getProviders(criteria, enabledOnly); 1937 } catch (RemoteException e) { 1938 throw e.rethrowFromSystemServer(); 1939 } 1940 } 1941 1942 /** 1943 * Returns the name of the provider that best meets the given criteria. Only providers that are 1944 * permitted to be accessed by the caller will be returned. If several providers meet the 1945 * criteria, the one with the best accuracy is returned. If no provider meets the criteria, the 1946 * criteria are loosened in the following order: 1947 * 1948 * <ul> 1949 * <li> power requirement 1950 * <li> accuracy 1951 * <li> bearing 1952 * <li> speed 1953 * <li> altitude 1954 * </ul> 1955 * 1956 * <p> Note that the requirement on monetary cost is not removed in this process. 1957 * 1958 * @param criteria the criteria that need to be matched 1959 * @param enabledOnly if true then only enabled providers are included 1960 * @return name of the provider that best matches the criteria, or null if none match 1961 * 1962 * @throws IllegalArgumentException if criteria is null 1963 * 1964 * @deprecated Criteria based APIs are deprecated, prefer to select a provider explicitly. 1965 */ 1966 @Deprecated 1967 public @Nullable String getBestProvider(@NonNull Criteria criteria, boolean enabledOnly) { 1968 Preconditions.checkArgument(criteria != null, "invalid null criteria"); 1969 1970 try { 1971 return mService.getBestProvider(criteria, enabledOnly); 1972 } catch (RemoteException e) { 1973 throw e.rethrowFromSystemServer(); 1974 } 1975 } 1976 1977 /** 1978 * Returns the information about the location provider with the given name, or null if no 1979 * provider exists by that name. 1980 * 1981 * @param provider a provider listed by {@link #getAllProviders()} 1982 * @return location provider information, or null if provider does not exist 1983 * 1984 * @throws IllegalArgumentException if provider is null 1985 * 1986 * @deprecated This method has no way to indicate that a provider's properties are unknown, and 1987 * so may return incorrect results on rare occasions. Use {@link #getProviderProperties(String)} 1988 * instead. 1989 */ 1990 @Deprecated 1991 public @Nullable LocationProvider getProvider(@NonNull String provider) { 1992 Preconditions.checkArgument(provider != null, "invalid null provider"); 1993 1994 if (!Compatibility.isChangeEnabled(GET_PROVIDER_SECURITY_EXCEPTIONS)) { 1995 if (NETWORK_PROVIDER.equals(provider) || FUSED_PROVIDER.equals(provider)) { 1996 try { 1997 mContext.enforcePermission(ACCESS_FINE_LOCATION, Process.myPid(), 1998 Process.myUid(), null); 1999 } catch (SecurityException e) { 2000 mContext.enforcePermission(ACCESS_COARSE_LOCATION, Process.myPid(), 2001 Process.myUid(), null); 2002 } 2003 } else { 2004 mContext.enforcePermission(ACCESS_FINE_LOCATION, Process.myPid(), Process.myUid(), 2005 null); 2006 } 2007 } 2008 2009 try { 2010 ProviderProperties properties = mService.getProviderProperties(provider); 2011 return new LocationProvider(provider, properties); 2012 } catch (IllegalArgumentException e) { 2013 // provider does not exist 2014 return null; 2015 } catch (RemoteException e) { 2016 throw e.rethrowFromSystemServer(); 2017 } 2018 } 2019 2020 /** 2021 * Returns the properties of the given provider, or null if the properties are currently 2022 * unknown. Provider properties may change over time, although this is discouraged, and should 2023 * be rare. The most common transition is when provider properties go from being unknown to 2024 * known, which is most common near boot time. 2025 * 2026 * @param provider a provider listed by {@link #getAllProviders()} 2027 * @return location provider properties, or null if properties are currently unknown 2028 * 2029 * @throws IllegalArgumentException if provider is null or does not exist 2030 */ 2031 public @Nullable ProviderProperties getProviderProperties(@NonNull String provider) { 2032 Preconditions.checkArgument(provider != null, "invalid null provider"); 2033 2034 try { 2035 return mService.getProviderProperties(provider); 2036 } catch (RemoteException e) { 2037 throw e.rethrowFromSystemServer(); 2038 } 2039 }
最新发布
12-30
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值