Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Onsensorchanged陀螺仪在后台一分钟后未接收到任何数据_Android_Android Service_Android Sensors - Fatal编程技术网

Android Onsensorchanged陀螺仪在后台一分钟后未接收到任何数据

Android Onsensorchanged陀螺仪在后台一分钟后未接收到任何数据,android,android-service,android-sensors,Android,Android Service,Android Sensors,因此,当应用程序处于前台,而第一分钟处于后台时,它可以正常工作。但在此之后,监听器停止接收陀螺数据。但是post线程和位置侦听器仍在工作。 我尝试了很多东西,但都没找到工作 public final class Secret extends Service implements SensorEventListener, LocationListener { private final String create = "CREATE TABLE IF NOT EXISTS data(lat

因此,当应用程序处于前台,而第一分钟处于后台时,它可以正常工作。但在此之后,监听器停止接收陀螺数据。但是post线程和位置侦听器仍在工作。

我尝试了很多东西,但都没找到工作

public final class Secret extends Service implements SensorEventListener, LocationListener {
    private final String create = "CREATE TABLE IF NOT EXISTS data(lat float, lon float, x float, y float, z float, timestamp bigint)";
    private SensorManager sensorManager;
    private Database database;
    private Location location;

    @Override
    public final void onCreate() {
        /* Init sensor manager */
        this.sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    }

    @Override
    public final int onStartCommand(final Intent intent, final int flags, final int startId) {
        /* Initialize & load database */
        this.database = new Database(openOrCreateDatabase("name_data.db", MODE_PRIVATE, null));
        this.database.exec(this.create);

        /* Init location manager */
        final LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        try {
            /* Register sensor change listener */
            this.sensorManager.registerListener(this, this.sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY), SensorManager.SENSOR_STATUS_ACCURACY_HIGH);

            /* Register location change listener */
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 1, this);
        } catch (final SecurityException e) {
            Toast.makeText(this, "Failed to start: " + e.getMessage(), Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }

        /* Post data every 5 minutes */
        final Thread thread = new Thread(new Runnable() {
            @Override
            public final void run() {
                /* Secret instance */
                final Secret secret = Secret.this;

                /* 5 minutes */
                final long time = 15000;

                while (true) {
                    try {
                        /* Wait 5 minutes */
                        Thread.sleep(time);

                        /*
                         * Unregister sensor change listener,
                         * this way posting wont cause any database errors,
                         * and the sensor events wont time out when app is running in the background
                         */
                        sensorManager.unregisterListener(secret);

                        /* Post database */
                        final int returned = post();

                        /* Register sensor change listener */
                        sensorManager.registerListener(secret, sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE), SensorManager.SENSOR_STATUS_ACCURACY_HIGH);

                        switch (returned) {
                            case -1:
                                System.err.println("Database too small to post");
                                break;

                            case HttpURLConnection.HTTP_OK:
                                System.out.println("Posted database");
                                break;

                            default:
                                System.err.println("Post failed: " + returned);
                        }
                    } catch (final InterruptedException e) {
                        System.err.println("Failed to sleep");
                        e.printStackTrace();
                    }
                }
            }
        });

        /* Set thread priority and start */
        thread.setPriority(Thread.MIN_PRIORITY);
        thread.start();
        return START_STICKY;
    }

    @Nullable
    @Override
    public final IBinder onBind(final Intent intent) {
        return null;
    }

    @Override
    public final void onSensorChanged(final SensorEvent event) {
        System.out.println(System.currentTimeMillis());
        /* Store data if location is known */
        if (event == null || this.location == null) return;
        float[] gyro = event.values;

        /* Insert data into database */
        this.database.exec("INSERT INTO data(lat, lon, x, y, z, timestamp) VALUES(" + this.location.getLatitude() + "," + this.location.getLongitude() + "," + gyro[0] + "," + gyro[1] + "," + gyro[2] + "," + System.currentTimeMillis() + ")");
    }

    @Override
    public final void onAccuracyChanged(final Sensor sensor, final int accuracy) {
    }

    @Override
    public final void onLocationChanged(final Location location) {
        this.location = location;
    }

    @Override
    public final void onStatusChanged(final String provider, final int status, final Bundle extras) {
    }

    @Override
    public final void onProviderEnabled(final String provider) {
    }

    @Override
    public final void onProviderDisabled(final String provider) {
    }

    /**
     * Post database to server
     */
    private final int post() {
        /* Check if database is large enough (8 MB) to be pushed */
        if (this.database.getFile().length() <= 8192) return -1;

        /* Push database to server */
        final int returned = Connection.post(this.database.getFile());

        /* Check if database was pushed successfully */
        if (returned == HttpURLConnection.HTTP_OK) {
            /* Clear database if it was pushed to server successfully */
            this.database.close();
            this.database.getFile().delete();

            /* Create new database */
            this.database = new Database(openOrCreateDatabase("name_data.db", MODE_PRIVATE, null));
            this.database.exec(this.create);
        }

        return returned;
    }
}
public final class Secret extends服务实现SensorEventListener、LocationListener{
private final String create=“创建表,如果不存在数据(lat float、lon float、x float、y float、z float、timestamp bigint)”;
私人传感器管理器传感器管理器;
专用数据库;
私人位置;
@凌驾
公共最终void onCreate(){
/*初始化传感器管理器*/
this.sensorManager=(sensorManager)getSystemService(Context.SENSOR\u SERVICE);
}
@凌驾
公共最终int onStartCommand(最终意图、最终int标志、最终int startId){
/*初始化并加载数据库*/
this.database=新数据库(openOrCreateDatabase(“name_data.db”,MODE_PRIVATE,null));
this.database.exec(this.create);
/*初始化位置管理器*/
最终位置管理器位置管理器=(位置管理器)getSystemService(Context.LOCATION\u服务);
试一试{
/*寄存器传感器更改侦听器*/
this.sensorManager.registerListener(this,this.sensorManager.getDefaultSensor(Sensor.TYPE\u approxity)、sensorManager.Sensor\u STATUS\u currency\u HIGH);
/*寄存器位置更改侦听器*/
locationManager.RequestLocationUpdate(locationManager.GPS_提供程序,0,1,此);
}捕获(最终安全异常e){
Toast.makeText(此“启动失败:”+e.getMessage(),Toast.LENGTH_LONG.show();
e、 printStackTrace();
}
/*每5分钟发布一次数据*/
最终线程线程=新线程(新可运行(){
@凌驾
公开最终作废运行(){
/*秘密实例*/
最后的秘密=秘密;
/*5分钟*/
最终长时间=15000;
while(true){
试一试{
/*等5分钟*/
睡眠(时间);
/*
*取消注册传感器更改侦听器,
*这种方式不会导致任何数据库错误,
*当应用程序在后台运行时,传感器事件不会超时
*/
sensorManager.UnregistereListener(机密);
/*邮政数据库*/
返回的final int=post();
/*寄存器传感器更改侦听器*/
sensorManager.registerListener(机密、sensorManager.getDefaultSensor(传感器类型\陀螺仪)、sensorManager.Sensor\状态\精度\高);
开关(返回){
案例1:
System.err.println(“数据库太小,无法发布”);
打破
案例HttpURLConnection.HTTP\u确定:
System.out.println(“发布的数据库”);
打破
违约:
System.err.println(“Post失败:+返回”);
}
}捕获(最终中断异常e){
System.err.println(“无法睡眠”);
e、 printStackTrace();
}
}
}
});
/*设置线程优先级并启动*/
线程设置优先级(线程最小优先级);
thread.start();
返回开始时间;
}
@可空
@凌驾
公共最终IBinder onBind(最终目的){
返回null;
}
@凌驾
传感器更改后的公共最终无效(最终传感器事件){
System.out.println(System.currentTimeMillis());
/*如果位置已知,则存储数据*/
if(event==null | | this.location==null)返回;
浮动[]陀螺=事件值;
/*将数据插入数据库*/
this.database.exec(“插入数据(纬度、经度、x、y、z、时间戳)值(“+this.location.getLatitude()+”、“+this.location.getLatitude()+”、“+gyro[0]+”、“+gyro[1]+”、“+gyro[2]+”、“+System.currentTimeMillis()+”);
}
@凌驾
公共最终无效OnAccuracy更改(最终传感器、最终int精度){
}
@凌驾
公共最终无效位置已更改(最终位置){
这个位置=位置;
}
@凌驾
公共最终void onStatusChanged(最终字符串提供程序、最终int状态、最终Bundle附加){
}
@凌驾
公共最终无效onProviderEnabled(最终字符串提供程序){
}
@凌驾
公共最终无效onProviderDisabled(最终字符串提供程序){
}
/**
*将数据库发布到服务器
*/
私人最终内部员额(){
/*检查数据库是否足够大(8MB)以进行推送*/

if(this.database.getFile().length())您能否在生命周期方法中添加一些日志记录,并检查系统是否在传感器读数停止之前调用了
服务
ondestory
方法?您能否在生命周期方法中添加一些日志记录,并检查
服务
ondestory
方法是否由系统在传感器之前调用你不来了吗?