Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android的领域中,是否可以进行进程间通信?_Android_Realm - Fatal编程技术网

在Android的领域中,是否可以进行进程间通信?

在Android的领域中,是否可以进行进程间通信?,android,realm,Android,Realm,是否可以在领域(Java、Android)中进行进程间通信。所以,比方说,如果我们从一个进程更新RealmObject字段的值,我们将在另一个进程的RealmChangeListener(对应于所提到的RealmObject)上获得触发器。我假设Realm的版本至少为2.0.2。Realm 2.0.0及更高版本支持非加密领域的进程间通知,但没有 我制作了一个样本项目,它对我很有用 在这里,RecyclerView显示了从属于远程进程的广播接收器插入的狗 10-11 20:53:46.340

是否可以在领域(Java、Android)中进行进程间通信。所以,比方说,如果我们从一个进程更新RealmObject字段的值,我们将在另一个进程的RealmChangeListener(对应于所提到的RealmObject)上获得触发器。我假设Realm的版本至少为2.0.2。

Realm 2.0.0及更高版本支持非加密领域的进程间通知,但没有



我制作了一个样本项目,它对我很有用

在这里,RecyclerView显示了从属于远程进程的广播接收器插入的狗

10-11 20:53:46.340 7302-7302/com.zhuinden.realm_multiprocess_example I/MainActivity: Activity in Process [7302]
10-11 20:53:46.344 7302-7302/com.zhuinden.realm_multiprocess_example D/RealmManager: Incrementing Activity Count [0]: opening Realm.
10-11 20:53:46.352 7302-7302/com.zhuinden.realm_multiprocess_example D/RealmManager: Increment: Count [1]

10-11 20:54:01.352 7560-7560/com.zhuinden.realm_multiprocess_example:remote D/AlarmReceiver: Alarm received in process [7560]
10-11 20:54:01.352 7560-8705/com.zhuinden.realm_multiprocess_example:remote I/AlarmReceiver: Inserting DOGE [Juice]

完整的源代码:

public class RealmManager {
    private static final String TAG = "RealmManager";

    static Realm realm;

    static RealmConfiguration realmConfiguration;

    public static RealmConfiguration buildDefaultRealmConfiguration() {
        return new RealmConfiguration.Builder()
                .deleteRealmIfMigrationNeeded()
                .build();
    }

    public static void initializeRealmConfig(Context appContext) {
        if(realmConfiguration == null) {
            Realm.init(appContext);
            Log.d(TAG, "Initializing Realm configuration.");
            setRealmConfiguration(buildDefaultRealmConfiguration());
        }
    }

    public static void setRealmConfiguration(RealmConfiguration realmConfiguration) {
        RealmManager.realmConfiguration = realmConfiguration;
        Realm.setDefaultConfiguration(realmConfiguration);
    }

    private static int activityCount = 0;

    public static Realm getRealm() {
        return realm;
    }

    public static void incrementCount() {
        if(activityCount == 0) {
            if(realm != null) {
                if(!realm.isClosed()) {
                    Log.w(TAG, "Unexpected open Realm found.");
                    realm.close();
                }
            }
            Log.d(TAG, "Incrementing Activity Count [0]: opening Realm.");
            realm = Realm.getDefaultInstance();
        }
        activityCount++;
        Log.d(TAG, "Increment: Count [" + activityCount + "]");
    }

    public static void decrementCount() {
        activityCount--;
        Log.d(TAG, "Decrement: Count [" + activityCount + "]");
        if(activityCount <= 0) {
            Log.d(TAG, "Decrementing Activity Count: closing Realm.");
            activityCount = 0;
            realm.close();
            if(Realm.compactRealm(realmConfiguration)) {
                Log.d(TAG, "Realm compacted successfully.");
            }
            realm = null;
        }
    }
}






领域2.0.0及更高版本支持非加密领域的进程间通知,但没有



我制作了一个样本项目,它对我很有用

在这里,RecyclerView显示了从属于远程进程的广播接收器插入的狗

10-11 20:53:46.340 7302-7302/com.zhuinden.realm_multiprocess_example I/MainActivity: Activity in Process [7302]
10-11 20:53:46.344 7302-7302/com.zhuinden.realm_multiprocess_example D/RealmManager: Incrementing Activity Count [0]: opening Realm.
10-11 20:53:46.352 7302-7302/com.zhuinden.realm_multiprocess_example D/RealmManager: Increment: Count [1]

10-11 20:54:01.352 7560-7560/com.zhuinden.realm_multiprocess_example:remote D/AlarmReceiver: Alarm received in process [7560]
10-11 20:54:01.352 7560-8705/com.zhuinden.realm_multiprocess_example:remote I/AlarmReceiver: Inserting DOGE [Juice]

完整的源代码:

public class RealmManager {
    private static final String TAG = "RealmManager";

    static Realm realm;

    static RealmConfiguration realmConfiguration;

    public static RealmConfiguration buildDefaultRealmConfiguration() {
        return new RealmConfiguration.Builder()
                .deleteRealmIfMigrationNeeded()
                .build();
    }

    public static void initializeRealmConfig(Context appContext) {
        if(realmConfiguration == null) {
            Realm.init(appContext);
            Log.d(TAG, "Initializing Realm configuration.");
            setRealmConfiguration(buildDefaultRealmConfiguration());
        }
    }

    public static void setRealmConfiguration(RealmConfiguration realmConfiguration) {
        RealmManager.realmConfiguration = realmConfiguration;
        Realm.setDefaultConfiguration(realmConfiguration);
    }

