当前位置: 首页 > 知识库问答 >
问题:

Android信标库后台扫描导致前台应用崩溃

晋奕
2023-03-14

我试图创建一个应用程序扫描信标。前台应用程序工作得很好。然而,当我试图在应用程序中包含后台扫描时,它会导致应用程序崩溃。基本上,一旦应用程序启动,应用程序要求权限,关闭,后台扫描通知就会出现。所以,每次后台服务检测到一个信标,它就关闭应用程序。

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

regionbootstrap.disable();
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>

这是主要活动:

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 = com.qualcomm.qti.Performance@df677b
06-12 11:20:46.023 26716-26716/com.example.utsav.whereismybag V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@4c8b998
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

共有1个答案

仰欣悦
2023-03-14

在logcat中,它显示错误access_coarse_location或access_fine_location

06-12 11:20:46.335 267 16-26729/com.example.utsav.WhereisMyBag W/Binder:Binder调用失败。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.iBluetooth.iBluetooth.java:1637)在Android.bluetooth.libuetoothgatt$stub$proxy.startscan(iBluetoothgatt.java:678)在在Android.os.binder.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}
                }
            }
        }
    }
 类似资料:
  • 我可以在Android 10上使用前台服务在后台进行工作BLE扫描。我的应用程序具有in-manifest Foreground\u服务,并在启动时从用户那里获得premissions ACCESS\u FINE\u位置和ACCESS\u background\u位置。然后用BLE扫描启动前台服务。当屏幕在服务上执行扫描和查找设备时,但当屏幕离开前台时,服务停止工作,扫描停止。 正在启动服务: 使

  • 如果我的应用在后台运行(服务),我偶然发现了一个接收GCM消息的问题。在我的场景中,我没有收到GCM消息(请注意,这不是关于如何接收GCM,就像这里一样),并且ActiveManager会杀死应用程序。所以我想知道我是否有概念上的误解,或者这是一个普遍的问题。 出身背景 我有一个在前台(活动)和后台(服务)运行的Android应用程序。应用程序将附加到持久性通知,以确保即使用户打开 Android

  • 我想从标准输入中读取一个双数,但是我总是得到这个异常: Java . util . inputmismatchexception 如果输入是整数,那没问题,但是如果是双精度的,我会得到异常。 线程“main”Java . util . inputmismatchexception Java . util . scanner . throw for(scanner . Java:864)Java .

  • 问题内容: 我正在开发一个Android 3.1应用程序,该应用程序使用USB主机模式通过USB上的MIDI与我的键盘(Korg M3)进行通信。这是在装有Android 4.0.3的Xoom上运行的。我可以通过USB接收MIDI消息而没有任何问题,但是将音符数据发送回键盘的效果是好坏参半,延迟半秒钟后便会频繁崩溃。 这是我在点击操作栏上的按钮发送注释时不断遇到的错误: E / dalvikvm(

  • 我正在开发一个应用程序,提供后台信标监测。我想开始范围时,用户进入信标从定义的区域。当应用程序在后台,它一直在监控,用户进入我定义的区域,我想开始测距,并获得InstanceID或主要,次要值,以确定什么信标是,连接到服务器,并发送给用户一个通知。如果我能在后台与服务器通信,那就最好了。我使用以下示例实现后台监控:https://altbeacon.github.io/android-beacon

  • 本文向大家介绍Android应用程序转到后台并回到前台判断方法,包括了Android应用程序转到后台并回到前台判断方法的使用技巧和注意事项,需要的朋友参考一下 我们知道,当我们按返回或Home键退出应用程序的界面时,应用程序会在后台被挂起。这么设计的好处是,由于应用被系统缓存在内存中,那么在用户打开启动应用时就可以通过调用缓存,快速启动应用。当然,由于当今内存和处理器能力的限制,系统会自动关闭一些