我的代码可以在Android上运行吗?

问题描述 投票:0回答:1

我是Android的新手。这是我的第一个兼容的应用程序。我知道这是一个非常愚蠢的问题,但我不知道下面的代码是否适用于磨损(我没有真正的设备)。

    public class MainWearActivity extends Activity implements SensorEventListener {

    // record the compass picture angle turned
    private float currentDegree = 0f;
    private SensorManager mSensorManager;
    ImageView image;
    TextView tvHeading;
    TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
        stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
            @Override
            public void onLayoutInflated(WatchViewStub stub) {
                mTextView = (TextView) stub.findViewById(R.id.text);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        // for the system's orientation sensor registered listeners
        mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_GAME);
    }

    @Override
    protected void onPause() {
        super.onPause();
        // to stop the listener and save battery
        mSensorManager.unregisterListener(this);
    }

    @Override
    public void onSensorChanged(SensorEvent event) {

        // get the angle around the z-axis rotated
        float degree = event.values[0];
        if (Math.round(degree) == 360) {
            tvHeading.setText("0°");
        }else{
            tvHeading.setText(Math.round(degree) + "°");
        }
        // create a rotation animation (reverse turn degree degrees)
        RotateAnimation ra = new RotateAnimation(currentDegree, -degree, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        // how long the animation will take place
        ra.setDuration(210);
        // set the animation after the end of the reservation status
        ra.setFillAfter(true);
        // Start the animation
        currentDegree = -degree;
        image.startAnimation(ra);
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // Not in use
    }
}

更新:

现在,您可以使用虚拟传感器在模拟器上测试需要传感器的应用程序。

android wear-os
1个回答
0
投票

您没有Android Wear。但是您可以访问Android Wear的模拟器。看看Android Wear Emulator

更新SDK,设置Android Wear设备,您可以自行测试应用并验证代码。

希望这可能会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.