我的代码可以用于 Android 件衣服吗?

Will my code work on an Android wear?

我是 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 衣服。但是您可以访问 Android Wear 的模拟器。看看Android Wear Emulator

更新您的 SDK,设置 Android Wear 设备,您可以测试您的应用并通过以下方式验证您的代码你自己。

希望这对您有所帮助。