2016-04-21 53 views
-1

我面臨的問題是通過我寫的服務爲應用程序授予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:

+1

Stack Overflow是編程問題。你的問題是什麼? – CommonsWare

+0

發佈您的清單 –

+0

我發佈了我的manifest.xml代碼 –

回答

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 
       } 
      } 
     } 
    } 
+0

我正在檢查權限onCreate()它是否正確。所以,通過該服務運行它應該有權限。但我想要的是如何在服務中的onCreate()方法中提供權限。 –

+0

我以爲你知道如何做到這一點,但你註釋掉了代碼併發布了它。更新了答案。 –

+0

當我在服務中使用ActivityCompat.requestPermissions()時,它期待第一個參數作爲一個活動並顯示如下錯誤:https://lh6.googleusercontent.com/-iV4t1YB_hZU/Vxpow_fqGxI/AAAAAAAAEQU/3Wzup0D-02o7ys00VopOYg4ov9v1SfsOwCL0B/w739- h229-no/error1.png –

相關問題