編輯:關於Android編程
下面這段話都能看懂吧~
Here I am explaining you how to use Light Sensor of Android.
1. Implement SensorEventListener
2. Create variable for SensorManager and Sensor
3. Get Object of SensorManager using system service
4. Get Object of Light Sensor from SensorManager
5. Implement onAccuracyChanged and onSensorChanged method
[java] package com.test;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
public class Test extends Activity implements SensorEventListener{
private SensorManager mSensorManager;
private Sensor mLight;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
mLight = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
}
@Override
protected void onResume() {
mSensorManager.registerListener(this, mLight, SensorManager.SENSOR_DELAY_NORMAL);
super.onResume();
}
@Override
protected void onPause() {
mSensorManager.unregisterListener(this);
super.onPause();
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
if(sensor.getType() == Sensor.TYPE_LIGHT){
Log.i("Sensor Changed", "Accuracy :" + accuracy);
}
}
public void onSensorChanged(SensorEvent event) {
if( event.sensor.getType() == Sensor.TYPE_LIGHT){
Log.i("Sensor Changed", "onSensor Change :" + event.values[0]);
}
}
}
package com.test;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
public class Test extends Activity implements SensorEventListener{
private SensorManager mSensorManager;
private Sensor mLight;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
mLight = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
}
@Override
protected void onResume() {
mSensorManager.registerListener(this, mLight, SensorManager.SENSOR_DELAY_NORMAL);
super.onResume();
}
@Override
protected void onPause() {
mSensorManager.unregisterListener(this);
super.onPause();
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
if(sensor.getType() == Sensor.TYPE_LIGHT){
Log.i("Sensor Changed", "Accuracy :" + accuracy);
}
}
public void onSensorChanged(SensorEvent event) {
if( event.sensor.getType() == Sensor.TYPE_LIGHT){
Log.i("Sensor Changed", "onSensor Change :" + event.values[0]);
}
}
}
所有傳感器:
[java] package androidegg.stu.sensor;
import java.util.List;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
public class AndroidEggSensorGetValueActivity extends Activity implements SensorEventListener{ //...①
/** Called when the activity is first created. */
private SensorManager sensorManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//SensorManager的接口取得
sensorManager = (SensorManager)this.getSystemService(SENSOR_SERVICE);
setContentView(R.layout.main);
}
@Override
protected void onResume(){ //...②
super.onResume();
List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ALL);
//sensor1
for (Sensor s : sensors){
sensorManager.registerListener(this,s,SensorManager.SENSOR_DELAY_NORMAL);
}
}
//
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent e) { //...③
// TODO Auto-generated method stub
switch(e.sensor.getType()){
//加速度的值表示
case Sensor.TYPE_ACCELEROMETER:{
TextView x = (TextView)findViewById(R.id.x);
x.setText("x:" + String.valueOf(e.values[SensorManager.DATA_X]));
TextView y = (TextView)findViewById(R.id.y);
y.setText("y:" + String.valueOf(e.values[SensorManager.DATA_Y]));
TextView z =(TextView)findViewById(R.id.z);
z.setText("z:" + String.valueOf(e.values[SensorManager.DATA_Z]));
break;
}
//傾斜度的值表示
case Sensor.TYPE_ORIENTATION:{
TextView x = (TextView)findViewById(R.id.Azimuth);
x.setText("Azimuth" + String.valueOf(e.values[SensorManager.DATA_X]));
TextView y = (TextView)findViewById(R.id.Pitch);
y.setText("Pitch:" + String.valueOf(e.values[SensorManager.DATA_Y]));
TextView z = (TextView)findViewById(R.id.Roll);
z.setText("Roll:" + String.valueOf(e.values[SensorManager.DATA_Z]));
break;
}
//磁力計 的值表示
case Sensor.TYPE_MAGNETIC_FIELD:{
TextView x = (TextView)findViewById(R.id.magnetic_x);
x.setText("x:" + String.valueOf(e.values[SensorManager.DATA_X]));
TextView y = (TextView)findViewById(R.id.magnetic_y);
y.setText("y:" + String.valueOf(e.values[SensorManager.DATA_Y]));
TextView z= (TextView)findViewById(R.id.magnetic_z);
z.setText("z:" + String.valueOf(e.values[SensorManager.DATA_Z]));
break;
}
//溫度計の値を表示する
case Sensor.TYPE_TEMPERATURE:{
TextView x = (TextView)findViewById(R.id.degree);
x.setText("Degree:" + String.valueOf(e.values[SensorManager.DATA_X]));
}
}
}
//
@Override
protected void onStop(){
super.onStop(); //...④
sensorManager.unregisterListener(this);
}
}
重要事項如下所示
① 為了取得傳感器的值,需要加載SensorEventListener
② 在onResume方法進行*********的登錄
③ 在onSensorChanged方法中取得傳感器的值,並在畫面表示。
在這個方法中在onResume方法中登錄的所有種類的傳感器的值都可以取道。判別傳感器的種類,將傳感器的值用
TextView在畫面表示出來。
④ 在onStop方法中接觸所有登錄的*********。
package androidegg.stu.sensor;
import java.util.List;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
public class AndroidEggSensorGetValueActivity extends Activity implements SensorEventListener{ //...①
/** Called when the activity is first created. */
private SensorManager sensorManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//SensorManager的接口取得
sensorManager = (SensorManager)this.getSystemService(SENSOR_SERVICE);
setContentView(R.layout.main);
}
@Override
protected void onResume(){ //...②
super.onResume();
List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ALL);
//sensor1
for (Sensor s : sensors){
sensorManager.registerListener(this,s,SensorManager.SENSOR_DELAY_NORMAL);
}
}
//
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent e) { //...③
// TODO Auto-generated method stub
switch(e.sensor.getType()){
//加速度的值表示
case Sensor.TYPE_ACCELEROMETER:{
TextView x = (TextView)findViewById(R.id.x);
x.setText("x:" + String.valueOf(e.values[SensorManager.DATA_X]));
TextView y = (TextView)findViewById(R.id.y);
y.setText("y:" + String.valueOf(e.values[SensorManager.DATA_Y]));
TextView z =(TextView)findViewById(R.id.z);
z.setText("z:" + String.valueOf(e.values[SensorManager.DATA_Z]));
break;
}
//傾斜度的值表示
case Sensor.TYPE_ORIENTATION:{
TextView x = (TextView)findViewById(R.id.Azimuth);
x.setText("Azimuth" + String.valueOf(e.values[SensorManager.DATA_X]));
TextView y = (TextView)findViewById(R.id.Pitch);
y.setText("Pitch:" + String.valueOf(e.values[SensorManager.DATA_Y]));
TextView z = (TextView)findViewById(R.id.Roll);
z.setText("Roll:" + String.valueOf(e.values[SensorManager.DATA_Z]));
break;
}
//磁力計 的值表示
case Sensor.TYPE_MAGNETIC_FIELD:{
TextView x = (TextView)findViewById(R.id.magnetic_x);
x.setText("x:" + String.valueOf(e.values[SensorManager.DATA_X]));
TextView y = (TextView)findViewById(R.id.magnetic_y);
y.setText("y:" + String.valueOf(e.values[SensorManager.DATA_Y]));
TextView z= (TextView)findViewById(R.id.magnetic_z);
z.setText("z:" + String.valueOf(e.values[SensorManager.DATA_Z]));
break;
}
//溫度計の値を表示する
case Sensor.TYPE_TEMPERATURE:{
TextView x = (TextView)findViewById(R.id.degree);
x.setText("Degree:" + String.valueOf(e.values[SensorManager.DATA_X]));
}
}
}
//
@Override
protected void onStop(){
super.onStop(); //...④
sensorManager.unregisterListener(this);
}
}
重要事項如下所示
① 為了取得傳感器的值,需要加載SensorEventListener
② 在onResume方法進行*********的登錄
③ 在onSensorChanged方法中取得傳感器的值,並在畫面表示。
在這個方法中在onResume方法中登錄的所有種類的傳感器的值都可以取道。判別傳感器的種類,將傳感器的值用
TextView在畫面表示出來。
④ 在onStop方法中接觸所有登錄的*********。
最近看了一些android的源碼,發現設計模式無處不在啊!感覺有點亂,於是決定要把設計模式好好梳理一下,於是有了這篇文章。1. Singleton(單例模式)作用:
Android中相機和相冊使用分析 歡迎轉載,但請尊重原創(文章來自不易,轉載請標明轉載出處,謝謝) 在手機應用程序中,使用自帶的相機拍照以及相冊選擇喜歡的圖片是
本文實例為大家分享了Android刮刮卡效果,供大家參考,具體內容如下android實現底層一張圖片,上層一個遮罩層,觸摸滑動按手指滑動路徑實現去除遮罩效果,類似於抽獎的
Android開發通用的工具類在開發中有些代碼都是重復性的,如果能把這些代碼集中的分類提取出來(比如網絡連接、數據保存等),然後再以後寫帶碼中,直接把這些代碼復制過來,然