Ich versuche, einen Vordergrund-Dienst erstellen mit einem Bluetooth-Gerät zu kommunizieren, und ich habe so mit dieser Struktur getan:Android Service-Membervariable geht null nach einiger Zeit läuft
einen Dienst verwenden, die auch Register ein BroadcastReceiver als private Member-Variable (um Gatt-Ereignisse zu behandeln), dann wird der Dienst mit dem Befehl startService gestartet. Dann binde ich diesen Dienst in meiner aktuellen Aktivität. Innerhalb des onStartCommand weise ich ein BluetoothDevice-Objekt (my_device) zu, das als Extra in der onStartCommand-Intention an eine Mitgliedsvariable im Service übergeben wurde. Nachdem der Dienst ausgeführt und die Mitgliedsvariable zugewiesen wurde, verliert er jedoch irgendwie die Membervariableninstanz und wird wieder null, wenn ich versuche, im BroadcastReceiver, der BLE Gatt Events verarbeitet, etwas zu tun.
public final class My_BLEService extends Service {
private My_BLEDevice my_device = null;
private BroadcastReceiver gattUpdateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Problem: my_device is null inside this function
}
};
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Create the Foreground notification
if (intent.getAction().equals(Constants.ACTION.STARTFOREGROUND_ACTION)) {
// Get BluetoothDevice object
if(this.my_device == null) {
// Initialize the my_device object
BluetoothDevice bleDevice = intent.getParcelableExtra(My_AppManager.RFSTAR);
this.my_device = new my_BLEDevice(bleDevice);
}
// Connect to the bluetooth device first thing:
if(!this.isConnected) {
this.connect();
}
// Start up the broadcast receiver here
if(!this.receiverRegistered) {
registerReceiver(this.gattUpdateReceiver, this.bleIntentFilter());
this.receiverRegistered = true;
}
Intent notificationIntent = new Intent(My_BLEService.this, MonitoringActivity.class);
// FIXME: Necessary? - notificationIntent.putExtra(My_AppManager.RFSTAR, bleDevice);
notificationIntent.setAction(Constants.ACTION.MAIN_ACTION);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Bitmap icon = BitmapFactory.decodeResource(getResources(),
R.drawable.logo_md);
// TODO: Add close button
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("BLE Monitor")
.setTicker("BLE Monitor")
.setContentText("BLE")
// TODO; This isn't appearing correctly the first time
.setSmallIcon(R.drawable.logo_md)
//.setLargeIcon(
// Bitmap.createScaledBitmap(icon, 128, 128, false))
.setContentIntent(pendingIntent)
// Note: this doesn't seem to do anything (setting it to false doesn't change it either)
.setOngoing(true).build();
startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE, notification);
} else if (intent.getAction().equals(Constants.ACTION.STOPFOREGROUND_ACTION)) {
if(this.receiverRegistered) {
unregisterReceiver(this.gattUpdateReceiver);
this.receiverRegistered = false;
}
if(this.isConnected) {
this.disconnect();
}
stopForeground(true);
stopSelf();
}
return START_STICKY;
}
Sind Sie sicher, dass Sie keine neue Instanz von 'My_BLEService' erhalten? –
Nein Ich weiß nicht, wie der Service-Lebenszyklus funktioniert. Wird der Papierkorb gelöscht und neue Instanzen erstellt? – JBaczuk
Keine Ahnung - noch nie eine Android App geschrieben (über Spielzeug Beispiele hinaus). Nur so kann ich sehen, dass "mein_Gerät" null ist, nachdem es nicht null ist. –