Android Wear:它如何在活动生命周期中发挥作用?

时间:2015-06-29 14:08:46

标签: android android-activity wear-os android-sensors activity-lifecycle

我制作了一个可穿戴的应用程序。

List<int> list = new List<int>();

//this code is in a for-loop 
string a = console.ReadLine();
if(!int.TryParse(a, out number))
   Console.WriteLine("give a number");
else
{
    if (number < anotherlist.Count && number >= 0)
    {
        for (int i = 0; i < list.Count; i++)
        {
            if (list[i] == number)
                Console.WriteLine("number already given");//so dont add this number to the list
            else
                list.Add(number);
        }
    }
}

当它开始时,我看到来自传感器的数据流。 过了一会儿,屏幕返回时钟表盘,日志显示STOP和SAVE在我的app调试流程上。我重新启动应用程序,但捆绑包为空,我丢失了捆绑中保存的所有数据。 @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) { } }; 从未被调用过,为什么我不能获得任何捆绑? 它是Gear Live。

2 个答案:

答案 0 :(得分:2)

关闭磨损应用的操作应该是slide从左到右关闭它。您将看到onDestroy被调用。 如果你只是按下按钮(我只有一个moto 360 1st gen。即侧面按钮),就像把应用程序放在前台一样。因此,只需调用onStoponStart对。希望这个迟到的答案仍然会帮助你。

答案 1 :(得分:1)

您需要通过从左向右滑动或长按(需要代码相同)来添加完成应用。调用完成后,将调用onDestroy。