出于设计原因,我必须在类中实现 Android 传感器特定代码extends Service
。例如,
public class SmokeService extends Service implements ISmokeDetector {
SensorManager mySensorManager;
Sensor myProximitySensor;
private double currentLightReading;
private Context myContext;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
super.onCreate();
mySensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
myProximitySensor = mySensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
mySensorManager.registerListener(proximitySensorEventListener, myProximitySensor, SensorManager.SENSOR_DELAY_NORMAL);
}
SensorEventListener proximitySensorEventListener = new SensorEventListener() {
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
}
};
@Override
public SmokePresenceStruct getsmokePresence() {
return new SmokePresenceStruct(true, timestamp);
}
@Override
public void getsmokePresence(ListenersmokePresence handler) {
handler.onNewsmokePresence(this.getsmokePresence());
}
@Override
public boolean isEventDriven() {
}
}
我的问题是“有没有办法通过非 Android Java 类启动上述服务? ”假设我有一个 java 类,它有 Context 并且它想要启动这个服务。例如 :
public class AndroidSmokeDetector {
Context myContext;
public AndroidSmokeDetector(Context context, Object obj) {
//super(context);
this.myContext = context;
}
}
请建议我通过AndroidSmokeDetector
课程与服务交互的方式。假设我会从中获得上下文MainActivity
?”