cicku
V2EX  ›  Android

这段 java 代码比较 UID 是用来做什么的?

  •  
  •   cicku · Mar 23, 2015 · 13352 views
    This topic created in 4103 days ago, the information mentioned may be changed or developed.

    小弟不才,对安卓这方面了解不是很深

    https://github.com/msg555/PowerTutor/blob/src/edu/umich/PowerTutor/components/Audio.java

    这是几年前的 powertutor 的代码,audio 用来获取音频设备信息,不过

    public int uid;
    public int id;
    public int assignUid;
    
    public int compareTo(Object obj) {
      MediaData x = (MediaData)obj;
      if(uid < x.uid) return -1;
      if(uid > x.uid) return 1;
      if(id < x.id) return -1;
      if(id > x.id) return 1;
      return 0;
    }
    
    public boolean equals(Object obj) {
      MediaData x = (MediaData)obj;
      return uid == x.uid && id == x.id;
    }
    

    }

    请问为什么要比较呢?是用来确定读写权限的嘛?现在是不是又比这个更简单的实现?

    3 replies    2015-03-30 00:42:28 +08:00
    xupefei
        1
    xupefei  
       Mar 24, 2015
    http://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html#compare(T,%20T) 说:

    Return: a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

    返回 -1 和返回 -10086 一样,1 和 10010 也一样。
    cicku
        2
    cicku  
    OP
       Mar 24, 2015
    @xupefei 不是,我的意思是这段音频设备的代码为什么要比较uid,就是单纯确定数据共享安全性的吗?
    vjnjc
        3
    vjnjc  
       Mar 30, 2015   ❤️ 1
    @cicku 用来判断2个MediaData 是不是包含了相同的数据。
    如果仅仅是用mediaData1 == mediaData2 只能判断是不是相同的引用
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1569 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 16:30 · PVG 00:30 · LAX 09:30 · JFK 12:30
    ♥ Do have faith in what you're doing.