简体   繁体   English

如何在android中实现跨进程锁?

[英]How to implement cross process lock in android?

I'm writing a library project for multiple APPs to use. 我正在编写一个供多个APP使用的库项目。 And for some reason, I must make a function mutual exclusion for different APPs, so I need a cross-process lock. 由于某种原因,我必须为不同的APP做一个函数互斥,所以我需要一个跨进程锁。 But as far as I know, in android APPs can only write to it's own file's directory in internal storage, and external storage is unreliable because some device don't have one. 但据我所知,在android中,APP只能在内部存储中写入自己文件的目录,外部存储不可靠,因为某些设备没有。 So file lock seems not applicable for me, so is there any other way to implement cross-process lock? 所以文件锁似乎不适合我,所以有没有其他方法来实现跨进程锁?

thanks~ 谢谢〜

If you do not want to (or you can not) use flock or fcntl, maybe you can use LocalServerSocket to implement a spinlock. 如果你不想(或你不能)使用flock或fcntl,也许你可以使用LocalServerSocket来实现一个自旋锁。 For example: 例如:

public class SocketLock {
    public SocketLock(String name) {
        mName = name;
    }

    public final synchronized void tryLock() throws IOException {
        if (mServer == null) {
            mServer = new LocalServerSocket(mName);
        } else {
            throw new IllegalStateException("tryLock but has locked");
        }
    }

    public final synchronized boolean timedLock(int ms) {
        long expiredTime = System.currentTimeMillis() + ms;

        while (true) {
            if (System.currentTimeMillis() > expiredTime) {
                return false;
            }
            try {
                try {
                    tryLock();
                    return true;
                } catch (IOException e) {
                    // ignore the exception
                }
                Thread.sleep(10, 0);
            } catch (InterruptedException e) {
                continue;
            }
        }
    }

    public final synchronized void lock() {
        while (true) {
            try {
                try {
                    tryLock();
                    return;
                } catch (IOException e) {
                    // ignore the exception
                }
                Thread.sleep(10, 0);
            } catch (InterruptedException e) {
                continue;
            }
        }
    }

    public final synchronized void release() {
        if (mServer != null) {
            try {
                mServer.close();
            } catch (IOException e) {
                // ignore the exception
            }
        }
    }

    private final String mName;

    private LocalServerSocket mServer;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Android上实现OMA前向锁定? - How to implement OMA forward lock on Android? 如何为Android应用程序实施密码的智能锁定 - How to Implement Smart Lock for Passwords to Android Application 如何在Android中以编程方式实现应用锁 - How to implement app lock programmatically in Android 如何在Android中实现跨应用程序自动填充? - How to implement cross-application autofill in Android? 如何实现自定义方式来锁定/解锁android手机 - How to implement a custom way to lock/unlock an android mobile phone 为Android应用程序实施密码锁 - Implement Passcode Lock for Android Application 如何在 Android MVVM 应用程序中进行 model 跨片段事务(正在进行的过程)? - How to model cross fragment transaction (ongoing process) in Android MVVM application? 如何实现棒棒糖锁屏? - How to implement Lock Screen for Lollipop? 如何在指定进程运行指定时间后在我的 android 手机上触发自动屏幕锁定进程? - How to trigger an automatic screen lock process on my android phone after a specified process running specified time? 如何实现一个始终存在的不可杀死的Android应用程序? - How to implement a NOT killable Android App that process always exists?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM