android传感器Gsensor和Psensor的使用举例
来源:互联网 发布:golang selenium爬虫 编辑:程序博客网 时间:2024/04/24 22:53
今天leader给的任务是这样的,做一个开机自启动的后台Service,实现对重力及距离的检测
当手机屏幕朝下时,让Psensor可用。否则不可用
实现代码如下:
SensorFunctionService.java
package com.sensor.demo;import android.app.Service;import android.content.Context;import android.content.Intent;import android.hardware.Sensor;import android.hardware.SensorEvent;import android.hardware.SensorEventListener;import android.hardware.SensorManager;import android.os.IBinder;import android.os.PowerManager;public class SensorFunctionService extends Service { private static final String LOG_TAG = "SensorDemoActivity";private static final int PROXIMITY_SCREEN_WAKE_LOCK = 32;private static final float CRITICAL_ANGLE = -5.0f;private static final int Z_ORATIATION = 2;private SensorManager mSensorManager;private Sensor mGsensor;private SensorEventListener mGsensorListener;private PowerManager pm;private PowerManager.WakeLock mProximityWakeLock;private boolean pSensorOpened; @Overridepublic void onCreate() {super.onCreate();pm = (PowerManager) getSystemService(Context.POWER_SERVICE); mProximityWakeLock = pm.newWakeLock(PROXIMITY_SCREEN_WAKE_LOCK, LOG_TAG); mSensorManager= (SensorManager) getSystemService(SENSOR_SERVICE); mGsensor = mSensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY); mGsensorListener = new SensorEventListener() {@Overridepublic void onAccuracyChanged(Sensor sensor, int accuracy) {}@Overridepublic void onSensorChanged(SensorEvent event) {if(event.values[Z_ORATIATION] < CRITICAL_ANGLE){//screen upif(!pSensorOpened){pSensorOpened = true;mProximityWakeLock.acquire();//open PSensor function}} else if(pSensorOpened){//screen downpSensorOpened = false;mProximityWakeLock.release();//close PSensor function}} }; mSensorManager.registerListener(mGsensorListener, mGsensor, SensorManager.SENSOR_DELAY_GAME);} @Overridepublic IBinder onBind(Intent arg0) {return null;}}
BootCompletedReceiver.java
package com.sensor.demo;import com.sensor.demo.SensorFunctionService;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;public class BootCompletedReceiver extends BroadcastReceiver{@Overridepublic void onReceive(Context context, Intent intent) {// TODO Auto-generated method stub Intent mIntent = new Intent(context, SensorFunctionService.class); context.startService(mIntent);}}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.sensor.demo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="7" /><uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.DEVICE_POWER" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <service android:name=".SensorFunctionService"></service> <receiver android:name=".BootCompletedReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> </application></manifest>
- android传感器Gsensor和Psensor的使用举例
- Android平台 Psensor传感器调试方法
- Android平台 Psensor传感器调试方法
- Android之传感器系统(Gsensor)
- Android之传感器系统(Gsensor)
- Android之传感器系统(Gsensor) .
- Android之传感器系统(Gsensor)
- Android之传感器系统(Gsensor)
- Android中传感器的使用:传感器和指南针
- android 传感器的使用
- android传感器的使用
- android 传感器的使用
- Android 传感器的使用
- Android 传感器的使用
- Android 传感器的使用
- Android传感器的使用
- Android传感器的使用(方向传感器,重力传感器)
- Android中传感器的使用
- android屏幕亮度的调整
- 传感器Sensor的应用:摇动手机
- C#设计模式之24——访问者模式
- 打印机的操作
- mvc与三层结构终极区别
- android传感器Gsensor和Psensor的使用举例
- 2012.3.5
- C#设计模式之——总结
- 转)服务器端注册脚本Page.ClientScript.RegisterStartupScript,RegisterClientScriptBlock,RegisterStartupScript,Re
- amoeba连接mysql--ERROR 2006 (HY000): MySQL server has gone away
- 用C生成简单格式的xml
- 多线程讲解
- 当红时尚造型师马锐的“美妆之路”
- socket 编程