requestPermissions()用于服务中的gps。错误:“位置提供程序需要ACCESS_FINE_LOCATION权限。”

时间:2016-04-21 23:16:01

标签: android service permissions android-gps

我面临的问题是通过我正在编写的服务向应用程序提供gps权限。 我试着找到一个例子来实现这一点,并且示例显示在Activity中编写gpslocation finder,但在这种情况下,我想在服务中实现它。

服务代码如下:

public class TravelService extends Service{

    private LocationManager locManager;
    private LocationListener locListener;


    @Override
    public void onCreate() {
        super.onCreate();
        locManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        locListener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                locManager.requestLocationUpdates("gps", 5000, 100, locListener);

            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {

            }

            @Override
            public void onProviderEnabled(String provider) {

            }

            @Override
            public void onProviderDisabled(String provider) {

            }
        };
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                return;
            }
            locManager.requestLocationUpdates("gps", 5000, 100, locListener);

            Toast.makeText(this, "Service Created", Toast.LENGTH_LONG).show();
        }
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        locManager.requestLocationUpdates("gps", 5000, 100, locListener);

        Toast.makeText(this,"Service Started",Toast.LENGTH_LONG).show();
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this,"Service destroyed",Toast.LENGTH_LONG).show();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

}

这是我在Manifest.xml中的代码:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.param.dateandtime">
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!--  Entry for LoginActivity.class -->
        <activity android:name=".LoginActivity"
            android:label="@string/app_name"></activity>
        <activity android:name=".Finish"
            android:label="@string/app_name"></activity>
        <service android:name=".TravelService"
            android:exported="false"/>
    </application>

</manifest>

这里我附上了错误日志,显示了我所面临的错误。我可以看到错误,因为“位置提供程序需要ACCESS_FINE_LOCATION权限”。 Error log:

1 个答案:

答案 0 :(得分:0)

您无法从服务请求权限,因为服务未绑定到UI。 所以,当你startService()时,从活动中做到这一点。

ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1001); //Any number can be used

实施onRequestPermissionsResult()回调

 @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case 1001: {
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED
                        && (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
                        || ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED)) {
                    //Start your service here
                }
            }
        }
    }