2017-06-12 35 views
0

我试图创建一个应用程序来扫描信标。前台应用程序工作正常。但是,当我尝试在应用程序中包含后台扫描时,会导致应用程序崩溃。基本上,一旦应用程序启动,应用程序要求权限,关闭并显示后台扫描通知。所以,每次后台服务检测到信标时,都会关闭应用程序。Android信标库后台扫描导致前台应用程序崩溃

我想要做的是以某种方式停止后台扫描,当应用程序进入前台。我试过使用

regionbootstrap.disable(); 

但它不能解决我的问题。

我使用的是Android的灯塔库2.11-β1

public class whereismybag extends Application implements BootstrapNotifier, 
RangeNotifier, BeaconConsumer { 
private static final String TAG = "wherebg"; 
private RegionBootstrap regionBootstrap; 
private BackgroundPowerSaver backgroundPowerSaver; 
BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this); 

@Override 
public void onCreate() { 
    super.onCreate(); 



    Log.d(TAG, "Background scanning started up"); 
    Region region = new Region("com.example.utsav.whereismybeacon", null, null, null); 
    regionBootstrap = new RegionBootstrap(this, region); 

    // To detect proprietary beacons, you must add a line like below corresponding to your beacon 
    // type. Do a web search for "setBeaconLayout" to get the proper expression. 
    beaconManager.getBeaconParsers().add(new BeaconParser(). 
      setBeaconLayout(BeaconParser.EDDYSTONE_UID_LAYOUT)); 

    // wake up the app when any beacon is seen (you can specify specific id filers in the parameters below) 


    backgroundPowerSaver = new BackgroundPowerSaver(this); 
    beaconManager.setBackgroundScanPeriod(5000L); 
    beaconManager.setBackgroundBetweenScanPeriod(6000L);} 




private void sendNotification(String text) { 
    NotificationCompat.Builder builder = 
      (NotificationCompat.Builder) new NotificationCompat.Builder(this) 
        .setContentTitle("Where is My bag?") 
        .setContentText(text) 
        .setSmallIcon(R.drawable.wimbicon); 

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); 
    stackBuilder.addNextIntent(new Intent(this, MainActivity.class)); 
    PendingIntent resultPendingIntent = 
      stackBuilder.getPendingIntent(
        0, 
        PendingIntent.FLAG_UPDATE_CURRENT 
      ); 
    builder.setContentIntent(resultPendingIntent); 
    NotificationManager notificationManager = 
      (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); 
    notificationManager.notify(1, builder.build()); 
} 

public void didEnterRegion(Region region) { 
    Log.d(TAG, "Got a didEnterRegion call"); 
    // This call to disable will make it so the activity below only gets launched the first time a beacon is seen (until the next time the app is launched) 
    // if you want the Activity to launch every single time beacons come into view, remove this call. 
    regionBootstrap.disable(); 
    try { 
     beaconManager.startRangingBeaconsInRegion(region); 
    } 
    catch (RemoteException e) { 
     if (BuildConfig.DEBUG) Log.d(TAG, "Can't start ranging"); 
    } 
    sendNotification("Detecting a bag! Tap to launch app."); 
    regionBootstrap.disable(); 
    try { 
     beaconManager.startRangingBeaconsInRegion(region); 

    } catch (RemoteException e) { 
     e.printStackTrace(); 
    } 
} 
    /*Intent intent = new Intent(whereismybag.this, MainActivity.class); 
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    this.startActivity(intent);*/ 


@Override 
public void didExitRegion(Region region) { 
    try { 
     beaconManager.stopRangingBeaconsInRegion(region); 
    } catch (RemoteException e) { 
     e.printStackTrace(); 
    } 
} 


@Override 
public void didDetermineStateForRegion(int state, Region region) { 
    Log.d(TAG,"I have just switched from seeing/not seeing beacons: " + state); 
} 



public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) { 
    Log.d(TAG,"Now finding beacon range"); 

} 

public void onBeaconServiceConnect() { 
    beaconManager.setRangeNotifier(this); 
}} 

上面的类使用启动:

android:name=".whereismybag" 

整个清单:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.utsav.whereismybag"> 

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> 
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.INTERNET"/> 

