我开发了一个应用程序来收集加速度计数据,即使该应用程序处于后台。当应用程序进入后台时,我能够运行代码,但是加速度计OnSensorChanged仅在应用程序位于前台时才触发。当应用程序在后台运行时,不会触发,导致返回0.0(零)值。
我使用IntentService实现了它,还使用了BroadcastReceiver。
@Override
protected void onHandleIntent(@Nullable Intent intent) {
id = CmnFuncs.getAndroidDeviceID(this);
address = CmnFuncs.getMACAddr(this);
mSensorManager = (SensorManager) this.getSystemService(Context.SENSOR_SERVICE);
Log.d(TagUtils.getTag(), "Acc Service Started");
Constants.IS_RUNNING_ACCSERVICE = true;
synchronized (this) {
int count = 0;
int WINDOW = 60;
int RATE = 2;
while (count < (WINDOW / RATE)) {
try {
mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
wait(RATE * 1000);
++count;
time = System.currentTimeMillis() / 1000;
StringBuffer sb = new StringBuffer();
sb.append(id + ", " + time + ", " + x + ", " + y + ", " + z + "\n");
if (x == 0.0 && y == 0.0 && z == 0.0) {
Log.d(TagUtils.getTag() + "Acc Data Not Saved ("+count+"): ", id + ", " + time + ", " + x + ", " + y + ", " + z + "\n");
} else {
content = sb.toString();
if (FileUtils.writeIntoFile(Constants.FILE_ACC, content)) {
Log.d(TagUtils.getTag() + "Acc Data Saved ("+count+"): ", content);
}
}
mSensorManager.unregisterListener(this);
} catch (InterruptedException e) {
e.printStackTrace();
Log.d(TagUtils.getTag(), "Something went wrong!");
}
}
}
}
@Override
public void onSensorChanged(SensorEvent event) {
Sensor sensor = event.sensor;
if (sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
getAccelerometer(event);
}
}
private void getAccelerometer(SensorEvent event) {
float[] values = event.values;
x = values[0];
y = values[1];
z = values[2];
Constants.ACC_X = x;
Constants.ACC_Y = y;
Constants.ACC_Z = z;
}
在应用程序进入后台时获取
D/(AccService.java:90): Acc Service Started
D/(AccService.java:113)Acc Data Not Saved (1):: b5653482690c7a2a, 1557920074, 0.0, 0.0, 0.0
D/(AccService.java:113)Acc Data Not Saved (2):: b5653482690c7a2a, 1557920076, 0.0, 0.0, 0.0
D/(AccService.java:113)Acc Data Not Saved (3):: b5653482690c7a2a, 1557920078, 0.0, 0.0, 0.0
D/(AccService.java:113)Acc Data Not Saved (4):: b5653482690c7a2a, 1557920080, 0.0, 0.0, 0.0
D/(AccService.java:113)Acc Data Not Saved (5):: b5653482690c7a2a, 1557920082, 0.0, 0.0, 0.0
D/(AccService.java:113)Acc Data Not Saved (6):: b5653482690c7a2a, 1557920084, 0.0, 0.0, 0.0
D/(AccService.java:113)Acc Data Not Saved (7):: b5653482690c7a2a, 1557920086, 0.0, 0.0, 0.0
I/ActivityManager: Waited long enough for: ServiceRecord{c348ec2 u0 com.example.android.samplesen/.services.AccService}
D/(AccService.java:113)Acc Data Not Saved (8):: b5653482690c7a2a, 1557920088, 0.0, 0.0, 0.0
D/(AccService.java:113)Acc Data Not Saved (9):: b5653482690c7a2a, 1557920090, 0.0, 0.0, 0.0
D/(AccService.java:113)Acc Data Not Saved (10):: b5653482690c7a2a, 1557920092, 0.0, 0.0, 0.0
D/(AccService.java:113)Acc Data Not Saved (11):: b5653482690c7a2a, 1557920094, 0.0, 0.0, 0.0
D/(AccService.java:113)Acc Data Not Saved (12):: b5653482690c7a2a, 1557920096, 0.0, 0.0, 0.0
D/(AccService.java:113)Acc Data Not Saved (13):: b5653482690c7a2a, 1557920098, 0.0, 0.0, 0.0
D/(AccService.java:113)Acc Data Not Saved (14):: b5653482690c7a2a, 1557920100, 0.0, 0.0, 0.0
D/(AccService.java:113)Acc Data Not Saved (15):: b5653482690c7a2a, 1557920102, 0.0, 0.0, 0.0
D/(AccService.java:113)Acc Data Not Saved (16):: b5653482690c7a2a, 1557920104, 0.0, 0.0, 0.0
D/(AccService.java:113)Acc Data Not Saved (17):: b5653482690c7a2a, 1557920107, 0.0, 0.0, 0.0
D/(AccService.java:113)Acc Data Not Saved (18):: b5653482690c7a2a, 1557920109, 0.0, 0.0, 0.0
D/(AccService.java:113)Acc Data Not Saved (19):: b5653482690c7a2a, 1557920111, 0.0, 0.0, 0.0
D/(AccService.java:113)Acc Data Not Saved (20):: b5653482690c7a2a, 1557920113, 0.0, 0.0, 0.0
D/(AccService.java:113)Acc Data Not Saved (21):: b5653482690c7a2a, 1557920115, 0.0, 0.0, 0.0
D/(AccService.java:113)Acc Data Not Saved (22):: b5653482690c7a2a, 1557920117, 0.0, 0.0, 0.0
D/(AccService.java:113)Acc Data Not Saved (23):: b5653482690c7a2a, 1557920119, 0.0, 0.0, 0.0
D/(AccService.java:113)Acc Data Not Saved (24):: b5653482690c7a2a, 1557920121, 0.0, 0.0, 0.0
D/(AccService.java:113)Acc Data Not Saved (25):: b5653482690c7a2a, 1557920123, 0.0, 0.0, 0.0
D/(AccService.java:113)Acc Data Not Saved (26):: b5653482690c7a2a, 1557920125, 0.0, 0.0, 0.0
D/(AccService.java:113)Acc Data Not Saved (27):: b5653482690c7a2a, 1557920127, 0.0, 0.0, 0.0
D/(AccService.java:113)Acc Data Not Saved (28):: b5653482690c7a2a, 1557920129, 0.0, 0.0, 0.0
D/(AccService.java:113)Acc Data Not Saved (29):: b5653482690c7a2a, 1557920131, 0.0, 0.0, 0.0
D/(AccService.java:113)Acc Data Not Saved (30):: b5653482690c7a2a, 1557920133, 0.0, 0.0, 0.0
D/(AccService.java:67): Acc Service Destroyed
在前景中使用该应用程序时获取
D/(AccService.java:97): Acc Service Started
D/(AccService.java:126)Acc Data Saved (1):: b5653482690c7a2a, 1557982553, 0.0, 9.77631, 0.812349
D/(AccService.java:126)Acc Data Saved (2):: b5653482690c7a2a, 1557982555, 0.0, 9.77631, 0.812349
D/(AccService.java:126)Acc Data Saved (3):: b5653482690c7a2a, 1557982557, 0.0, 9.77631, 0.812349
D/(AccService.java:126)Acc Data Saved (4):: b5653482690c7a2a, 1557982559, 0.0, 9.77631, 0.812349
D/(AccService.java:126)Acc Data Saved (5):: b5653482690c7a2a, 1557982561, 0.0, 9.77631, 0.812349
D/(AccService.java:126)Acc Data Saved (6):: b5653482690c7a2a, 1557982563, 0.0, 9.77631, 0.812349
D/(AccService.java:126)Acc Data Saved (7):: b5653482690c7a2a, 1557982565, 0.0, 9.77631, 0.812349
D/(AccService.java:126)Acc Data Saved (8):: b5653482690c7a2a, 1557982567, 0.0, 9.77631, 0.812349
D/(AccService.java:126)Acc Data Saved (9):: b5653482690c7a2a, 1557982569, 0.0, 9.77631, 0.812349
D/(AccService.java:126)Acc Data Saved (10):: b5653482690c7a2a, 1557982571, 0.0, 9.77631, 0.812349
D/(AccService.java:126)Acc Data Saved (11):: b5653482690c7a2a, 1557982573, 0.0, 9.77631, 0.812349
D/(AccService.java:126)Acc Data Saved (12):: b5653482690c7a2a, 1557982576, 0.0, 9.77631, 0.812349
D/(AccService.java:126)Acc Data Saved (13):: b5653482690c7a2a, 1557982578, 0.0, 9.77631, 0.812349
D/(AccService.java:126)Acc Data Saved (14):: b5653482690c7a2a, 1557982580, 0.0, 9.77631, 0.812349
D/(AccService.java:126)Acc Data Saved (15):: b5653482690c7a2a, 1557982582, 0.0, 9.77631, 0.812349
D/(AccService.java:126)Acc Data Saved (16):: b5653482690c7a2a, 1557982584, 0.0, 9.77631, 0.812349
D/(AccService.java:126)Acc Data Saved (17):: b5653482690c7a2a, 1557982586, 0.0, 9.77631, 0.812349
D/(AccService.java:126)Acc Data Saved (18):: b5653482690c7a2a, 1557982588, 0.0, 9.77631, 0.812349
D/(AccService.java:126)Acc Data Saved (19):: b5653482690c7a2a, 1557982590, 0.0, 9.77631, 0.812349
D/(AccService.java:126)Acc Data Saved (20):: b5653482690c7a2a, 1557982592, 0.0, 9.77631, 0.812349
D/(AccService.java:126)Acc Data Saved (21):: b5653482690c7a2a, 1557982594, 0.0, 9.77631, 0.812349
D/(AccService.java:126)Acc Data Saved (22):: b5653482690c7a2a, 1557982596, 0.0, 9.77631, 0.812349
D/(AccService.java:126)Acc Data Saved (23):: b5653482690c7a2a, 1557982598, 0.0, 9.77631, 0.812349
D/(AccService.java:126)Acc Data Saved (24):: b5653482690c7a2a, 1557982600, 0.0, 9.77631, 0.812349
D/(AccService.java:126)Acc Data Saved (25):: b5653482690c7a2a, 1557982602, 0.0, 9.77631, 0.812349
D/(AccService.java:126)Acc Data Saved (26):: b5653482690c7a2a, 1557982604, 0.0, 9.77631, 0.812349
D/(AccService.java:126)Acc Data Saved (27):: b5653482690c7a2a, 1557982606, 0.0, 9.77631, 0.812349
D/(AccService.java:126)Acc Data Saved (28):: b5653482690c7a2a, 1557982608, 0.0, 9.77631, 0.812349
D/(AccService.java:126)Acc Data Saved (29):: b5653482690c7a2a, 1557982610, 0.0, 9.77631, 0.812349
D/(AccService.java:126)Acc Data Saved (30):: b5653482690c7a2a, 1557982612, 0.0, 9.77631, 0.812349
D/(AccService.java:70): Acc Service Destroyed