    private static int activityCount = 0;

    public static Realm getRealm() {
        return realm;
    }

    public static void incrementCount() {
        if(activityCount == 0) {
            if(realm != null) {
                if(!realm.isClosed()) {
                    Log.w(TAG, "Unexpected open Realm found.");
                    realm.close();
                }
            }
            Log.d(TAG, "Incrementing Activity Count [0]: opening Realm.");
            realm = Realm.getDefaultInstance();
        }
        activityCount++;
        Log.d(TAG, "Increment: Count [" + activityCount + "]");
    }

    public static void decrementCount() {
        activityCount--;
        Log.d(TAG, "Decrement: Count [" + activityCount + "]");
        if(activityCount <= 0) {
            Log.d(TAG, "Decrementing Activity Count: closing Realm.");
            activityCount = 0;
            realm.close();
            if(Realm.compactRealm(realmConfiguration)) {
                Log.d(TAG, "Realm compacted successfully.");
            }
            realm = null;
        }
    }
}






你能详细说明一下吗?我已经看过这一页了。但我没有观察到预期的行为。你有一些工作的例子吗?那么,什么是预期的行为,它是如何不发生的?SharedRealm中的
waitForChange
应该处理这个问题。waitForChange在本质上是阻塞的,而不是触发器。这不是我的意思。Realm已经运行了一个守护进程线程,该线程通过
waitForChange
侦听更新。其他进程中的提交也应该通知它。@nature1729 Realm对待UI线程就像对待任何其他循环器线程一样。您是否关闭了UI线程中的领域实例?如果是这种情况,则不会从其他线程/进程向UI线程发送通知。但我没有观察到预期的行为。你有一些工作的例子吗?那么,什么是预期的行为,它是如何不发生的?SharedRealm中的
waitForChange
应该处理这个问题。waitForChange在本质上是阻塞的,而不是触发器。这不是我的意思。Realm已经运行了一个守护进程线程,该线程通过
waitForChange
侦听更新。其他进程中的提交也应该通知它。@nature1729 Realm对待UI线程就像对待任何其他循环器线程一样。您是否关闭了UI线程中的领域实例?如果是这种情况,则不会从其他线程/进程向UI线程发送通知。
public enum DogNames {
    Jack,
    Jill,
    Joe,
    Jarble,
    Juice,
    Jen,
    Jim,
    Munch,
    Slurp,
    Boop,
    Largo,
    Boson,
    Pete,
    Boner,
    Derp,
    Roger,
    Bunbun,
    Twix,
    Dog
}
public class Dog extends RealmObject {
    @PrimaryKey
    private long id;

    private String name;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
public class AlarmReceiver extends BroadcastReceiver {
    private static final String TAG = "AlarmReceiver";

    RealmConfiguration realmConfiguration;

    Executor executor;

    @Override
    @SuppressLint("NewApi")
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "Alarm received in process [" + android.os.Process.myPid() + "]");
        Realm.init(context);
        if(realmConfiguration == null) {
            realmConfiguration = RealmManager.buildDefaultRealmConfiguration();
        }
        if(executor == null) {
            executor = Executors.newSingleThreadExecutor();
        }
        executor.execute(() -> {
            try(Realm r = Realm.getInstance(realmConfiguration)) {
                if(Looper.getMainLooper() == Looper.myLooper()) {
                    Log.i(TAG, "UI thread transaction: this should be fixed!");
                }
                r.executeTransaction(realm -> {
                    Dog dog = new Dog();
                    long id = realm.where(Dog.class).findAll().size()+1;
                    dog.setId(id);
                    dog.setName(DogNames.values()[((Long)id).intValue() % DogNames.values().length].name());
                    realm.insert(dog);
                    Log.i(TAG, "Inserting DOGE [" + dog.getName() + "]");
                });
            }
        });
    }
}
<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.zhuinden.realm_multiprocess_example"
          xmlns:android="http://schemas.android.com/apk/res/android">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <receiver
            android:name=".AlarmReceiver"
            android:process=":remote"/>
    </application>

</manifest>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/activity_main"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.zhuinden.realm_multiprocess_example.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</RelativeLayout>
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        mavenCentral()
        jcenter()
        maven {url "https://clojars.org/repo/"}
        maven { url "https://jitpack.io" }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.0'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
        classpath "io.realm:realm-gradle-plugin:2.0.2"
        classpath 'me.tatarka:gradle-retrolambda:3.2.5'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        mavenCentral()
        jcenter()
        maven {url "https://clojars.org/repo/"}
        maven { url "https://jitpack.io" }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'
apply plugin: 'me.tatarka.retrolambda'
apply plugin: 'realm-android'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.2"
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    defaultConfig {
        applicationId "com.zhuinden.realm_multiprocess_example"
        minSdkVersion 14
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    packagingOptions {
        // Exclude file to avoid
        // Error: Duplicate files during packaging of APK
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/ASL2.0'
        exclude 'META-INF/services/javax.annotation.processing.Processor'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:24.2.1'
    testCompile 'junit:junit:4.12'
    compile "com.android.support:recyclerview-v7:24.2.1"

    apt 'com.jakewharton:butterknife-compiler:8.4.0'
    compile 'com.jakewharton:butterknife:8.4.0'

    apt 'dk.ilios:realmfieldnameshelper:1.1.0'
    compile 'io.realm:android-adapters:1.3.0'
}