8.1 安卓系统层实现人体感应功能

第一步:接收开机广播,启动人体感应服务

第二步:在服务BodyInductionService.java中处理人体感应,读取人体感应值/sys/devices/virtual/adw/adwdev/adwgpio1 ,读取到0为有人,1为没人;有人经过时唤醒设备,没人时30秒后休眠屏幕

BodyInductionService.java

    /**
     *method: onHandleIntent()  创建服务   /sys/class/gpio/gpio39/value  
     *@author qhj by 20200309
     */
    @Override
    protected void onHandleIntent(Intent arg0) {
        String bl_data = "1024";
        Log.i(TAG, "BodyInductionService-->onHandleIntent()------ by qhj");   

        
        while(bootBodyInductionServiceflag){
        
                   
                   int body_induction = 1;
                   Log.i(TAG,"------------body_induction=="+body_induction);
                   String body_value = adw_read_file("/sys/devices/virtual/adw/adwdev/adwgpio1");
                   Log.i(TAG,"------------body_value=="+body_value);
                   if(body_value.equals("0") && body_induction != 0 ){
                         Intent wakeupIntent = new Intent();
                         wakeupIntent.setAction("android.adw.intent.action.wakeup");
                         wakeupIntent.addFlags(0x01000000);
                         sendBroadcast(wakeupIntent);
                         sleepScreen(body_induction);
                         Log.i(TAG,"-----------body_induction!=0_start wakeup!!!");
                    }else if(body_induction == 0){
                         if (timer != null) {
                                   timer.cancel();
                                   timer = null;
                           }
                           if (task != null) {
                          
                                   task.cancel();
                                   task = null;
                           }
                         Log.i(TAG,"-----------body_induction==0 cancel task!!!");
                    }else if(body_value.equals("1")&& body_induction != 0){
                          
                        Log.i(TAG,"-----------body_induction!=0_body_value==0 no do!!!");
                    }
                
                SystemClock.sleep(1000);
        
        }         
    
    }
    
    
    
    /**
     *  无人时定时休眠 
     *  by qhj  20200309
     */
    public void sleepScreen(int time_induction) {
               if (timer == null) {
                       timer = new Timer();
               } else {
                       timer.cancel();
                       timer = null;
                       timer = new Timer();
               }
               if (task == null) {
 
                       task = new TimerTask() {
                               public void run() {
                                       Log.d("SVVV","--------------The_Device_is_Going_sleep___1111!!!");
                                       //isWakeDemoKey = true;
                                     PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
                                     pm.goToSleep(SystemClock.uptimeMillis());
                              }
                       };
 
               } else {
 
                       task.cancel();
                       task = null;
                       task = new TimerTask() {
                               public void run() {
                                       Log.d(TAG,"--------------The_Device_is_Going_sleep___2222!!!");
                                       //isWakeDemoKey = true;
                                     PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
                                     pm.goToSleep(SystemClock.uptimeMillis());
                                       

                               }
                       };
 
               }
                          Log.d(TAG,"--------time_induction==ms=="+time_induction * 30 * 1000);
               Date delay = new Date(System.currentTimeMillis() + time_induction * 30 * 1000);
 
               if (timer != null && task != null) {
                       timer.schedule(task, delay);
               }
 
    } 





    /*
     * 读取设备节点内容.
     */
    private static String adw_read_file(String path){
        String data = "1024";// 默认值
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(path));
            data = reader.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return data;
    }

注:唤醒是封装的广播接口

                         Intent wakeupIntent = new Intent();
                         wakeupIntent.setAction("android.adw.intent.action.wakeup");
                         wakeupIntent.addFlags(0x01000000);
                         sendBroadcast(wakeupIntent);

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值