我有一个应用程序,它使用片段中的传感器侦听器来检测Moto360智能手表上的摇动手势。每个片段都有一组不同的手势,因此在打开/关闭时,必须从传感器管理器注册/取消注册它的听众。一切正常,直到手表进入环境模式。应用程序从环境模式返回后,传感器开始出现故障或完全停止工作。我怀疑我可能正在处理注册/取消注册错误。任何人都可以看到我可能会出错的地方吗?
我有一个MainWearActivity类,它扩展了WearableActivity并处理所有片段事务:
public class MainWearActivity extends WearableActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main_wear);
setAmbientEnabled();
// ... etc ...//
}
@Override
public void onEnterAmbient(Bundle ambientDetails) {
super.onEnterAmbient(ambientDetails);
}
@Override
public void onUpdateAmbient() {
super.onUpdateAmbient();
}
@Override
public void onExitAmbient() {
super.onExitAmbient();
}
// Method the main fragment uses to replace itself with a new fragment
public void replaceFragment(Fragment frag, String fragTag) {
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.fragmentContainer, frag, fragTag);
mFragmentTransaction.addToBackStack(null);
mFragmentTransaction.commit();
}
}
我有一个主片段,它向用户显示一个菜单,并允许他们选择一个选项。当用户选择一个选项时,主片段放在backstack上,并由选项片段替换。当用户从选项片段返回时,会弹出backstack以返回主菜单片段:
public class MainFragment extends Fragment {
private MainWearActivity mMainWearActivity;
View view;
private SensorManager mSensorMgr;
private WearXFlick mXFlick; //Extends SensorEventListener
private Vibrator vibrator;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mMainWearActivity = (MainWearActivity) getActivity();
vibrator = (Vibrator) getActivity().getSystemService(Context.VIBRATOR_SERVICE);
mSensorMgr = (SensorManager) mMainWearActivity.getSystemService(Activity.SENSOR_SERVICE);
mXFlick = new WearXFlick();
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mSensorMgr.registerListener(mXFlick, mSensorMgr.getDefaultSensor(Sensor.TYPE_GYROSCOPE), SensorManager.SENSOR_DELAY_GAME);
mXFlick.setOnShakeListener(new OnShakeListener() {
@Override
public void onShake() {
vibrator.vibrate(Consts.vibPattern, -1);
// Select the current menu item
switch (mMainWearActivity.getCurrentSelection()) {
case Consts.MENU_MAIL:
MailFragment mf = new MailFragment();
mMainWearActivity.replaceFragment(mf, Consts.FRAG_MAIL);
break;
}
}
});
}
@Override
public void onStop() {
mSensorMgr.unregisterListener(mXFlick);
super.onStop();
}
@Override
public void onStart() {
super.onStart();
mSensorMgr.registerListener(mXFlick, mSensorMgr.getDefaultSensor(Sensor.TYPE_GYROSCOPE), SensorManager.SENSOR_DELAY_GAME);
}
@Override
public void onPause() {
mSensorMgr.unregisterListener(mXFlick);
super.onPause();
}
@Override
public void onResume() {
super.onResume();
mSensorMgr.registerListener(mXFlick, mSensorMgr.getDefaultSensor(Sensor.TYPE_GYROSCOPE), SensorManager.SENSOR_DELAY_GAME);
}
@Override
public void onDestroyView() {
mSensorMgr.unregisterListener(mXFlick);
super.onDestroyView();
}
}
一旦调用了replaceFragment(),就会加载一个新的片段,它基本上具有与此相同的格式,其中一组不同的传感器监听器在停止/开始/暂停/恢复时注册和取消注册,尽管我真的不喜欢&#39 ; t。知道我是否应该取消注册/注册传感器监听器的正确方法。但是,一切正常,直到应用程序进入环境模式并返回。当发生这种情况时,传感器会发生故障或失灵。