のために実行した後にnullを行きます(Gattイベントを処理するために)privateメンバー変数としてBroadcastReceiverを使用すると、サービスはstartServiceコマンドを使用して開始されます。次に、私は現在の活動の中でそのサービスにバインドします。 onStartCommandの中で、onStartCommandインテントの余分なものとして渡されたBluetoothDeviceオブジェクト(my_device)をサービスのメンバー変数に割り当てます。しかし、サービスが実行され、メンバ変数が割り当てられた後、何とかそのメンバ変数のインスタンスが失われ、BLE Gattイベントを処理するBroadcastReceiverで何かをしようとすると再びnullになります。Androidのサービスのメンバ変数は、私はBluetoothデバイスと通信するためのフォアグラウンドサービスを作成しようとしていると私は、この構造を使用して行ってきたいくつかの時間
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;
}
あなたは 'My_BLEService'の新しいインスタンスを取得していませんか? –
いいえサービスライフサイクルの仕組みがわかりません。それはゴミ箱と新しいインスタンスを作成しますか? – JBaczuk
いいえアイデア - アンドロイドアプリを書かなかった(おもちゃの例を超えて)。これは、null以外の値になった後に 'my_device'がnullになるのを見ることができる唯一の方法です。 –