开发者

Android Menu Option not Working

开发者 https://www.devze.com 2023-03-12 17:30 出处:网络
I am in the process of implementing this menu but it is not updating the speed of my sensor. My code snipet is:

I am in the process of implementing this menu but it is not updating the speed of my sensor. My code snipet is:

Global variables:

private static final int MENU_SLOW = 0;
private static final int MENU_NORMAL = 1;
private static final int MENU_FAST = 2;
private static final int MENU_EXIT = -1;
int ROTATION_SPEED = 3;


private 开发者_如何学Cstatic SensorManager mySensorManager;
private boolean sersorrunning;
private myCompassView myCompassView;

 /** Called when the activity is first created.*/ 
 @Override
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);

     myCompassView = (myCompassView)findViewById(R.id.mycompassview);

     mySensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);

     List<Sensor> mySensors = mySensorManager.getSensorList(Sensor.TYPE_ORIENTATION);

     if(mySensors.size() > 0){

      mySensorManager.registerListener(mySensorEventListener, mySensors.get(0), ROTATION_SPEED);
      sersorrunning = true;
      Toast.makeText(this, "POINT the Needle to N for NORTH", Toast.LENGTH_LONG).show();

     }
     else{
      Toast.makeText(this, "COMPASS not Initilised", Toast.LENGTH_LONG).show();
      sersorrunning = false;
      finish();
     }
 }

Sensor speed code:

 if(mySensors.size() > 0){

  mySensorManager.registerListener(mySensorEventListener, mySensors.get(0), ROTATION_SPEED);
  sersorrunning = true;
  Toast.makeText(this, "POINT the Needle to N for NORTH", Toast.LENGTH_LONG).show();

 }
 else{
  Toast.makeText(this, "COMPASS not Initilised", Toast.LENGTH_LONG).show();
  sersorrunning = false;
  finish();
 }

And here is how i am trying to change the variable ROTATION_SPEED:

public boolean onOptionsItemSelected (MenuItem item){

    if (item.getItemId() == MENU_SLOW){
        ROTATION_SPEED = SensorManager.SENSOR_DELAY_UI;
        Toast.makeText(this, "Speed changed to SystemPreffered", Toast.LENGTH_SHORT).show();

    }else if (item.getItemId() == MENU_NORMAL){
        ROTATION_SPEED = SensorManager.SENSOR_DELAY_NORMAL;
        Toast.makeText(this, "Speed changed to NORMAL", Toast.LENGTH_SHORT).show();

    }else if (item.getItemId() == MENU_FAST){
        ROTATION_SPEED = SensorManager.SENSOR_DELAY_FASTEST;
        Toast.makeText(this, "Speed changed to FAST", Toast.LENGTH_SHORT).show();

    }else if (item.getItemId() == MENU_EXIT){
        android.os.Process.killProcess(android.os.Process.myPid());
        Toast.makeText(this, "Exiting application...", Toast.LENGTH_SHORT).show();
    }
    return false;
}


Options menu items are not checked as you have predefined constants. They usually are resources that you have defined in menu/options_menu.xml and inflate in onCreateOptionsMenu. Your handler should look like:

public boolean onOptionsItemSelected(MenuItem item) {
    // Handle all of the possible menu actions.
    switch (item.getItemId()) {
        case R.id.menu_slow:
            ...
            break;
        case R.id.menu_normal:
                                 ...
            break;
        case R.id.menu_fast:
                                 ...
            break;
        case R.id.menu_exit:
                                 ...
            break;
        }
    Log.e(TAG,"onOptionsItemSelected");
    return super.onOptionsItemSelected(item);

Put a few logs in your handler so you can see what is happening too.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号