Android-如何在触发某个传感器时动态更改文本

Android-如何在触发某个传感器时动态更改文本,android,Android,这是我的onSensorChanged代码: @Override public void onSensorChanged(SensorEvent event) { if (sm.getSensorList(Sensor.TYPE_ACCELEROMETER).size() != 0) { dynamictext.setText("X: " + event.values[0] + " MS^-2 \nY: " + event.values[1] +

这是我的onSensorChanged代码:

 @Override
    public void onSensorChanged(SensorEvent event) {
        if (sm.getSensorList(Sensor.TYPE_ACCELEROMETER).size() != 0) {
            dynamictext.setText("X: " + event.values[0] + " MS^-2 \nY: " + event.values[1] + " MS^-2 \nZ: " + event.values[2] + " MS^-2");
        } else if (sm.getSensorList(Sensor.TYPE_ROTATION_VECTOR).size() != 0) {
            dynamictext.setText("X: " + event.values[0] + "\nY: " + event.values[1] + "\nZ: " + event.values[2] + "scalar: " + event.values[3]);}
如您所见,我正在检查传感器列表中是否存在传感器,然后更新dynamictext,但这不是我的意图

其目的是仅在触发传感器时更新此文本。用Psuedo编写,如下所示,例如:

如果(加速度计传感器被触发==加速度计){ 运行代码}

我试过:

if(this == TYPE_ACCELEROMETER){}
注意:HandlerThread用于我的传感器,因此我可以实际设置动态文本


我有点认为我需要通过比赛?如果您对此有任何澄清或想法,我们将不胜感激,谢谢

您不需要传递任何事件,因为此方法是自动调用的

确保已添加以下行:

s = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sm.registerListener(this, s, SensorManager.SENSOR_DELAY_GAME);
这是我更改文本视图的代码,您将了解:

package com.example.accel_dialer;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.net.Uri;
import android.os.Bundle;
import android.text.NoCopySpan.Concrete;
import android.widget.TextView;
import android.widget.Toast;

public class AccelDialActivity extends Activity implements SensorEventListener {
    SensorManager sm;
    Sensor s;
    Context cv = this;
    TextView tv1, tv2, tv3, tv4,tv5,tv6;
    double a, min = 20,max=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_accel_dial);
        sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

        s = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sm.registerListener(this, s, SensorManager.SENSOR_DELAY_GAME);
        tv1 = (TextView) findViewById(R.id.textView1);
        tv2 = (TextView) findViewById(R.id.textView2);
        tv3 = (TextView) findViewById(R.id.textView3);
        tv4 = (TextView) findViewById(R.id.textView4);
        tv5 = (TextView) findViewById(R.id.min);
        tv6 = (TextView) findViewById(R.id.max);

    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        // TODO Auto-generated method stub
        float ax, ay, az;

        ax = event.values[0];
        ay = event.values[1];
        az = event.values[2];
        a = Math.sqrt((ax * ax) + (ay * ay) + (az * az));
        if(a<min)
        {
            min=a;
        }
        if(a>max)
        {
            max=a;
        }

        tv1.setText("AX:" + ax);
        tv2.setText("Ay:" + ay);
        tv3.setText("Az:" + az);
        tv4.setText("A:" + a);
        tv5.setText(""+min);
        tv6.setText(""+max);
        if(a>40)
        {
            Intent i = new Intent(Intent.ACTION_CALL);
            i.setData(Uri.parse("tel:any contact number"));
            cv.startActivity(i);
        }

    }
}
package com.example.accel\u拨号器;
导入android.app.Activity;
导入android.content.Context;
导入android.content.Intent;
导入android.hardware.Sensor;
导入android.hardware.SensorEvent;
导入android.hardware.SensorEventListener;
导入android.hardware.SensorManager;
导入android.net.Uri;
导入android.os.Bundle;
导入android.text.NoCopySpan.Concrete;
导入android.widget.TextView;
导入android.widget.Toast;
公共类AccelDialActivity扩展活动实现SensorEventListener{
传感器管理器sm;
传感器;
上下文cv=此;
文本视图tv1、tv2、tv3、tv4、tv5、tv6;
双a,最小值=20,最大值=0;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(右布局、活动加速拨号);
sm=(SensorManager)getSystemService(Context.SENSOR\u服务);
s=sm.getDefaultSensor(传感器类型\加速计);
sm.registerListener(这个,s,SensorManager.SENSOR\u DELAY\u游戏);
tv1=(TextView)findViewById(R.id.textView1);
tv2=(TextView)findViewById(R.id.textView2);
tv3=(TextView)findViewById(R.id.textView3);
tv4=(TextView)findViewById(R.id.textView4);
tv5=(TextView)findViewById(R.id.min);
tv6=(TextView)findViewById(R.id.max);
}
@凌驾
精度更改时的公共无效(传感器,int精度){
//TODO自动生成的方法存根
}
@凌驾
传感器更改时的公共无效(传感器事件){
//TODO自动生成的方法存根
浮斧,ay,az;
ax=事件值[0];
ay=事件值[1];
az=事件值[2];
a=数学sqrt((ax*ax)+(ay*ay)+(az*az));
if(amax)
{
max=a;
}
tv1.setText(“AX:+AX”);
tv2.setText(“Ay:+Ay”);
tv3.setText(“Az:+Az”);
tv4.setText(“A:+A”);
tv5.setText(“+min”);
tv6.setText(“+max”);
如果(a>40)
{
意向i=新意向(意向.行动呼吁);
i、 setData(Uri.parse(“电话:任何联系号码”);
星触觉(i);
}
}
}

您不需要传递任何事件,因为此方法是自动调用的

确保已添加以下行:

s = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sm.registerListener(this, s, SensorManager.SENSOR_DELAY_GAME);
这是我更改文本视图的代码,您将了解:

package com.example.accel_dialer;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.net.Uri;
import android.os.Bundle;
import android.text.NoCopySpan.Concrete;
import android.widget.TextView;
import android.widget.Toast;

public class AccelDialActivity extends Activity implements SensorEventListener {
    SensorManager sm;
    Sensor s;
    Context cv = this;
    TextView tv1, tv2, tv3, tv4,tv5,tv6;
    double a, min = 20,max=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_accel_dial);
        sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

        s = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sm.registerListener(this, s, SensorManager.SENSOR_DELAY_GAME);
        tv1 = (TextView) findViewById(R.id.textView1);
        tv2 = (TextView) findViewById(R.id.textView2);
        tv3 = (TextView) findViewById(R.id.textView3);
        tv4 = (TextView) findViewById(R.id.textView4);
        tv5 = (TextView) findViewById(R.id.min);
        tv6 = (TextView) findViewById(R.id.max);

    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        // TODO Auto-generated method stub
        float ax, ay, az;

        ax = event.values[0];
        ay = event.values[1];
        az = event.values[2];
        a = Math.sqrt((ax * ax) + (ay * ay) + (az * az));
        if(a<min)
        {
            min=a;
        }
        if(a>max)
        {
            max=a;
        }

        tv1.setText("AX:" + ax);
        tv2.setText("Ay:" + ay);
        tv3.setText("Az:" + az);
        tv4.setText("A:" + a);
        tv5.setText(""+min);
        tv6.setText(""+max);
        if(a>40)
        {
            Intent i = new Intent(Intent.ACTION_CALL);
            i.setData(Uri.parse("tel:any contact number"));
            cv.startActivity(i);
        }

    }
}
package com.example.accel\u拨号器;
导入android.app.Activity;
导入android.content.Context;
导入android.content.Intent;
导入android.hardware.Sensor;
导入android.hardware.SensorEvent;
导入android.hardware.SensorEventListener;
导入android.hardware.SensorManager;
导入android.net.Uri;
导入android.os.Bundle;
导入android.text.NoCopySpan.Concrete;
导入android.widget.TextView;
导入android.widget.Toast;
公共类AccelDialActivity扩展活动实现SensorEventListener{
传感器管理器sm;
传感器;
上下文cv=此;
文本视图tv1、tv2、tv3、tv4、tv5、tv6;
双a,最小值=20,最大值=0;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(右布局、活动加速拨号);
sm=(SensorManager)getSystemService(Context.SENSOR\u服务);
s=sm.getDefaultSensor(传感器类型\加速计);
sm.registerListener(这个,s,SensorManager.SENSOR\u DELAY\u游戏);
tv1=(TextView)findViewById(R.id.textView1);
tv2=(TextView)findViewById(R.id.textView2);
tv3=(TextView)findViewById(R.id.textView3);
tv4=(TextView)findViewById(R.id.textView4);
tv5=(TextView)findViewById(R.id.min);
tv6=(TextView)findViewById(R.id.max);
}
@凌驾
精度更改时的公共无效(传感器,int精度){
//TODO自动生成的方法存根
}
@凌驾
传感器更改时的公共无效(传感器事件){
//TODO自动生成的方法存根
浮斧,ay,az;
ax=事件值[0];
ay=事件值[1];
az=事件值[2];
a=数学sqrt((ax*ax)+(ay*ay)+(az*az));
if(amax)
{
max=a;
}
tv1.setText(“AX:+AX”);
tv2.setText(“Ay:+Ay”);
tv3.setText(“Az:+Az”);
tv4.setText(“A:+A”);
tv5.setText(“+min”);
tv6.setText(“+max”);
如果(a>40)
{
意向i=新意向(意向.行动呼吁);
i、 setData(Uri.parse(“电话:任何联系号码”);
星触觉(i);
}
}
}