<application 
    android:name=".whereismybag" 
    android:allowBackup="true" 
    android:icon="@drawable/wimbicon" 
    android:label="@string/app_name" 
    android:roundIcon="@drawable/wimbicon" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 

    <!-- android:name=".whereismybag" --> 

    <activity 
     android:name=".MainActivity" 
     android:launchMode="singleInstance" 
     android:screenOrientation="portrait" /> 
    <activity 
     android:name=".splashscreen" 
     android:screenOrientation="portrait"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity 
     android:name=".scanning" 
     android:launchMode="singleInstance" 
     android:parentActivityName=".MainActivity" 
     android:screenOrientation="portrait" /> 
    <activity 
     android:name=".beaconfound" 
     android:launchMode="singleInstance" 
     android:parentActivityName=".MainActivity" 
     android:screenOrientation="portrait" /> 
    <activity 
     android:name=".save" 
     android:launchMode="singleInstance" 
     android:parentActivityName=".MainActivity" 
     android:screenOrientation="portrait" /> 
    <activity 
     android:name=".viewsaved" 
     android:launchMode="singleInstance" 
     android:parentActivityName=".MainActivity" 
     android:screenOrientation="portrait" /> 
    <activity 
     android:name=".findingbags" 
     android:launchMode="singleInstance" 
     android:parentActivityName=".MainActivity" 
     android:screenOrientation="portrait" /> 
    <activity 
     android:name=".bagfound" 
     android:launchMode="singleTask" 
     android:parentActivityName=".MainActivity" 
     android:screenOrientation="portrait" /> 
    <activity 
     android:name=".setrange" 
     android:launchMode="singleInstance" 
     android:parentActivityName=".MainActivity" 
     android:screenOrientation="portrait" /> 
    <activity android:name=".beaconfoundlist"></activity> 
</application> 

这是在MainActivity:

public class MainActivity extends AppCompatActivity { 
Button button; 

/*SharedPreferences preferences = getApplicationContext().getSharedPreferences("com.utsav.whereismybag", 0);*/ 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 



    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
     if (checkSelfPermission(Manifest.permission.BLUETOOTH_ADMIN) != PackageManager.PERMISSION_GRANTED) { 
      ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.BLUETOOTH_ADMIN}, 1); 
     } 
     if (checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 1); 
     } 
     if (checkSelfPermission(Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED) { 
      ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.INTERNET}, 1); 
     } 
    } 

    boolean gps_enabled = false; 
    boolean bluetoothadmin_enabled = false; 
    //checking for active location 
    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    try { 
     gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER); 
    } catch (Exception ex) { 
    } 
    try { 
     bluetoothadmin_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 
    } catch (Exception ex) { 
    } 
    if (!gps_enabled && !bluetoothadmin_enabled) { 
     android.app.AlertDialog.Builder dialog = new android.app.AlertDialog.Builder(this); 
     dialog.setMessage(getResources().getString(R.string.gps_network_not_enabled)); 
     dialog.setPositiveButton(getResources().getString(R.string.open_location_settings), new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface paramDialogInterface, int paramInt) { 
       Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
       startActivity(myIntent); 
      } 
     }); 
     dialog.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() { 

      @Override 
      public void onClick(DialogInterface paramDialogInterface, int paramInt) { 
       // TODO Auto-generated method stub 

      } 
     }); 
     dialog.show(); 
    } 
    //checking for active bluetooth 
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
    if (mBluetoothAdapter == null) { 
     android.app.AlertDialog.Builder dialog = new android.app.AlertDialog.Builder(this); 
     dialog.setMessage("This device does not support Bluetooth"); 
     // Device does not support Bluetooth 
    } else { 
     if (!mBluetoothAdapter.isEnabled()) { 
      // Bluetooth is not enabled 
      android.app.AlertDialog.Builder dialog = new android.app.AlertDialog.Builder(this); 
      dialog.setMessage(getResources().getString(R.string.bluetooth_not_enabled)); 
      dialog.setPositiveButton(getResources().getString(R.string.open_bluetooth_settings), new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface paramDialogInterface, int paramInt) { 
        Intent myIntent = new Intent(Settings.ACTION_BLUETOOTH_SETTINGS); 
        startActivity(myIntent); 
       } 
      }); 
      dialog.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() { 

       @Override 
       public void onClick(DialogInterface paramDialogInterface, int paramInt) { 
        // TODO Auto-generated method stub 

       } 
      }); 
      dialog.show(); 
     } 
    } 

    button = (Button) findViewById(R.id.setupbags); 
    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent myIntent1 = new Intent(MainActivity.this, 
        scanning.class); 
      startActivity(myIntent1); 
     } 
    });//Start scanning 
    button = (Button) findViewById(R.id.savedbeac); 
    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent myIntent1 = new Intent(MainActivity.this, 
        viewsaved.class); 
      startActivity(myIntent1); 
     } 
    });//Start scanning 
    button = (Button) findViewById(R.id.findbags); 
    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent myIntent1 = new Intent(MainActivity.this, 
        findingbags.class); 
      startActivity(myIntent1); 
     } 
    });//Start scanning 
    button = (Button) findViewById(R.id.setrange); 
    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent myIntent1 = new Intent(MainActivity.this, 
        setrange.class); 
      startActivity(myIntent1); 
     } 
    });//Start scanning 
}} 

