当前位置: 首页 > 知识库问答 >
问题:

关于android生命周期

解晟睿
2023-03-14

我从这里的开发指南中读到了android活动的生命周期。现在我混淆了哪部分代码驻留在哪种方法中,比如onCreate、onStart、onResume、onRestart、onPause、onResume、onStop和OnDestroy。你能帮我把它们放在正确的地方吗。而且,即使应用程序最小化,跟踪也应该继续。我有以下代码。

public class MainActivity extends FragmentActivity implements  LocationListener {

//List of user defined variables

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    /*for now i  have kept everything in onCreate method
     * i have start and stop button to start the tracking and stop the tracking and show the distance of travel
    1. Checking whether GPS is on or OFF
    2. button = (ImageButton) findViewById(R.id.myButton);
    3. Code to load the Google map
    4. Now specified what start and stop button does.
        i. when i press start button it starts overlaying the path in the map and calculate distance travelled so far
        ii. when i press stop button it stops tracking and shows the details of final result in next activity.
    LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 2, this);
    */

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

@Override
public void onLocationChanged(Location arg0) {
    // TODO Auto-generated method stub
    //i hace code for tracking and overlaying here

}

@Override
public void onProviderDisabled(String arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
    // TODO Auto-generated method stub

}

}

共有1个答案

寿阳华
2023-03-14

基于我认为你想要的想法:“一个有开始记录和停止记录按钮的应用程序,同时使用谷歌地图显示路线。”正确?

您将需要两个组件:

服务(后台操作)

    null
    null
 类似资料:
  • 除了在上一节说到的4个函数,还有2个函数只用于处理单个线程的启动和关闭,他们只作用于线程环境。 首先,建立一个基本扩展,根据你PHP源码树使用下面几个源文件。 config.m4 PHP_ARG_ENABLE(sample4, [Whether to enable the "sample4" extension], [ enable-sample4 Enable "sample4"

  • 本文向大家介绍Android Service生命周期?相关面试题,主要包含被问及Android Service生命周期?时的应答技巧和注意事项,需要的朋友参考一下 onCreate() 首次创建服务时,系统将调用此方法。如果服务已在运行,则不会调用此方法,该方法只调用一次。 onStartCommand() 当另一个组件通过调用startService()请求启动服务时,系统将调用此方法。 onD

  • 本文向大家介绍Android Activity生命周期详解,包括了Android Activity生命周期详解的使用技巧和注意事项,需要的朋友参考一下 Activity 的生命周期。 一、理解Activity Activity是Android程序的4大组件之一。 Activity是Android程序的表示层。程序的每一个显示屏幕就是一个Activity。 学过WEB开发的同学,可以把Activit

  • 本文向大家介绍Android 服务的生命周期,包括了Android 服务的生命周期的使用技巧和注意事项,需要的朋友参考一下 示例 服务生命周期具有以下回调 onCreate() : 在首次创建服务以设置您可能需要的初始配置时执行。仅在服务尚未运行时才执行此方法。 onStartCommand() : 每次执行startService()都由另一个组件(例如Activity或BroadcastRec

  • 我为测试函数做了简单的应用程序,如onCreate()、onStart等。每个函数都让你有自己的日志。开始测试。 null 为什么在执行应用程序时调用onResume方法?(在a中) 为什么onPause和onStop方法一起调用?(在b中) 为什么在C中调用三个方法而不是onRestart或onrestume?(c) 为什么调用onDestroy方法?(d) 附言:对不起,英语:)

  • 我的问题是关于react中的生命周期方法,现在我有了这样的场景:一些数据由ajax在父组件的componentDidMount中获取,然后这些数据作为道具作为子组件传递,现在我们在子组件中,所以我们可以实现不同的生命周期方法, ComponentDidMount:无法看到道具的真值,例如,它中的日志日期返回空数组,而不是所需的数组 ComponentWillMount:相同。 ComponentW