您不需要传递任何事件,因为此方法是自动调用的

确保已添加以下行:

s = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sm.registerListener(this, s, SensorManager.SENSOR_DELAY_GAME);
这是我更改文本视图的代码,您将了解:

package com.example.accel_dialer;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.net.Uri;
import android.os.Bundle;
import android.text.NoCopySpan.Concrete;
import android.widget.TextView;
import android.widget.Toast;

public class AccelDialActivity extends Activity implements SensorEventListener {
    SensorManager sm;
    Sensor s;
    Context cv = this;
    TextView tv1, tv2, tv3, tv4,tv5,tv6;
    double a, min = 20,max=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_accel_dial);
        sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

        s = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sm.registerListener(this, s, SensorManager.SENSOR_DELAY_GAME);
        tv1 = (TextView) findViewById(R.id.textView1);
        tv2 = (TextView) findViewById(R.id.textView2);
        tv3 = (TextView) findViewById(R.id.textView3);
        tv4 = (TextView) findViewById(R.id.textView4);
        tv5 = (TextView) findViewById(R.id.min);
        tv6 = (TextView) findViewById(R.id.max);

    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        // TODO Auto-generated method stub
        float ax, ay, az;

        ax = event.values[0];
        ay = event.values[1];
        az = event.values[2];
        a = Math.sqrt((ax * ax) + (ay * ay) + (az * az));
        if(a<min)
        {
            min=a;
        }
        if(a>max)
        {
            max=a;
        }

        tv1.setText("AX:" + ax);
        tv2.setText("Ay:" + ay);
        tv3.setText("Az:" + az);
        tv4.setText("A:" + a);
        tv5.setText(""+min);
        tv6.setText(""+max);
        if(a>40)
        {
            Intent i = new Intent(Intent.ACTION_CALL);
            i.setData(Uri.parse("tel:any contact number"));
            cv.startActivity(i);
        }

    }
}
package com.example.accel\u拨号器;
导入android.app.Activity;
导入android.content.Context;
导入android.content.Intent;
导入android.hardware.Sensor;
导入android.hardware.SensorEvent;
导入android.hardware.SensorEventListener;
导入android