gpt4 book ai didi

安卓磨损 : How does it work an activity life cycle?

转载 作者:可可西里 更新时间:2023-10-31 22:03:28 28 4
gpt4 key购买 nike

我制作了一个带有 Activity 的可穿戴应用。

@Override
protected void onCreate(Bundle savedInstanceState) {
Log.i("WEAR", "CREATE");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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);
}
});
SensorManager sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
Sensor hrs = sm.getDefaultSensor(Sensor.TYPE_HEART_RATE);
sm.registerListener(hrListener, hrs, 3);

if (savedInstanceState != null) {
Log.i("WEAR", "RESTORE");
// ... get previous sensor data from the bundle

}
}

@Override
protected void onStop() {
Log.i("WEAR", "STOP");
super.onStop();
sm.unregisterListener(hrListener, hrs);
}

@Override
protected void onDestroy() {
Log.i("WEAR", "DESTROY");
super.onDestroy();
}

@Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
Log.i("WEAR", "SAVE");
// ... save sensor data in the bundle
super.onSaveInstanceState(savedInstanceState);
}

private SensorEventListener hrListener = new SensorEventListener() {
@Override
public void onSensorChanged(final SensorEvent event) {
final float hearRate = event.values[0];
Log.i("SENSOR", hearRate);
mTextView.setText(Float.toString(hearRate));
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {

}
};

当它启动时,我会看到来自传感器的数据流。一段时间后,屏幕返回到时钟表盘,日志在我的应用程序调试流程中显示停止并保存。我重新启动应用程序,但 bundle 为空,我丢失了 bundle 中保存的所有数据。 onDestroy 从未被调用,所以为什么我没有得到任何包?这是 Gear Live。

最佳答案

关闭wear app的 Action 应该是从左向右滑动来关闭它。您将看到 onDestroy 被调用。如果您只需按下按钮(我只有第一代 moto 360。即侧边按钮),就像将应用程序从前台关闭一样。所以只调用了 onStoponStart 对。希望这个迟到的答案仍然对您有所帮助。

关于安卓磨损 : How does it work an activity life cycle?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31117873/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com