java.lang.SecurityException:“ gps”位置提供程序需要ACCESS_FINE_LOCATION权限

时间:2018-12-03 18:42:10

标签: android android-permissions android-gps

我正在尝试制作一个可检索gps信号的应用程序(也在屏幕关闭时),但是遇到了问题。我收到以下错误:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: bluetooth.study.com.bluetooth, PID: 32011
              java.lang.RuntimeException: Unable to create service bluetooth.study.com.bl_test_f.gpsLogic.GPSService: java.lang.SecurityException: "gps" location provider requires ACCESS_FINE_LOCATION permission.
                  at android.app.ActivityThread.handleCreateService(ActivityThread.java:3568)
                  at android.app.ActivityThread.-wrap4(Unknown Source:0)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1812)
                  at android.os.Handler.dispatchMessage(Handler.java:106)
                  at android.os.Looper.loop(Looper.java:164)
                  at android.app.ActivityThread.main(ActivityThread.java:7000)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
               Caused by: java.lang.SecurityException: "gps" location provider requires ACCESS_FINE_LOCATION permission.
                  at android.os.Parcel.readException(Parcel.java:2029)
                  at android.os.Parcel.readException(Parcel.java:1975)
                  at android.location.ILocationManager$Stub$Proxy.requestLocationUpdates(ILocationManager.java:784)
                  at android.location.LocationManager.requestLocationUpdates(LocationManager.java:888)
                  at android.location.LocationManager.requestLocationUpdates(LocationManager.java:470)
                  at bluetooth.study.com.bl_test_f.gpsLogic.GPSService.onCreate(GPSService.java:65)
                  at android.app.ActivityThread.handleCreateService(ActivityThread.java:3558)
                  at android.app.ActivityThread.-wrap4(Unknown Source:0) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1812) 
                  at android.os.Handler.dispatchMessage(Handler.java:106) 
                  at android.os.Looper.loop(Looper.java:164) 
                  at android.app.ActivityThread.main(ActivityThread.java:7000) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408) 

我的课程(相关)如下:

(主要活动:)

公共类MainActivity扩展了AppCompatActivity {

private Context context;
private static MainActivity ins;
public final String TAG = this.getClass().getSimpleName();
public static ArrayList<ScannedDevice> scannedDevices;
public static ArrayList<String> scannedMacAddresses;
public Button buttonStartScan;

AlarmManager alarmManager;
Intent intent;
PendingIntent pendingIntent;

BluetoothScanner bluetoothScanner;
AlarmReceiver alarmReceiver;
public static CustomAdapter arrayAdapter;

public ListView listView;
LocationManager logManager;
private BroadcastReceiver broadcastReceiver;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ins = this;
    buttonStartScan = (Button)findViewById(R.id.button2);
    buttonStartScan.setEnabled(false);

    /*
    Man braucht die Referenzen, um später von anderen Klassen darauf zugreifen zu können
     */
    if(!runtime_permissions()){
        start_scan();
    }

}

public void start_scan(){
    buttonStartScan.setEnabled(true);
    buttonStartScan.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            start_scan();
            scannedDevices = new ArrayList<>();
            scannedMacAddresses = new ArrayList<>();


            bluetoothScanner = new BluetoothScanner(getApplicationContext());

            configureSchedule();
            bluetoothScanner.initializeScanner();
            Log.e(TAG, "Scanner has been initialized");

            listView = (ListView) findViewById(R.id.listView);
            arrayAdapter = new CustomAdapter(getApplicationContext(), scannedDevices, scannedMacAddresses);
            listView.setAdapter(arrayAdapter);

            bluetoothScanner.scanFinished();
            Log.e(TAG, "Scan was finished");
        }
    });

    Intent i = new Intent(getApplicationContext(), GPSService.class);
    startService(i);

}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    Log.e(TAG, "OnResuestPermissonsResult is called");
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if(requestCode == 100){
        if( grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED){
            start_scan();
        }else {
            runtime_permissions();
        }
    }
}

protected void onDestroy() {
    Toast.makeText(this, "On Destroy", Toast.LENGTH_SHORT).show();
    if (bluetoothScanner.bluetoothAdapter.isDiscovering()) {
        bluetoothScanner.bluetoothAdapter.cancelDiscovery();
    }
    unregisterReceiver(bluetoothScanner.broadcastReceiver);

    if(broadcastReceiver != null){
        unregisterReceiver(broadcastReceiver);
    }

    super.onDestroy();
}

public Context getContext() {
    return context;
}

public void configureSchedule() {
    AlarmReceiver alarmReceiver = new AlarmReceiver();
    alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    intent = new Intent(this, AlarmReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, intent, 0);

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000, pendingIntent);
    Log.e(TAG, "Scheduler configured");
    Toast.makeText(this, "Alarm set in 10 seconds for every 60 seconds", Toast.LENGTH_LONG).show();
}

private boolean runtime_permissions() {
    if(Build.VERSION.SDK_INT >= 23 && ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED){

        requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION},100);

        return true;
    }
    return false;
}

public static void addDevice(ScannedDevice sd) {
    if (/*scannedDevices.size() == 0 ||*/ !scannedMacAddresses.contains(sd.getMacAddress())) {
        scannedMacAddresses.add(sd.getMacAddress());
        /*scannedDevices.add(sd);*/
        arrayAdapter.add(sd);
        Log.e("MainActivity", "Device added");
    }
}

public static MainActivity getIns() {
    return ins;
}

}

和GPSServices.java

public class GPSService extends Service {
private LocationListener locListener;
private LocationManager locMananger;

public GPSService() {

}

@Override
public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.
    return null;
}

@SuppressLint("MissingPermission")
public void onCreate() {
    locListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {

            Intent i = new Intent("location_update");
            i.putExtra("longitude", location.getLongitude());
            i.putExtra("latitude", location.getLatitude());
            sendBroadcast(i);
        }

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

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {
            Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(i);
        }
    };
    locMananger = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
    locMananger.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 0, locListener);



}

public void onDestroy(){
    super.onDestroy();
    if(locMananger != null){
        locMananger.removeUpdates(locListener);
    }
}

}

当然还有mainfest文件

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.SET_ALARM" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />


<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".gui.MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver android:name=".bluetoothLogic.AlarmReceiver" />

    <service
        android:name=".gpsLogic.GPSService"
        android:enabled="true"
        android:exported="false"></service>
</application>

我找不到我的错误,有人可以帮我吗?

0 个答案:

没有答案