运行一段时间后,Android Service成员变量为null

时间:2016-04-14 13:39:10

标签: java android service bluetooth bluetooth-lowenergy

我试图创建一个与蓝牙设备通信的前台服务,并且我已经使用这种结构这样做了:

使用服务,该服务还将BroadcastReceiver注册为私有成员变量(用于处理Gatt事件),然后使用startService命令启动服务。然后,我绑定到当前Activity中的该服务。在onStartCommand中,我分配了一个BluetoothDevice对象(my_device),该对象在onStartCommand intent中作为额外传递给服务中的成员变量。但是,在服务运行并且分配了成员变量之后,它会以某种方式失去该成员变量实例,并且当我尝试在处理BLE Gatt事件的BroadcastReceiver中执行任何操作时它再次变为null。

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;
}

1 个答案:

答案 0 :(得分:0)

好吧,我自己创造了这个问题。问题是,在我的服务中,我正在重置onBind方法中的成员变量my_device:

public IBinder onBind(Intent intent) {
    this.my_device = intent.getParcelableExtra(Kidpod_AppManager.RFSTAR);
    return this.kBinder;
}

在活动中调用bindService方法时没有设置额外的,因此它将其设置为null。