【问题标题】:Which Activity Life cycle gets called when app redirect to settings应用重定向到设置时调用哪个 Activity 生命周期
【发布时间】:2023-12-18 19:16:02
【问题描述】:

在我的应用程序中,如果未启用 gps,我将调用 gps 设置菜单。我在 Oncreate() 方法中调用了它。但是,我还想检查用户是否真的启用了它?

我的问题是当用户从设置菜单返回时调用哪个活动生命周期方法。

我尝试为 OnResume 方法编写代码。但是即使启用了 GPS,alertdilogue 也会不断弹出。

    if (gps.canGetLocation()) {

        latitude = gps.getLatitude();
        longitude = gps.getLongitude();
        altitude = gps.getAltitude();
        gpsLocation = "Latitude: " + latitude + ", Longitude:" + longitude
                + ",Altitude:" + altitude;
        System.out.println("GPS @ Avaialable Form"+gpsLocation);

        Logger.e(this, "GPS Location", gpsLocation);
    } else {
        gps.showSettingsAlert();
    }

我在 OnCreate() 和

中调用了这段代码
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    System.out.println("On Resume gets Called");


    if (gps.canGetLocation()) {

        latitude = gps.getLatitude();
        longitude = gps.getLongitude();
        altitude = gps.getAltitude();
        gpsLocation = "Latitude: " + latitude + ", Longitude:" + longitude
                + ",Altitude:" + altitude;
        System.out.println("GPS @ Avaialable Form"+gpsLocation);

        Logger.e(this, "GPS Location", gpsLocation);
    } else {
        gps.showSettingsAlert();
    }

在 OnResume() 方法中。

【问题讨论】:

  • 在应用导航到设置时调用 onstyop,如果您使用 startactivityfor resule 而不是 onactivity 结果调用,则在返回 onresume 时调用

标签: android android-activity gps lifecycle


【解决方案1】:

找出的一个好方法是使用内置的调用方法触发,当一个事件像 on pause() 被调用时,然后你记录事件的名称,然后你知道哪个类被触发到哪个时间.

【讨论】:

    【解决方案2】:

    据我所知,唯一可靠的方法是设置并通过侦听器 (GpsStatus.Listener) - LocationManager#addGpsStatusListener(...) 侦听 gps 变化。 在 onGpsStatusChanged 方法中,您可以对 GpsStatus#GPS_EVENT_STARTED 或 GpsStatus#GPS_EVENT_STOPPED 做出反应,并通过 Handler 更改 UI 的更新。希望这会有所帮助:-)

    【讨论】: