加速度似乎仅几秒钟工作? [英] Accelerometer seems to only work for a few seconds?

查看:211
本文介绍了加速度似乎仅几秒钟工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有了这个Android清单:

With this in the 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:

And the following as 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:

And finally the following 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的东西是检测一个无限循环和杀害的应用程序。这可能吗?

Every time I reboot the phone, the "Sensor changed" output is logged repeatedly for a couple of seconds but then stops. It's as though something within Android is detecting an infinite loop and killing the app. Is that possible?

这是对三星Galaxy S6边缘的Andr​​oid 5.1.1。三星捆绑节省电池系统作为标准,但禁止这似乎没有任何效果。

This is Android 5.1.1 on a Samsung Galaxy S6 Edge. Samsung bundle a battery saving system as standard but disabling this seems to have no effect.

我想可能会发生的另一件事是,在我的清单接收块列出的两个动作是矛盾,即第一次触发是创造监听器和第二个触发然后崩溃,但所有我发现列表中的例子既和删除要么再次似乎没有任何效果。

Another thing I thought could be happening is that the two actions listed in my Manifest receiver block were conflicting, i.e. the first trigger was creating the listener and the second trigger then crashing it, but all of the examples I'm finding list both and removing either seems again to have no effect.

有什么想法吗?

推荐答案

这是摘自<一个href=\"http://developer.android.com/reference/android/content/BroadcastReceiver.html#ReceiverLifecycle\"相对=nofollow> Android文档的广播接收器

一个BroadcastReceiver对象仅适用于呼叫的持续时间
  到的onReceive(上下文,意图)。一旦从这个您code回报
  功能,系统认为对象被完成,​​并且不再
  活动的。

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.

这有你能在做什么重要的影响
  的onReceive(上下文,意图)执行:任何需要
  异步操作不可用,因为你需要
  从函数返回处理异步操作,但在
  该点的BroadcastReceiver不再有效并且因此
  系统是免费的异步操作之前杀死它的进程
  完成。

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.

这听起来像什么你的情况发生。在广播接收器不再有效,因此该系统终止进程。

That sounds like what is happening in your case. The BroadcastReceiver is no longer active so the system kills the process.

要避免这一点,你可以移动 SensorChangedListener 服务,然后启动服务从里面的广播接收器。这样,您将继续收到的事件。

To avoid this you can move the SensorChangedListener into a Service and then start the Service from inside the BroadcastReceiver. That way you will continue to receive the events.

这篇关于加速度似乎仅几秒钟工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