安卓-光线传感器

<TextView
        android:id="@+id/txt_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25sp"
        android:text="光线传感器!" />

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="显示" />

    <ImageButton
        android:id="@+id/img_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/img_1"/>






import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.text.Layout;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity  implements SensorEventListener {

    private SensorManager senseormanager;
    private TextView txtview;
    private Button btn_values;
    private ImageView img_view;
    LinearLayout alyout;
    int i =1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txtview = findViewById(R.id.txt_view);
        btn_values = findViewById(R.id.button);
        img_view = findViewById(R.id.img_view);
        senseormanager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    }


    @Override
    protected void onResume(){
        super.onResume();
        senseormanager.registerListener(this,senseormanager.getDefaultSensor(Sensor.TYPE_LIGHT),SensorManager.SENSOR_DELAY_GAME);
    }

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

    @Override
    public  void onSensorChanged(SensorEvent event){
        btn_values.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                float[] values = event.values;
                int sensorType = event.sensor.getType();
                StringBuilder stringbuilder = null;
                if(sensorType==Sensor.TYPE_LIGHT){
                    stringbuilder = new StringBuilder();
                    stringbuilder.append("光的强度值:");
                    stringbuilder.append(values[0]);
                    txtview.setText(stringbuilder.toString());
                }

                if(i==1){
                    //alyout.setBackgroundResource(R.drawable.img_2);
                    img_view.setImageResource(R.drawable.img_2);
                    i *= -1;
                }else{
                    img_view.setImageResource(R.drawable.img_1);
                    i *= -1;
                }
            }
        });
    }

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

    }
}


public class MainActivity extends Activity implements SensorEventListener{ public static final String TAG = "SensorTest"; private SensorManager sensorManager; private TextView tv; private PowerManager localPowerManager = null;// 电源管理对象 private WakeLock localWakeLock = null;// 电源锁 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); tv = (TextView) findViewById(R.id.tv); localPowerManager = (PowerManager) getSystemService(POWER_SERVICE); // 获取PowerManager.WakeLock对象,后面的参数|表示同时传入两个值,最后的是LogCat里用的Tag localWakeLock = this.localPowerManager.newWakeLock(32, "hahaha");// 第一个参数为电源锁级别,第二个是日志tag } @Override protected void onResume() { sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY), SensorManager.SENSOR_DELAY_UI); super.onResume(); } @Override protected void onPause() { sensorManager.unregisterListener(this); super.onPause(); } @Override public void onSensorChanged(SensorEvent event) { // TODO Auto-generated method stub float[] values = event.values; switch (event.sensor.getType()) { case Sensor.TYPE_PROXIMITY: tv.setText(values[0] + ""); if (values[0] == 0.0) {// 贴近手机 System.out.println("hands up"); Log.d(TAG, "hands up in calling activity"); if (localWakeLock.isHeld()) { return; } else { localWakeLock.acquire();// 申请设备电源锁 } } else {// 远离手机 System.out.println("hands moved"); Log.d(TAG, "hands moved in calling activity"); if (localWakeLock.isHeld()) { return; } else { localWakeLock.setReferenceCounted(false); localWakeLock.release(); // 释放设备电源锁 } break; } } }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值