代码之家  ›  专栏  ›  技术社区  ›  Huzo

侦听器接口如何工作或为什么工作?除了作为一个监听器,接口还有其他用途吗?

  •  0
  • Huzo  · 技术社区  · 6 年前

    SensorEventListener

    现在我们必须重写这个监听器接口的方法。

    public void onSensorChanged(SensorEvent event);

    public void onAccuracyChanged(Sensor sensor, int accuracy);

    我不明白的是:

    1. onAccuracyChanged 方法在精度更改时被调用?
    2. 不精确性改变 只是一个我们重写的空方法,因为我们的公式(或我们实现的接口)要求我们这样做。如果它是由较低级别引起的魔法
    3. 什么时候,为什么有人会在他/她的电脑中使用界面
    4 回复  |  直到 6 年前
        1
  •  2
  •   Themelis    6 年前

    suitable answer . 请允许我举一个关于听众的例子。

    听众:

    Worker ,以及另一个对该数据感兴趣的类 InterestedClass

    public class Worker extends Thread{
      interface DataFetchedListener{
        void onDataFetched(String data);
      }
    
      private DataFetchedListener listener;
    
      @Override
      public void run(){
       String data = fetchData();
       // Data fetched inform your listener so he can take action
       listener.onDataFetched(data);
      }
    
      public void setDataFetchedListener(DataFetchedListener listener){
       this.listener = listener;
      }
    
      private String fetchData(){
        // returns the fetched data after some operations
        return "Data";
      }
    }
    
    
    public class InterestedClass implements Worker.DatafetchedListener{
    
     @Override
     public void onDataFetched(String data){
      doSomethingWith(data);
     }
    
     private doSomethingWith(String data){
      // just print it in the console
      System.out.println("Data fetched is -> " + data);
     }
    
    }
    

    工人 不关心哪个类将操作其数据,只要该类 DataFetchedListener .

    任何类别 能够处理数据( 兴趣班 只需在控制台中打印)但是 只是它实现了它的接口 .

    主管道可以这样走。。。

    public class Application{
      public static void main(String[] args){
       InterestedClass interested = new InterestedClass();
       Worker worker = new Worker();
       worker.setDataFetchedListener(intereseted);
       worker.start(); // Starts Worker's thread
      }
    } 
    

    工人 interested 对象),侦听器将相应地进行操作( 感兴趣 将数据打印到控制台)。

        2
  •  1
  •   Community CDub    4 年前

    1. 实现它们的类
    2. 匿名班级

    考虑这个代码:

    interface TestInterface {
        void doSomething();
    }
    
    class TestClass{
        private TestInterface ti;
        public TestClass(TestInterface ti){
            this.ti = ti;
        }
    
        public void testActionMethod(){
            ti.doSomething();
            //some other codes
        }
    }
    
    class OurOwnLauncherApp{
        public static void main(String[] args) {
            TestClass tc = new TestClass(new TestInterface() {
                @Override
                public void doSomething() {
                    System.out.println("Hi!");
                }
            });
    
            tc.testActionMethod();
    
            TestClass tc2 = new TestClass(new TestInterface() {
                @Override
                public void doSomething() {
                    System.out.println("Bye!");
                }
            });
    
            tc2.testActionMethod();
        }
    }
    

    在这里,我们有:

    1. 一个界面(就像你要求的那样)
    2. 函数类使用该接口

    这段代码的作用是,它为 测试方法 和呼唤 剂量 测试方法 , 我们将调用反转回我们自己的方法。 这就是为什么你会看到这个结果:

    你好

    再见!

    这正是侦听器接口及其工作方式的简化版本

        3
  •  1
  •   hadi.mansouri    6 年前

    对于某些实体,可以侦听该实体上的某些事件(将该实体命名为事件生成器)。因此,其他实体应该有某种方式来侦听这些更改(让我们将这些实体命名为侦听器)。现在,侦听器将自己注册为事件生成器的侦听器。当事件生成器上发生事件时,它将调用已注册侦听器的相关方法。

    1-侦听器实现接口 2-侦听器将自身注册为按钮(事件生成器)的侦听器 3-事件生成器调用所有已注册侦听器的适当方法(此方法是接口的方法)。

    对于您的情况,android提供了一个管理器,您可以通过它在某些传感器上注册侦听器: android.hardware.SensorManager.registerListener() SensorEventListener )作为传感器侦听器,该传感器中的更改将导致调用侦听器的方法)。

        4
  •  1
  •   Gratien Asimbahwe    6 年前

    为此你打开了一个秘密 听事件。但这还不够,因为您也需要得到通知,以便您可以根据事件进行回复。你设定 回调

    监听器是通过回调进行监听和通知的接口。

    • 首先,使用事件发生时要调用的空实体方法创建一个接口:
    public interface MyListener{
    
          void actionOneHappens(Object o);
          void actionTwo();
          void actionThree();
    
    }
    
    • 创建一个处理某些内容的类,例如:
    public class MyCounter{
    //create a member of type MyListener if you intend to exchange infos
    
    private MyListener myListener;
    
    //let's create a setter for our listener
    public void setMyListener(MyListener listener)
    {
    this.myListener=listener;
    }
    
      MyCounter(){
    
      }
    //this method will help us count
    public void startCounting()
    {
      new CountDownTimer(10000,1000)
           {
    
               @Override
               public void onTick(long millisUntilFinished) {
    
                //I want to notify at third second after counter launched
    
                if(millisUntilFinished/1000==3)
                {
                  // I notify if true :
                  //as someone can forget to set the listener let's test if it's not //null
                  if(myListener!=null){
                     myListener.actionThree();
                  }
    
    
                }
    
               }
    
               @Override
               public void onFinish() {
    
               }
           }.start();
    }
    
    
    
    
    }
    
    • 然后可以创建类型为的对象 MyCounter 知道什么时候是三点:

    MyCounter myCounter=new MyCounter();
    
    myCounter.setMyListener(new MyListener()
    {
    //then override methods here
      @override
      void actionOneHappens(Object o){
      }
      @override
      void actionTwo()
      {}
    
      @override
      void actionThree()
      {
       //Add you code here
       Toast.makeText(getApplicationContext(),"I'm at 3",Toast.LENGTH_LONG).show()
       }
    
    
    
    });
    
    //start your counter
    myCounter.startCounting();

    就这样!!这就是我们前进的方向。