2

我有一个GPS基于应用程序的工作。它有许多活动,并且都使用GPS. 例如,一个活动显示到某个位置的距离,另一个活动显示速度和方向。我设置并呼入requestLocationUpdates()onCreate()呼入removeUpdates()onPause()

这一切都有效,但GPS在切换活动时会闪烁并且必须重新获取。我做了一个测试,如果我把removeUpdates()inonStop()而不是 inonPause()没有闪烁。显然onStop()是在新活动开始后调用,这解释了没有闪烁。

我已阅读有关此主题的帖子,似乎存在意见分歧。但是,看起来我应该使用onStop()并且想知道是否有任何理由不应该使用。

第二个问题是没有onResume()代码,因此GPS在后退箭头或关闭和打开屏幕后没有代码。我应该解决这个问题。现在我的onCreate()代码创建了位置管理器,基本上我所有的代码都在里面。看起来我应该按照生命周期流程图移动removeUpdates()到 onStop 并将 lm 和 ll 的创建移动到onStart(). 它是否正确?

这是我的onCreate()代码:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.set);
    textView1 = (TextView)findViewById(R.id.textView1);
    lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    ll = new mylocationlistener();
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
}

这是我的 onPause 代码:

    @Override
    protected void onPause() {
        super.onPause();
        if(lm != null) {
            lm.removeUpdates(ll);
        }
        ll = null;
        lm = null;
    }

所以我认为我应该将 onPause() 更改为 onStop() 并将 onStart() 设为:

@Override
public void onStart() {
    super.onStart();
    lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    ll = new mylocationlistener();
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
}

我的 onCreate 代码是这样的;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.set);
    textView1 = (TextView)findViewById(R.id.textView1);
  }

This said, I am kind of new to all this and this is my first application so I would like to know what I am missing here.

4

3 回答 3

7

I've done something similar to your app, except that I've delegated the GPS functionality to a service which stops the GPS when no activities are bound to it and starts it when an activity binds to it. I bind to the service in the onStarts and unbind in the onStops. Logging the on.. methods when I switch activities gives a sequence like this:

FIRST Activity onCreate
FIRST Activity onStart
FIRST Activity onResume
FIRST Activity onPause
SECOND Activity onCreate
SECOND Activity onStart
SECOND Activity onResume
FIRST Activity onStop

If done it via a service so that I've only got one LocationManager to control, you could either take the same approach, or requestUpdates in the onStarts() and remove them in the onStops()

于 2012-07-27T17:42:55.443 回答
1

First of all I'd suggest following good read about location strategies.

If you just want a one-time location fix you need to make it a bit more independent from the activity life cycle because acquiring the GPS location can take longer than the user switches your activities.

So don't stop the request in onPause or onStop. You could use a special location query handler that lives as a singleton instance in your Application object (create a derivative of Application for that).

Tell that location handler to get a fresh fix if necessary in your activities' onStart or onResume method for example. Let the handler keep the latest and best location data which can be immediately used as needed.

You could query for the first time a NETWORK_PROVIDER location because this should be faster than GPS and you immediately have something to work with even though it's not accurate. On top of that you'd also get a location in case the user turned off the GPS module.

However, that depends on how accurate the result has to be. You could also create a local service that queries the GPS location and waits for its result to come. When the result is there you can notify your currently running activity via a local broadcast intent.

In any case, you can get the last known location with locationManager.getLastKnownLocation

于 2012-07-27T17:39:16.693 回答
0

First, I'm not 100% what question you're asking here, but I will try to help you out. OnPause is called whenever the Activity loses focus. This is primarily for popups and such that cause the Activity to be in the background, but blurred. OnStop is called whenever a new activity is started. You should put your location retrieval code in OnResume. OnResume is called any time that the application begins again. This includes any calls to OnCreate or OnStart. You should put your lm.removeUpdates(ll); logic in OnStop as well. This is the generally accept method for location retrieval in a life cycle; I just ended a sentence with a semicolon... I've been coding too much.

于 2012-07-27T17:56:24.027 回答