获取本地传感器的列表
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SensorManager systemService = (SensorManager) getSystemService(SENSOR_SERVICE);
List<Sensor> sensorList = systemService.getSensorList(Sensor.TYPE_ALL);
for (Sensor sensor : sensorList) {
Log.v("meeeeeee", ""+sensor.getName());
}
Sensor lightSensor = systemService.getDefaultSensor(Sensor.TYPE_LIGHT);
systemService.registerListener(new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
float[] values =event.values;
Log.v("meeeeeee", ""+values[0]);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}, lightSensor, SensorManager.SENSOR_DELAY_NORMAL);
Sensor Temparature = systemService.getDefaultSensor(Sensor.TYPE_TEMPERATURE);
}
}
方向传感器的监听
TextView mTv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTv=(TextView) findViewById(R.id.tv);
SensorManager manager = (SensorManager) getSystemService(SENSOR_SERVICE);
Sensor OrientationSensor = manager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
manager.registerListener(new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
final float[] values =event.values;
Log.v("meeeeeee", ""+values[0]);
runOnUiThread(new Runnable() {
@Override
public void run() {
mTv.setText(values[0]+"");
}
});
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}, OrientationSensor,SensorManager.SENSOR_DELAY_FASTEST);
}
温度传感器的监听
TextView mTv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTv=(TextView) findViewById(R.id.tv);
SensorManager sensorman = (SensorManager) getSystemService(SENSOR_SERVICE);
Sensor TemSensor = sensorman.getDefaultSensor(Sensor.TYPE_TEMPERATURE);
sensorman.registerListener(new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
Log.v("meeeeeee", "检测到值");
float[] values = event.values;
Log.v("meeeeeee", ""+values[0]);
if(event.sensor.getType()==Sensor.TYPE_TEMPERATURE){
float temperature=event.values[0];
mTv.setText(String.valueOf(temperature)+"°C");
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}, TemSensor, SensorManager.SENSOR_DELAY_FASTEST);
}
Fragment入门
1.创建Fragment的子类,复写oncreateView方法
2.
Fragment soundFragment1 ;
Fragment soundFragment2 ;
Fragment soundFragment3 ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
soundFragment1 = new SoundFragment();
soundFragment2 = new Sound2Fragment();
soundFragment3 = new Sound3Fragment();
findViewById(R.id.tv1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
changeFragment(soundFragment1);
}
});
findViewById(R.id.tv2).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
changeFragment(soundFragment2);
}
});
findViewById(R.id.tv3).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
changeFragment(soundFragment3);
}
});
changeFragment(soundFragment1);
}
private void changeFragment(Fragment s) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction trans = fragmentManager.beginTransaction();
trans.replace(R.id.frame, s);
trans.addToBackStack(null);
trans.commit();
}
LinearLayout的分割线
/**
* LinearLayout的divider只能是一个shapedrawable
* <shape>
* <solid android:color="#FFFF0000" />
* <size android:width="1dp"/>
* </shape>
* 设置:showDividers="middle" */
Fragment的向下兼容
/* 1.将引导的包从import android.app.Fragment;
改为import android.support.v4.app.Fragment;
是把所有有关fragment都都改为v4.app.fragment;
2.将activity改为Fragmentactivity
3.获取fragmentmanager的方法改为getSupportFragmentManager()
*/