这里是日志:

06-12 11:20:42.616 26716-26716/com.example.utsav.whereismybag D/.wherebg: 
Background scanning started up 
06-12 11:20:42.624 26716-26716/com.example.utsav.whereismybag 
I/BeaconManager: BeaconManager started up on pid 26716 named 
'com.example.utsav.whereismybag' for application package 
'com.example.utsav.whereismybag'. isMainProcess=true 
06-12 11:20:42.629 26716-26716/com.example.utsav.whereismybag 
D/BeaconParser: Parsing beacon layout: m:2-3=beac,i:4-19,i:20-21,i:22- 
23,p:24-24,d:25-25 
06-12 11:20:42.635 26716-26716/com.example.utsav.whereismybag 
D/BeaconParser: Parsing beacon layout: s:0-1=feaa,m:2-2=00,p:3-3:-41,i:4- 
13,i:14-19 
06-12 11:20:42.790 26716-26753/com.example.utsav.whereismybag D/AppTracker: 
App Event: start 
06-12 11:20:42.811 26716-26716/com.example.utsav.whereismybag 
W/BluetoothCrashResolver: Can't read macs from 
BluetoothCrashResolverState.txt 
06-12 11:20:42.813 26716-26716/com.example.utsav.whereismybag 
I/CycledLeScanner: This Android 5.0. We are using new scanning APIs 
06-12 11:20:42.815 26716-26716/com.example.utsav.whereismybag 
I/BeaconService: beaconService version 2.11-beta1 is starting up on the main 
process 
06-12 11:20:42.822 26716-26756/com.example.utsav.whereismybag 
I/DpmTcmClient: RegisterTcmMonitor from: 
com.android.okhttp.TcmIdleTimerMonitor 
06-12 11:20:42.825 26716-26756/com.example.utsav.whereismybag 
D/NetworkSecurityConfig: No Network Security Config specified, using 
platform default 
06-12 11:20:42.843 26716-26716/com.example.utsav.whereismybag 
W/ModelSpecificDistanceCalculator: Cannot find match for this device. Using default 
06-12 11:20:42.843 26716-26716/com.example.utsav.whereismybag 
W/ModelSpecificDistanceCalculator: Cannot find match for this device. Using default 
06-12 11:20:42.846 26716-26716/com.example.utsav.whereismybag I/BeaconService: starting with intent Intent { cmp=com.example.utsav.whereismybag/org.altbeacon.beacon.service.BeaconService } 
06-12 11:20:42.846 26716-26716/com.example.utsav.whereismybag I/BeaconService: binding 
06-12 11:20:42.857 26716-26754/com.example.utsav.whereismybag I/Adreno: QUALCOMM build     : 853a1ff, I9c435c2712 
                    Build Date      : 01/10/17 
                    OpenGL ES Shader Compiler Version: XE031.09.00.04 
                    Local Branch      : 
                    Remote Branch     : 
                    Remote Branch     : 
                    Reconstruct Branch    : 
