调用另一个文件中的类以提供服务。

huangapple go评论78阅读模式
英文:

calling class from another file to service

问题

Accelerometer.java:

package com.example.pahoappv2;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

public class Accelerometer {
    public interface Listener {
        void onTranslation(float tx, float ty, float tz);
    }
    private Listener listener;
    public void setListener(Listener l) {
        listener = l;
    }

    private SensorManager sensorManager;
    private Sensor sensor;
    private SensorEventListener sensorEventListener;

    Accelerometer(Context context) {
        sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
        sensor = sensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);
        if (sensor == null) System.out.println("sensor not found"); // debug message
        sensorEventListener = new SensorEventListener() {
            @Override
            public void onSensorChanged(SensorEvent sensorEvent) {
                if (listener != null) {
                    listener.onTranslation(sensorEvent.values[0], sensorEvent.values[1], sensorEvent.values[2]);
                }
            }

            @Override
            public void onAccuracyChanged(Sensor sensor, int i) {

            }
        };
    }
    public void register() {
        sensorManager.registerListener(sensorEventListener, sensor, SensorManager.SENSOR_DELAY_NORMAL);
    }
    public void unregister() {
        sensorManager.unregisterListener(sensorEventListener);
    }
}

MainActivity.java (working codes properly):

private Accelerometer accelerometer;
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        accelerometer = new Accelerometer(this);
        accelerometer.setListener(new Accelerometer.Listener() {
            @Override
            public void onTranslation(float tx, float ty, float tz) {
                //getting data and doing smt
            }
        });
    ...
    ...
    ...
    }

ExampleService.java:

public class ExampleService extends Service {
    Accelerometer accelerometer; // working
    accelerometer = new Accelerometer(this); // working
    accelerometer.setListener( // IDE cant see setListener method
         new Accelerometer.Listener() {
         @Override
         public void onTranslation(float tx, float ty, float tz) {
             // get data
         }
    });
    ...
    ...
    ...
}
英文:

I have a class Accelerometer.java taking data from sensors and I can call it in MainActivity, it works. But in order to make a application running in background, I built a service and now I want to call my sensor class here. But my IDE(android studio) cant see the methods of class while it works for MainActivity perfectly.

Thank you so much

Accelerometer.java

package com.example.pahoappv2;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
public class Accelerometer {
public interface Listener {
void onTranslation(float tx,float ty,float tz);
}
private Listener listener;
public void setListener(Listener l){
listener = l;
}
private SensorManager sensorManager;
private Sensor sensor;
private SensorEventListener sensorEventListener;
Accelerometer(Context context){
sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
sensor = sensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);
if(sensor==null) System.out.println("sensor not found"); //debug message
sensorEventListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
if(listener!=null) {
listener.onTranslation(sensorEvent.values[0],sensorEvent.values[1],sensorEvent.values[2]);
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
};
}
public void register(){
sensorManager.registerListener(sensorEventListener,sensor,SensorManager.SENSOR_DELAY_NORMAL);
}
public void unregister(){
sensorManager.unregisterListener(sensorEventListener);
}
}

MainActivity.java (working codes properly)

private Accelerometer accelerometer;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
accelerometer = new Accelerometer(this);
accelerometer.setListener(new Accelerometer.Listener() {
@Override
public void onTranslation(float tx, float ty, float tz) {
//getting data and doing smt
}
});
...
...
...
}

ExampleService.java

public class ExampleService extends Service {
Accelerometer accelerometer; //working
accelerometer = new Accelerometer(this); //working
accelerometer.setListener( //IDE cant see setListener method
new Accelerometer.Listener() {
@Override
public void onTranslation(float tx, float ty, float tz) {
//get data
}
});
...
...
...
}

答案1

得分: 0

你必须在onCreate、onStartCommand等方法内调用它...

public class ExampleService extends Service {

    @Override
    public void onCreate() {
        super.onCreate();
        Accelerometer accelerometer; // 正在工作
        accelerometer = new Accelerometer(this); // 正在工作
        accelerometer.setListener(
            new Accelerometer.Listener() {
                @Override
                public void onTranslation(float tx, float ty, float tz) {
                    // 获取数据
                }
            });
    }
    // ...
    // ...
    // ...
}
英文:

You have to call it inside onCreate, onStartCommand ...

public class ExampleService extends Service {
@Override
public void onCreate() {
super.onCreate();
Accelerometer accelerometer; //working
accelerometer = new Accelerometer(this); //working
accelerometer.setListener(
new Accelerometer.Listener() {
@Override
public void onTranslation(float tx, float ty, float tz) {
//get data
}
});
}
...
...
...

}

huangapple
  • 本文由 发表于 2020年7月27日 08:16:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63107115.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定