我将如何检测广播接收器中的运动传感器?
How i will detect motion sensor in broadcast receiver?
我在 Manifest.xml
中写了一个接收器来检测设备运动是否改变
<receiver android:name="com.hanuman.sensor.receiver.SensorReceiver" >
<intent-filter
android:enabled="true"
android:exported="false" >
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
并且在 receiver onReceive() 内部方法代码是:
String action = intent.getAction();
if (action.equals(Intent.ACTION_USER_PRESENT)) {
System.out.println("User is present");
Intent s = new Intent(context, MainActivity.class);
s.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(s);
} else {
System.out.println("User is not present");
}
最后我的问题是,当它被移动时没有检测到传感器,它检测到我的 设备处于解锁状态然后它调用我的 MainActivity,但我想检测当我的设备运动发生变化时,我想在接收器中进行检测。我该怎么做?
像下面这样获取 SensorManager 的对象-
SensorManager sensorMan = (SensorManager)getSystemService(SENSOR_SERVICE);
Sensor sensor = sensorMan.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
注册侦听器以侦听运动传感器的变化-
sensorMan.registerListener(context, sensor,
SensorManager.SENSOR_DELAY_UI);
然后覆盖onSensorChanged()方法检测变化-
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){
//do some stuff
}
//do some other code for other Sensor type
}
你不能也不应该这样做,因为当屏幕关闭时,传感器会自动关闭,这是由大多数设备中的系统完成的,一些设备支持屏幕关闭后的传感器侦听,如 nexus 4.But 您可以创建将收听屏幕关闭和屏幕打开的接收器。
public class 屏幕扩展 BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
screenOn();
}
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
screenOff();
}
}
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
intentFilter.addAction(Intent.ACTION_SCREEN_ON);
mReceiver = new Screen();
registerReceiver(mReceiver, intentFilter);
我在 Manifest.xml
中写了一个接收器来检测设备运动是否改变 <receiver android:name="com.hanuman.sensor.receiver.SensorReceiver" >
<intent-filter
android:enabled="true"
android:exported="false" >
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
并且在 receiver onReceive() 内部方法代码是:
String action = intent.getAction();
if (action.equals(Intent.ACTION_USER_PRESENT)) {
System.out.println("User is present");
Intent s = new Intent(context, MainActivity.class);
s.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(s);
} else {
System.out.println("User is not present");
}
最后我的问题是,当它被移动时没有检测到传感器,它检测到我的 设备处于解锁状态然后它调用我的 MainActivity,但我想检测当我的设备运动发生变化时,我想在接收器中进行检测。我该怎么做?
像下面这样获取 SensorManager 的对象-
SensorManager sensorMan = (SensorManager)getSystemService(SENSOR_SERVICE);
Sensor sensor = sensorMan.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
注册侦听器以侦听运动传感器的变化-
sensorMan.registerListener(context, sensor,
SensorManager.SENSOR_DELAY_UI);
然后覆盖onSensorChanged()方法检测变化-
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){
//do some stuff
}
//do some other code for other Sensor type
}
你不能也不应该这样做,因为当屏幕关闭时,传感器会自动关闭,这是由大多数设备中的系统完成的,一些设备支持屏幕关闭后的传感器侦听,如 nexus 4.But 您可以创建将收听屏幕关闭和屏幕打开的接收器。
public class 屏幕扩展 BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
screenOn();
}
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
screenOff();
}
}
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
intentFilter.addAction(Intent.ACTION_SCREEN_ON);
mReceiver = new Screen();
registerReceiver(mReceiver, intentFilter);