activity反复进入onDestroy onCreate方法

问题:pad平放在桌子上,无任何操作,activity会反复进入onDestroy onCreate方法

加了此属性android:configChanges="keyboardHidden|orientation",好了,可是我并没有进行横竖屏切换

activity中注册了180度重力感应的代码new MySensorEventListener(this).register();  可是也没有进行180度的切换

所以问题是好了,但问题的确切出处还不清楚


1,MySensorEventListener 180度重力感应类

package cn.zhl.util;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

public class MySensorEventListener {
	Activity activity;

	public MySensorEventListener(Activity activity) {
		this.activity = activity;
	}

	public void register() {
		// 重力感应侦听器
		SensorEventListener lsn = new SensorEventListener() {
			public void onAccuracyChanged(Sensor s, int accuracy) {
			}

			@Override
			public void onSensorChanged(SensorEvent e) {
				// TODO Auto-generated method stub
				float x, y, z;
				x = e.values[SensorManager.DATA_X];
				y = e.values[SensorManager.DATA_Y];
				z = e.values[SensorManager.DATA_Z];
				TurnScreen(x, y, z);
			}
		};
		SensorManager sensorMgr = (SensorManager) activity
				.getSystemService(activity.SENSOR_SERVICE);
		Sensor sensor = sensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
		// 注册重力感应侦听器,第三个参数是检测的精确度
		sensorMgr.registerListener(lsn, sensor, SensorManager.SENSOR_DELAY_UI);
	}

	/**
	* 根据重力感应侦测信号,屏幕180度翻转
	* 
	* @param x
	*            x轴值,-10~10
	* @param y
	*            y轴值,-10~10
	* @param z
	*            z轴值,-10~10
	*/
	private void TurnScreen(float x, float y, float z) {
		// 横屏正方向
		if (activity.getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
				&& y < -5) {
			activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
		}
		// 横屏反方向
		else if (activity.getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
				&& y > 5) {
			activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		}
		// 纵屏正方向
		else if (activity.getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
				&& x > 5) {
			activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
		}
		// 纵屏反方向
		else if (activity.getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT
				&& x < -5) {
			activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		}
	}

}

2,

一般在AndroidManifest.xml文件中都没有使用到android:configChanges="keyboardHidden|orientation"配置,当然还是很有用的哈

就是如果配置了这个属性,当我们横竖屏切换的时候会直接调用onCreate方法中的onConfigurationChanged方法,而不会重新执行onCreate方法,那当然如果不配置这个属性的话就会重新调用onCreate方法了,下面是测试

package com.test;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;

public class TestActivity extends Activity {
    EditText et;
    TextView tv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        et = (EditText) findViewById(R.id.et);
        tv = (TextView) findViewById(R.id.tv);
        System.out.println("我是onCreate方法");
    }
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
            tv.setText("横屏");
        }else{
            tv.setText("竖屏");
        }
        
    }
}



你可能感兴趣的:(activity反复进入onDestroy onCreate方法)