加速度计似乎只能工作几秒钟?
Accelerometer seems to only work for a few seconds?
Android 清单中有这个:
<receiver android:name=".OnBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
和下面的 OnBootReceiver.class:
package io.cordova.hellocordova;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorManager;
public class OnBootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
SensorManager sManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
Sensor sensor = sManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sManager.registerListener(new ShakeEventManager(context), sensor, SensorManager.SENSOR_DELAY_NORMAL);
}
}
最后 ShakeEventManager.class:
package io.cordova.hellocordova;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.util.Log;
public class ShakeEventManager implements SensorEventListener {
private Context mContext;
public ShakeEventManager(Context context) {
mContext = context;
}
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
Log.i("testing", "Sensor changed");
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
每次我重新启动 phone 时,"Sensor changed" 输出都会重复记录几秒钟,然后停止。就好像 Android 中的某些东西正在检测无限循环并终止应用程序。这可能吗?
这是三星 Galaxy S6 Edge 上的 Android 5.1.1。三星标配了省电系统,但禁用它似乎没有效果。
我认为可能发生的另一件事是我的 Manifest 接收器块中列出的两个操作相互冲突,即第一个触发器是创建侦听器,第二个触发器然后使其崩溃,但我的所有示例都是查找列表并删除其中任何一个似乎再次无效。
有什么想法吗?
这取自 BroadcastReceiver
上的 Android Documentation。
A BroadcastReceiver object is only valid for the duration of the call
to onReceive(Context, Intent). Once your code returns from this
function, the system considers the object to be finished and no longer
active.
This has important repercussions to what you can do in an
onReceive(Context, Intent) implementation: anything that requires
asynchronous operation is not available, because you will need to
return from the function to handle the asynchronous operation, but at
that point the BroadcastReceiver is no longer active and thus the
system is free to kill its process before the asynchronous operation
completes.
这听起来像是您的情况。 BroadcastReceiver
不再活动,因此系统终止进程。
为避免这种情况,您可以将 SensorChangedListener
移动到 Service
中,然后从 BroadcastReceiver
中启动 Service
。这样您将继续收到事件。
Android 清单中有这个:
<receiver android:name=".OnBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
和下面的 OnBootReceiver.class:
package io.cordova.hellocordova;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorManager;
public class OnBootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
SensorManager sManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
Sensor sensor = sManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sManager.registerListener(new ShakeEventManager(context), sensor, SensorManager.SENSOR_DELAY_NORMAL);
}
}
最后 ShakeEventManager.class:
package io.cordova.hellocordova;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.util.Log;
public class ShakeEventManager implements SensorEventListener {
private Context mContext;
public ShakeEventManager(Context context) {
mContext = context;
}
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
Log.i("testing", "Sensor changed");
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
每次我重新启动 phone 时,"Sensor changed" 输出都会重复记录几秒钟,然后停止。就好像 Android 中的某些东西正在检测无限循环并终止应用程序。这可能吗?
这是三星 Galaxy S6 Edge 上的 Android 5.1.1。三星标配了省电系统,但禁用它似乎没有效果。
我认为可能发生的另一件事是我的 Manifest 接收器块中列出的两个操作相互冲突,即第一个触发器是创建侦听器,第二个触发器然后使其崩溃,但我的所有示例都是查找列表并删除其中任何一个似乎再次无效。
有什么想法吗?
这取自 BroadcastReceiver
上的 Android Documentation。
A BroadcastReceiver object is only valid for the duration of the call to onReceive(Context, Intent). Once your code returns from this function, the system considers the object to be finished and no longer active.
This has important repercussions to what you can do in an onReceive(Context, Intent) implementation: anything that requires asynchronous operation is not available, because you will need to return from the function to handle the asynchronous operation, but at that point the BroadcastReceiver is no longer active and thus the system is free to kill its process before the asynchronous operation completes.
这听起来像是您的情况。 BroadcastReceiver
不再活动,因此系统终止进程。
为避免这种情况,您可以将 SensorChangedListener
移动到 Service
中,然后从 BroadcastReceiver
中启动 Service
。这样您将继续收到事件。