我一直在阅读并看到系统服务无法立即访问;必须在调用onCreate()之后访问它们。Android:我可以在没有活动的情况下访问系统服务吗?
我正在创建一个键盘服务,但我想访问系统服务,如传感器。有没有一种方法可以在没有活动的情况下做到这一点?
我已经尝试让我的类继承InputMethodService并实现SensorEventListener。我实现了onSensorChanged()方法,并告诉它打印到logcat,但它永远不会被调用。我有一个方法onCreateInputView(),其中我定义传感器管理器和加速度计:
public class MyInput extends InputMethodService implements SensorEventListener {
MyInputView MyInputView;
private SensorManager mSensorManager;
@Override
public View onCreateInputView() {
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
final MyInputView MyInputView = (MyInputView) getLayoutInflater().inflate(R.layout.MyInput, null);
this.MyInputView = MyInputView;
return MyInputView;
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
Log.d("called", "called");
}
}
[基于给定的建议看来InputMethodService从上下文导出,因此,的onCreate()应称为... ]
有趣的问题。 InputMethodService也从Context继承,所以getSystemService()方法应该至少存在,并且它有一个onCreate()生命周期方法。如果在调用onCreate()之后尝试使用此上下文获取所需服务,会发生什么情况? –
好吧,那么设置更新的代码以及不会触发的事件方法在哪里?在这一点上,集体的信念似乎是,你想要做的是*可能*因此问题与你的尝试,我们无法帮助,没有看到你正在试图做什么来得到*只是这个*加工。 –
@ChrisStratton附加是我正在处理 – mino