06-12 11:20:42.861 26716-26754/com.example.utsav.whereismybag I/OpenGLRenderer: Initialized EGL, version 1.4 
06-12 11:20:42.861 26716-26754/com.example.utsav.whereismybag D/OpenGLRenderer: Swap behavior 1 
06-12 11:20:42.867 26716-26716/com.example.utsav.whereismybag D/.wherebg: I have just switched from seeing/not seeing beacons: 0 
06-12 11:20:42.946 26716-26716/com.example.utsav.whereismybag I/BeaconService: start monitoring received 
06-12 11:20:44.166 26716-26756/com.example.utsav.whereismybag I/ModelSpecificDistanceCalculator: Successfully saved new distance model file 
06-12 11:20:44.167 26716-26756/com.example.utsav.whereismybag W/ModelSpecificDistanceCalculator: Cannot find match for this device. Using default 
06-12 11:20:44.167 26716-26756/com.example.utsav.whereismybag I/ModelSpecificDistanceCalculator: Successfully updated distance model with latest from online database 
06-12 11:20:45.799 26716-26819/com.example.utsav.whereismybag D/AppTracker: App Event: stop 
06-12 11:20:45.860 26716-26716/com.example.utsav.whereismybag I/BeaconService: set scan intervals received 
06-12 11:20:45.991 26716-26716/com.example.utsav.whereismybag W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable 
06-12 11:20:46.021 26716-26716/com.example.utsav.whereismybag V/BoostFramework: mAcquireFunc method = public int com.qualcomm.qti.Performance.perfLockAcquire(int,int[]) 
06-12 11:20:46.021 26716-26716/com.example.utsav.whereismybag V/BoostFramework: mReleaseFunc method = public int com.qualcomm.qti.Performance.perfLockRelease() 
06-12 11:20:46.021 26716-26716/com.example.utsav.whereismybag V/BoostFramework: mAcquireTouchFunc method = public int com.qualcomm.qti.Performance.perfLockAcquireTouch(android.view.MotionEvent,android.util.DisplayMetrics,int,int[]) 
06-12 11:20:46.021 26716-26716/com.example.utsav.whereismybag V/BoostFramework: mIOPStart method = public int com.qualcomm.qti.Performance.perfIOPrefetchStart(int,java.lang.String) 
06-12 11:20:46.021 26716-26716/com.example.utsav.whereismybag V/BoostFramework: mIOPStop method = public int com.qualcomm.qti.Performance.perfIOPrefetchStop() 
06-12 11:20:46.023 26716-26716/com.example.utsav.whereismybag V/BoostFramework: BoostFramework() : mPerf = [email protected] 
06-12 11:20:46.023 26716-26716/com.example.utsav.whereismybag V/BoostFramework: BoostFramework() : mPerf = [email protected] 
06-12 11:20:46.120 26716-26753/com.example.utsav.whereismybag D/AppTracker: App Event: start 
06-12 11:20:46.124 26716-26756/com.example.utsav.whereismybag D/AppTracker: App Event: stop 
06-12 11:20:46.137 26716-26716/com.example.utsav.whereismybag I/BeaconService: set scan intervals received 
06-12 11:20:46.149 26716-26716/com.example.utsav.whereismybag I/CycledLeScanner: Adjusted scanStopTime to be 885106541 
06-12 11:20:46.215 26716-26716/com.example.utsav.whereismybag I/BeaconService: set scan intervals received 
06-12 11:20:46.238 26716-26716/com.example.utsav.whereismybag W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView 
06-12 11:20:46.314 26716-26755/com.example.utsav.whereismybag D/BluetoothAdapter: STATE_ON 
06-12 11:20:46.314 26716-26755/com.example.utsav.whereismybag D/BluetoothLeScanner: could not find callback wrapper 
06-12 11:20:46.318 26716-26755/com.example.utsav.whereismybag D/BluetoothAdapter: STATE_ON 
06-12 11:20:46.327 26716-26729/com.example.utsav.whereismybag D/BluetoothLeScanner: onClientRegistered() - status=0 clientIf=5 mClientIf=0 
06-12 11:20:46.335 26716-26729/com.example.utsav.whereismybag W/Binder: Binder call failed. 
                    java.lang.SecurityException: Need ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission to get scan results 
                     at android.os.Parcel.readException(Parcel.java:1684) 
                     at android.os.Parcel.readException(Parcel.java:1637) 
                     at android.bluetooth.IBluetoothGatt$Stub$Proxy.startScan(IBluetoothGatt.java:678) 
                     at android.bluetooth.le.BluetoothLeScanner$BleScanCallbackWrapper.onClientRegistered(BluetoothLeScanner.java:367) 
                     at android.bluetooth.IBluetoothGattCallback$Stub.onTransact(IBluetoothGattCallback.java:56) 
                     at android.os.Binder.execTransact(Binder.java:580) 
06-12 11:20:48.328 26716-26755/com.example.utsav.whereismybag D/BluetoothAdapter: STATE_ON 
06-12 11:20:50.089 26716-26819/com.example.utsav.whereismybag D/AppTracker: App Event: start 
06-12 11:20:50.096 26716-26716/com.example.utsav.whereismybag I/BeaconService: set scan intervals received 
06-12 11:20:50.102 26716-26716/com.example.utsav.whereismybag I/CycledLeScanner: Adjusted scanStopTime to be 885111115 
06-12 11:20:50.555 26716-26755/com.example.utsav.whereismybag D/BluetoothAdapter: STATE_ON 
06-12 11:20:50.560 26716-26728/com.example.utsav.whereismybag D/BluetoothLeScanner: onClientRegistered() - status=0 clientIf=5 mClientIf=0 
06-12 11:20:50.938 26716-26973/com.example.utsav.whereismybag D/.wherebg: I have just switched from seeing/not seeing beacons: 1 
06-12 11:20:50.938 26716-26973/com.example.utsav.whereismybag D/.wherebg: Got a didEnterRegion call 
06-12 11:20:50.959 26716-26716/com.example.utsav.whereismybag I/BeaconService: stop monitoring received 

                      --------- beginning of crash 
