我面臨的問題是通過我寫的服務爲應用程序授予gps權限。 我試圖找到一個例子來實現這一點,例子顯示在Activity中寫一個gpslocation finder,但在這種情況下,我想實現這個服務。requestPermissions()爲服務中的gps。錯誤:「位置提供程序需要ACCESS_FINE_LOCATION權限。」
服務代碼如下:
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:
Stack Overflow是編程問題。你的問題是什麼? – CommonsWare
發佈您的清單 –
我發佈了我的manifest.xml代碼 –