0

「MainActivity」では、デバイスを振ると「CompassActivity」に移動する関数を作成します。そして、それは機能します。そしてもう一度振って「MainActivity」に戻したいのですが、うまくいきません。

MainActivity のコードは次のとおりです。

package com.lab.mapme;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ImageButton;

public class MainActivity extends Activity implements SensorEventListener {

    ImageButton _buttonForward = null;
    ImageButton _buttonBackward = null;
    ImageButton _buttonLeft = null;
    ImageButton _buttonRight = null;

    boolean _fisMoved = false;
    boolean _bisMoved = false;
    boolean _lisMoved = false;
    boolean _risMoved = false;

    boolean _isShaked = false;
    long _lastSampleTime = 0;

    private SensorManager _sensorManager;

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            handleAccelerometer(event);
        }
    }

    private void handleAccelerometer(SensorEvent event) {
        float[] values = event.values;
        float x = values[0];
        float y = values[1];
        float z = values[2];

        Acceleration_getCompress(x, y, z);
        if (_isShaked) {
            startActivity(new Intent(getApplicationContext(),
                    CompassActivity.class));
        }

        Acceleration_getDirectionForward(y);
        if (_fisMoved) {
            _buttonForward.setBackgroundColor(Color.YELLOW);
        } else {
            _buttonForward.setBackgroundColor(Color.TRANSPARENT);
        }

        Acceleration_getDirectionBackward(y);
        if (_bisMoved) {
            _buttonBackward.setBackgroundColor(Color.YELLOW);
        } else {
            _buttonBackward.setBackgroundColor(Color.TRANSPARENT);
        }

        Acceleration_getDirectionLeft(x);
        if (_lisMoved) {
            _buttonLeft.setBackgroundColor(Color.YELLOW);
        } else {
            _buttonLeft.setBackgroundColor(Color.TRANSPARENT);
        }

        Acceleration_getDirectionRight(x);
        if (_risMoved) {
            _buttonRight.setBackgroundColor(Color.YELLOW);
        } else {
            _buttonRight.setBackgroundColor(Color.TRANSPARENT);
        }
    }

    private void Acceleration_getCompress(float x, float y, float z) {
        float a = (x * x + y * y + z * z);
        float aToGravity = a
                / (SensorManager.GRAVITY_EARTH * SensorManager.GRAVITY_EARTH);
        long currentTime = System.currentTimeMillis();
        if (aToGravity >= 2) {
            if (currentTime - _lastSampleTime < 200) {
                // duration too short, ignore slight movement
                return;
            }
            _lastSampleTime = currentTime;
            _isShaked = !_isShaked;
        }

    }

    private void Acceleration_getDirectionForward(float y) {
        float a = y;
        if (a > a + y) {
            _fisMoved = !_fisMoved;
        }
    }

    private void Acceleration_getDirectionBackward(float y) {
        float a = y;
        if (a < a + y) {
            _bisMoved = !_bisMoved;
        }
    }

    private void Acceleration_getDirectionLeft(float x) {
        float a = x;
        if (a < a + x) {
            _lisMoved = !_lisMoved;
        }
    }

    private void Acceleration_getDirectionRight(float x) {
        float a = x;
        if (a > a + x) {
            _risMoved = !_risMoved;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        _sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

        _buttonForward = (ImageButton) findViewById(R.id.buttonforward);
        _buttonBackward = (ImageButton) findViewById(R.id.buttonbackward);
        _buttonLeft = (ImageButton) findViewById(R.id.buttonleft);
        _buttonRight = (ImageButton) findViewById(R.id.buttonright);
    }

    @Override
    protected void onResume() {
        super.onResume();
        _sensorManager.registerListener(this,
                _sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                SensorManager.SENSOR_DELAY_NORMAL);
    }

    @Override
    protected void onPause() {
        super.onPause();
        _sensorManager.unregisterListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

ANそしてそれは CompassActivity のコードです

package com.lab.mapme;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.Toast;

public class CompassActivity extends Activity {

    private SensorManager _sensorManager;
    private Sensor _sensor;

    private CompassView _compassView;

    boolean _isShaked = false;
    long _lastSampleTime = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        _sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        _sensor = _sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
        _compassView = new CompassView(this);
        setContentView(_compassView);
    }

    private SensorEventListener _sensorEventListener = new SensorEventListener() {
        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
        }

        @Override
        public void onSensorChanged(SensorEvent event) {
            // angle between the magnetic north direction
            // 0=North, 90=East, 180=South, 270=West
            float azimuth = event.values[0];
            _compassView.updateData(azimuth);

            if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
                handleAccelerometer(event);
            }
        }

    };

    private void handleAccelerometer(SensorEvent event) {
        float[] values = event.values;
        float x = values[0];
        float y = values[1];
        float z = values[2];

        Acceleration_getDirection(x, y, z);
        if (_isShaked) {
            startActivity(new Intent(getApplicationContext(),
                    MainActivity.class));
        }
    }

    private void Acceleration_getDirection(float x, float y, float z) {
        float a = (x * x + y * y + z * z);
        float aToGravity = a
                / (SensorManager.GRAVITY_EARTH * SensorManager.GRAVITY_EARTH);
        long currentTime = System.currentTimeMillis();
        if (aToGravity >= 2) {
            if (currentTime - _lastSampleTime < 200) {
                // duration too short, ignore slight movement
                return;
            }
            _lastSampleTime = currentTime;
            _isShaked = !_isShaked;
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (_sensor != null) {
            _sensorManager.registerListener(_sensorEventListener, _sensor,
                    SensorManager.SENSOR_DELAY_NORMAL);
        } else {
            Toast.makeText(this, "ORIENTATION Sensor not found",
                    Toast.LENGTH_LONG).show();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (_sensor != null) {
            _sensorManager.unregisterListener(_sensorEventListener);
        }
    }

}
4

1 に答える 1

1

を使用startActivity(new Intent(getApplicationContext(), MainActivity.class));すると、新しいアクティビティ インスタンスが作成されます。代わりに、onFinish()またはonBackPressed()メソッドを適切に呼び出して、古いアクティビティをリサイクルします。

それが機能しない場合はhandleAccelerometer、CompassActivity 中にデバイスを振ったときに が呼び出されていることを確認してください。

于 2013-05-17T14:37:13.603 に答える