06-12 11:20:50.963 26716-26973/com.example.utsav.whereismybag E/AndroidRuntime: FATAL EXCEPTION: IntentService[BeaconIntentProcessor] 
                      Process: com.example.utsav.whereismybag, PID: 26716 
                      java.lang.NullPointerException: Attempt to invoke virtual method 'void org.altbeacon.beacon.BeaconManager.startRangingBeaconsInRegion(org.altbeacon.beacon.Region)' on a null object reference 
                       at com.example.utsav.whereismybag.whereismybag.didEnterRegion(whereismybag.java:91) 
                       at org.altbeacon.beacon.BeaconIntentProcessor.onHandleIntent(BeaconIntentProcessor.java:99) 
                       at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:68) 
                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                       at android.os.Looper.loop(Looper.java:154) 
                       at android.os.HandlerThread.run(HandlerThread.java:61) 
06-12 11:20:50.963 26716-26973/com.example.utsav.whereismybag D/AppTracker: App Event: crash 
06-12 11:20:50.967 26716-26716/com.example.utsav.whereismybag I/BeaconService: unbinding 
06-12 11:20:50.967 26716-26716/com.example.utsav.whereismybag E/BeaconService: onDestroy() 
06-12 11:20:50.967 26716-26755/com.example.utsav.whereismybag D/BluetoothAdapter: STATE_ON 
06-12 11:20:50.969 26716-26716/com.example.utsav.whereismybag I/BeaconService: onDestroy called. stopping scanning 
06-12 11:20:50.980 26716-26753/com.example.utsav.whereismybag D/AppTracker: App Event: stop 
06-12 11:20:50.981 26716-26716/com.example.utsav.whereismybag E/BeaconManager: Cannot contact service to set scan periods 
06-12 11:20:50.984 26716-26753/com.example.utsav.whereismybag E/DeviceManagerUtil: RunningAppProcessInfo is null 
06-12 11:20:50.986 26716-26973/com.example.utsav.whereismybag I/Process: Sending signal. PID: 26716 SIG: 9 
+0

您可以发布日志猫 –

+0

我现在已经添加了日志。 @MuhammadYounas – uns

+0

您正在测试哪个版本 –

回答

0

从logcat中显示它的错误ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION

十一月6日至12日:20:46.335 26716-26729 /com.example.utsav.whereismybag W /活页夹:活页夹调用失败。 java.lang.SecurityException异常:需要ACCESS_COARSE_LOCATION或 ACCESS_FINE_LOCATION许可获得的扫描结果 在android.os.Parcel.readException(Parcel.java:1684) 在android.os.Parcel.readException(Parcel.java:1637) 在 android.bluetooth.IBluetoothGatt $存根$ Proxy.startScan(IBluetoothGatt.java:678) 在 android.bluetooth.le.BluetoothLeScanner $ BleScanCallbackWrapper.onClientRegistered(BluetoothLeScanner.java:367) 在 android.bluetooth.IBluetoothGattCallback $ Stub.onTransact(IBluetoothGattCallback.java:56) at android.os.Bin der.execTransact(Binder.java:580)

为了让你必须使用下面的代码许可

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
<uses-permission android:name="android.permission.INTERNET"/> 


if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == 
     PackageManager.PERMISSION_GRANTED && 
     ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) == 
     PackageManager.PERMISSION_GRANTED) { 
    // {beacon code} 
} else { 
    ActivityCompat.requestPermissions(this, new String[] { 
     Manifest.permission.ACCESS_FINE_LOCATION, 
     Manifest.permission.ACCESS_COARSE_LOCATION }, 
     TAG_CODE_PERMISSION_LOCATION); 
} 

@Override 
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { 
     switch (requestCode) { 
      case TAG_CODE_PERMISSION_LOCATION: { 
       if(grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
        // {beacon code} 
       } 
      } 
     } 
    } 
+0

中的更改介绍我不认为我可以请求权限,因为后台代码不在活动中。这是一堂课。我要求主要和其他所有活动的权限。 我的问题是,如果我在应用程序中包含此背景扫描代码,则会导致前台进程崩溃。否则,如果不包括背景扫描,前景处理就可以正常工作。 – uns