onSensorChanged Azimut价值跳跃

时间:2013-01-04 16:04:11

标签: android android-activity compass-geolocation

我是Android的新手,所以我相信我在某个地方犯了一个错误而无法修复它。我有一个主要活动,我们称之为主菜单。从这开始,我按下相应的按钮启动其他活动,以启动我想要的活动。问题是我在其中一个已启动的活动中使用了onSensorchanged()。如果应用程序第一次启动,则启动的活动值将按预期运行,但如果我关闭活动(换句话说现在我回到主菜单中)并再次启动活动,则从onSensorchanged()读取值跳跃。正确的值与错误的值组合在一起。我使用Intent来启动第二个活动,并认为问题出在这个问题上。

主菜单代码:

public class MainMenuActivity extends Activity{

Button gps_btn,lineFollow_btn,man_control_btn;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_menu);
    addListenerOnButton();
    Debug.startMethodTracing("Navigation Trace");
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main_menu, menu);
    return true;
}

@Override
protected void onPause() 
{


    super.onPause();
    Debug.stopMethodTracing();

}

public void addListenerOnButton() 
{

    final Context context = this;

    gps_btn = (Button) findViewById(R.id.GPS_btn);

    gps_btn.setOnClickListener(new OnClickListener() 
    {

        public void onClick(View arg0) 
        {

            Intent intent = new Intent(context, AutoControl.class);
                        startActivity(intent);   

        }

    });

    lineFollow_btn = (Button) findViewById(R.id.line_follower_btn);

    lineFollow_btn.setOnClickListener(new OnClickListener() 
    {

        public void onClick(View arg0) 
        {

            Intent intent2 = new Intent(context, CamDemoActivity.class);
                        startActivity(intent2);   

        }

    });

    man_control_btn = (Button) findViewById(R.id.man_ctrl_btn);

    man_control_btn.setOnClickListener(new OnClickListener() 
    {

        public void onClick(View arg0) 
        {

            Intent intent2 = new Intent(context, ManualAct_PLC.class);
                        startActivity(intent2);   

        }

    });

}

OnSensorChanged的代码:

    public void onSensorChanged(SensorEvent event) 
{
    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
        gravity = event.values;
    if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
        geoMagnetic = event.values;
    if (gravity != null && geoMagnetic != null) 
    {
        float R[] = new float[9];
        float I[] = new float[9];
        boolean success = SensorManager.getRotationMatrix(R, I, gravity,geoMagnetic);

        if (success) 
        {
            /* Orientation has azimuth, pitch and roll */
            float orientation[] = new float[3];
            SensorManager.getOrientation(R, orientation);
            float azimut = orientation[0];
            updateSensorInfoView(azimut);           
            steerRobot(azimut);
            System.out.println("Raw Sensor value:"  + Double.toString(Math.toDegrees(azimut)));
            System.out.println("Degree Sensor value:"  + calculations.compassDegString(azimut));
        }
    }

}

查看记录的值,再次启动AutoControl活动后,我的传感器数据是意外的:

    01-04 17:52:34.925: I/System.out(31387): Calculatebearing value:2.168326647225342
    01-04 17:52:34.925: I/System.out(31387): Raw Sensor value:-116.11590698474976
    01-04 17:52:34.925: I/System.out(31387): Degree Sensor value:244.0
    01-04 17:52:34.955: I/System.out(31387): Calculatebearing value:173.96307878454928
    01-04 17:52:34.955: I/System.out(31387): Raw Sensor value:72.0893408779263
    01-04 17:52:34.955: I/System.out(31387): Degree Sensor value:72.0
    01-04 17:52:35.065: I/System.out(31387): Calculatebearing value:108.5784565015826
    01-04 17:52:35.065: I/System.out(31387): Raw Sensor value:137.473963160893
    01-04 17:52:35.070: I/System.out(31387): Degree Sensor value:137.0

请注意,度传感器值在短时间内大幅跳跃。我根据计算出的轴承角度在屏幕上绘制了一个箭头,一旦活动第二次加载,就会像疯了一样跳跃。

我怀疑是否创建了此活动的第二个实例,但是使用了Manifest文件中的android:launchMode =“singleInstance”标记来尝试阻止此操作。

我有其他人遇到过这样的问题吗?我似乎没有在网上发现类似的问题。

1 个答案:

答案 0 :(得分:0)

将问题活动转移到自己的应用程序后,我仍然遇到了同样的问题。消除了同一活动的多个实例正在运行的担忧。

看完之后: https://groups.google.com/forum/?fromgroups=#!topic/android-developers/UNMaDEUcXb0

我通过更改代码OnSensorchanged方法解决了这个问题。多次运行后指针似乎有问题,因此最好使用传感器值对象更改的克隆:

改变

案例Sensor.TYPE_MAGNETIC_FIELD:                     mag = event.values;

案例Sensor.TYPE_MAGNETIC_FIELD:                     mag = event.values.clone();