gpt4 book ai didi

android - 加速度计似乎只能工作几秒钟?

转载 作者:行者123 更新时间:2023-11-29 20:09:56 25 4
gpt4 key购买 nike

在 Android Manifest 中:

<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) {

}
}

每次我重新启动手机时,都会重复记录“传感器已更改”输出几秒钟,然后停止。就好像 Android 中的某些东西正在检测一个无限循环并杀死应用程序。这可能吗?

这是 Samsung Galaxy S6 Edge 上的 Android 5.1.1。三星将节电系统作为标准配置 bundle 在一起,但禁用它似乎没有任何效果。

我认为可能发生的另一件事是我的 Manifest 接收器 block 中列出的两个操作是冲突的,即第一个触发器是创建监听器,第二个触发器然后使其崩溃,但我找到的所有示例列表两者并删除其中任何一个似乎都没有效果。

有什么想法吗?

最佳答案

这取自 Android DocumentationBroadcastReceiver 上。

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 不再处于 Activity 状态,因此系统会终止该进程。

要避免这种情况,您可以将 SensorChangedListener 移动到 Service 中,然后从 BroadcastReceiver 中启动 Service >。这样您将继续接收事件。

关于android - 加速度计似乎只能工作几秒钟?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35157178/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com