活动 - 使用Realm服务不一致的数据

时间:2016-05-24 10:41:46

标签: android android-service realm

我在存储在Realm数据库中的数据存在问题。如果我在MainActivity上更改了一个值,当我尝试从NotificationListenerService读取它时,数​​据是不正确的。

这是我的模特:

public class Model extends RealmObject {

    String name;
    boolean checked;


    public Model() {}

    public Model(String name, boolean checked) {
        this.name = name;
        this.checked = checked;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isChecked() {
        return checked;
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
    }

}

我从Application类填充数据库:

public class ExampleApplication extends Application{

    @Override
    public void onCreate() {
        super.onCreate();
        RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(getApplicationContext()).deleteRealmIfMigrationNeeded().build();
        Realm.setDefaultConfiguration(realmConfiguration);
        Realm.getDefaultInstance().beginTransaction();
        Realm.getDefaultInstance().copyToRealm(new Model("Test1", true));
        Realm.getDefaultInstance().copyToRealm(new Model("Test2", true));
        Realm.getDefaultInstance().commitTransaction();
    }
}

我的活动是:

  • 一个按钮,其中包含指向设备设置上的通知侦听器服务的链接
  • 显示模型状态(已选中或未选中)的文本视图
  • 用于更改模型选中值的按钮

这是代码:

public class MainActivity extends AppCompatActivity {

    Button btnEnableService;
    Button btnChangeValue;
    TextView tvStatus;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnEnableService = (Button) findViewById(R.id.btn_enable_service);
        btnChangeValue = (Button) findViewById(R.id.btn_change_value);
        tvStatus = (TextView) findViewById(R.id.tv_status);

        btnEnableService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    startActivity(new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS));
                }catch(ActivityNotFoundException anfe){
                    Toast.makeText(MainActivity.this, "Your device not supports Notification Listener Service", Toast.LENGTH_SHORT).show();
                }
            }
        });

        btnChangeValue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean status = Realm.getDefaultInstance().where(Model.class).equalTo("name", "Test1").findFirst().isChecked();
                Realm.getDefaultInstance().beginTransaction();
                Realm.getDefaultInstance().where(Model.class).equalTo("name", "Test1").findFirst().setChecked(!status);
                Realm.getDefaultInstance().commitTransaction();
                updateView();
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        updateView();
    }

    private void updateView(){
        boolean status = Realm.getDefaultInstance().where(Model.class).equalTo("name", "Test1").findFirst().isChecked();
        tvStatus.setText("Status: " + status);
    }
}

当我点击btnChangeValue按钮时,我更改Realm上的值,并使用新值更新textview。现在一切都好。

另一方面,我有一项服务来收听设备通知。它非常简单,只显示LogCat上模型的状态值:

public class CustomNotificationListenerService extends NotificationListenerService{

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

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        super.onNotificationPosted(sbn);
        boolean status = Realm.getDefaultInstance().where(Model.class).equalTo("name", "Test1").findFirst().isChecked();
        Log.i("XXX", "Status: " + status);
    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {
        super.onNotificationRemoved(sbn);
    }
}

我的问题是模型的选中值不一致,我从MainActivity和NotificationListenerService获得不同的值。

我做错了什么?

提前致谢!

1 个答案:

答案 0 :(得分:0)

经过几个小时的测试后,我解决了我的问题每次操作后关闭Realm实例

因此,数据是一致的,我可以从NotificationListenerService和MainActivity查询。

<强> ExampleApplication

public class ExampleApplication extends Application{

    @Override
    public void onCreate() {
        super.onCreate();
        RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(getApplicationContext()).deleteRealmIfMigrationNeeded().build();
        Realm.setDefaultConfiguration(realmConfiguration);
        Realm mRealm = Realm.getDefaultInstance();
        mRealm.beginTransaction();
        mRealm.copyToRealm(new Model("Test1", true));
        mRealm.copyToRealm(new Model("Test2", true));
        mRealm.commitTransaction();
        mRealm.close();
    }
}

<强> MainActivity

...
@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    btnChangeValue.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Realm mRealm = Realm.getDefaultInstance();
            boolean status = mRealm.where(Model.class).equalTo("name", "Test1").findFirst().isChecked();
            mRealm.beginTransaction();
            mRealm.where(Model.class).equalTo("name", "Test1").findFirst().setChecked(!status);
            mRealm.commitTransaction();
            mRealm.close();
            updateView();
        }
    });
}
...

<强> CustomNotificationListenerService

...
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
    super.onNotificationPosted(sbn);
    Realm mRealm = Realm.getDefaultInstance();
    boolean status = mRealm.where(Model.class).equalTo("name", "Test1").findFirst().isChecked();
    mRealm.close();
    Log.i("XXX", "Status: " + status);
}
...

希望它对你有所帮助。