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

时间:2016-04-12 20:20:12

标签: android wear-os

我是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
    }
}

更新

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

1 个答案:

答案 0 :(得分:0)

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

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

希望这可能有所帮助。