英文:
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
}
});
}
...
...
...
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论