标签: android-sensors

Android加速度计和磁传感器如何为方向信息做出贡献?

我知道如何从加速度计和磁场传感器获取方向矢量,但我不明白为什么需要来自磁场传感器的数据.有人能解释为什么吗?在相关的说明中,我是否需要在创建游戏时使用SENSOR_DELAY_GAME频率请求更新,或者可以在不显着影响响应性的情况下更频繁地更新(可能是磁场传感器)?

android sensor accelerometer orientation android-sensors

0
推荐指数
1
解决办法
6116
查看次数

IntentService STICKY和传感器监控在Android中不应该停止

在我的应用程序onCreate我检查一些条件,然后我开始这样的活动:

Intent startIntent = new Intent(getApplicationContext(), EnableLocationProviderActivity.class);
startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(startIntent);
Run Code Online (Sandbox Code Playgroud)

从该Activity开始,我启动一个IntentService,为传感器注册一些监听器,它以STICKY开头,意味着它应该被显式停止.IntentService监视传感器.

我的问题是,当我回到第一个Activity时,传感器不再感应了(我将一个Log.v放在onSensorChanged中(开始显示数据,然后停止).

如果我没有明确地停止它,为什么它会停止?此外,我看到有时会调用IntentService的OnDestroy,但是,如果它是STICKY并且我没有调用stopself()并且没有以任何其他方式停止,那么如何调用它?

谢谢!吉列尔莫.

编辑

这是IntentService的代码(它应该一直在运行,尽管手机进入睡眠状态或按下主页按钮(我知道电池和其他一切,用户将被警告这个并且会有机会)在他想要时关闭应用程序.

从MainActivity调用该服务,如下所示:

Intent startIntent = new Intent(GdpTesisApplication.getInstance().getApplicationContext(), SensingService.class);
startService(startIntent);
Run Code Online (Sandbox Code Playgroud)

服务代码是这样的:

public class SensingService extends IntentService implements SensorEventListener {
    private float[] mAccelerationValues;
    private SensorManager mSensorManager = null;
    String sensorType = "";

    public SensingService(String name) {
        super(name);
        setIntentRedelivery(true);
    }

    public SensingService() {
        super("SensingService");
        setIntentRedelivery(true);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.v(ApplicationName,"SensingService.onStartCommand");
        super.onStartCommand(intent, flags, startId); // If this is not written then onHandleIntent is …
Run Code Online (Sandbox Code Playgroud)

android android-intent android-sensors intentservice

0
推荐指数
1
解决办法
4879
查看次数

如何在单击按钮时注册或取消注册传感器事件侦听器?

我使用以下代码注册和注销SensorEventListener.

 //Get the Toggle Button
         final ToggleButton tb=(ToggleButton) findViewById(R.id.activate);
         //Listener for ToggleButton
         tb.setOnClickListener(new View.OnClickListener() {


             public void onClick(View arg0) {
                 if(tb.isChecked()){
                    //Register the sensor
                     //smanager.
                     smanager.registerListener(this, smanager.getDefaultSensor.TYPE_LINEAR_ACCELERATION,SensorManager.SENSOR_DELAY_NORMAL);
                     Log.v(classname, "Sensor Listener Unregistered");
                 }
                 else{
                      //deRegister the Sensor
                     // Unregister the listener
                     smanager.unregisterListener(this);
                     Log.v(classname, "Sensor Listener Unregistered");

                 }
             }
         });
Run Code Online (Sandbox Code Playgroud)


但是我收到以下错误.

The method registerListener(SensorListener, Sensor, int) is not applicable for the arguments new View.onClickListener(),{},Sensor,int
Run Code Online (Sandbox Code Playgroud)

onPause()在活动方法中编写相同的代码时,我没有收到此错误.有什么问题以及如何纠正这个问题?

android android-sensors

0
推荐指数
1
解决办法
5870
查看次数

Android Activity — NullPointerException(“无法实例化活动 ComponentInfo”)?

我正在尝试为 Android 创建一个程序。但是,每当我在 AVD 上运行它时,它都会在 LogCat 中返回一个错误,指出我的代码中存在 NullPointerException,因此“无法实例化活动 ComponentInfo”。

这是我的代码:

package com.example.sensor;

import android.app.Activity;   
import android.os.Bundle;   
import android.hardware.SensorManager;   
import android.hardware.Sensor;   
import android.hardware.SensorEventListener;   
import android.hardware.SensorEvent;   
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {   

    private SensorManager sensorMgr;
    private TextView result;
    Sensor sensor = sensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);   
    private float x, y, z;   
    protected void onCreate(Bundle savedInstanceState) {   

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        result = (TextView)findViewById(R.id.result);

        sensorMgr = (SensorManager)getSystemService(SENSOR_SERVICE);   
        SensorEventListener lsn = new SensorEventListener() {   
            @SuppressWarnings("deprecation")
            public void onSensorChanged(SensorEvent e) {   
            float[] values …
Run Code Online (Sandbox Code Playgroud)

android android-sensors sensormanager

0
推荐指数
1
解决办法
1949
查看次数

Android - 传感器或监听器为空

我试图在我的应用程序中实现一个传感器监听器,它会sensor or listener is null在该mSensorManager.registerListener(sensorEventListener, mSensor, mSensorManager.SENSOR_DELAY_FASTEST);行引发错误.这是我的完整代码:

    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));*/
    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_GAME_ROTATION_VECTOR);
    sensorEventListener = new SensorEventListener() {
        public void onSensorChanged(SensorEvent event) {
            double xAxis = event.values[0];
            TextView textView = (TextView) findViewById(R.id.rotation);
            textView.setText(String.valueOf(xAxis));
        }

        public void onAccuracyChanged(Sensor s, int i) {

        }
    };

    mSensorManager.registerListener(sensorEventListener, mSensor, mSensorManager.SENSOR_DELAY_FASTEST);
Run Code Online (Sandbox Code Playgroud)

我检查过,传感器实际上是null.我究竟做错了什么?谢谢.

编辑:该设备是第二代Moto G.

java android android-sensors

0
推荐指数
1
解决办法
9070
查看次数

错误:IllegalArgumentException:无法为retrofit2.Response 创建调用适配器

我已经通过本教程(http://www.duchess-france.org/accelerator-time-series-and-prediction-with-android-cassandra-and-spark/)创建了一个加速度计 Rest API,只是为了看看端点 ( http://192.168.0.104/acceleration )本地主机服务器上的数据值。但是我遇到了“无法为retrofit2.Response创建调用适配器”的错误

但是在教程中使用了 Retrofit (< 2.0),我使用的是 Retorfit2.0 (2.1)。因此,根据更新的库进行了很少的更改。

这是我下面的AccelerometerAct.java

package accelerometer.sensor.com.acceleration;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.Date;

import accelerometer.sensor.com.acceleration.model.Acceleration;
import retrofit2.Retrofit;



public class AccelerometerAct extends AppCompatActivity implements SensorEventListener{

    private String restURL;
    private TextView acceleration;
    private Button myStartButton;
    private Button myStopButton;

    private AccelerometerAPI accelerometerAPI;

    private SensorManager sm;
    private Sensor accelerometer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.accelerometer_activity); …
Run Code Online (Sandbox Code Playgroud)

android android-sensors restful-architecture retrofit2

0
推荐指数
1
解决办法
4549
查看次数

压力传感器没有得到任何值

我现在正在开发一个应用程序,其中一部分包括估计用户所在建筑物的楼层。我正在尝试在支持它的设备上使用气压计/压力传感器并尝试基于此进行估计离那。如果用户没有气压计/压力传感器,应用程序会打开一个对话框供手动输入,否则估计高度和楼层数。

现在,我只是想让气压计/压力传感器正常工作。我正在使用具有可以设置的压力值的 Android 模拟器,并且我的代码的相关部分设置如下:

主活动.java

public class MainActivity extends AppCompatActivity implements SensorEventListener, .... {

    //sensor variables
    public float mPressureValue = 0.0f;
    public float mHeight = 0.0f;
    public Integer pressureBasedFloor = 0;

    //check if device has pressure sensor, setup in OnCreate
    boolean hasBarometer = false;

    //pressure sensor to get pressure, height and floor
    @Override
    public void onSensorChanged(SensorEvent event) {
        //if you use this listener as listener of only one sensor (ex, Pressure), then you don't need to check sensor type.
        //if …
Run Code Online (Sandbox Code Playgroud)

android android-sensors

0
推荐指数
1
解决办法
1118
查看次数