在onPause停止GPS [英] OnPause to stop GPS

查看:181
本文介绍了在onPause停止GPS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新#2。我最初的问题是由两个谁回答的恩情解决。显然,我需要开始的事实,由此产生code,而它运行,不会停止对GPS的新问题。但是,没有任何错误,所以我现在将标志着这是解决了,如果我知道如何: - )

UPDATE #2. My initial question was solved by the kindness of the two who answered. Apparently I need to start a new question for the fact that the resulting code, while it runs, does not stop the GPS. But there are no errors now so I would mark this as solved if I knew how :-)

更新。

我已经工作code感谢意见的唯一问题是,它没有做什么是应该做的。它工作在没有错误。

I have "working" code thanks to comments the only problem is that it doesn't do what it is supposed to do. It is working in that there are no errors.

要使它工作,我说这在全球范围内活动的顶部。

To make it work, I added this globally at the top of the activity.

    protected LocationListener ll;
protected LocationManager lm;

和使用该的onPause code

And used this OnPause code

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

一切编译并运行在手机上。我得到显示GPS位置。麻烦的是,我们的目标是让GPS关闭,这样在离开该应用程序后,电池不去死。但GPS岿然不动。我认为这是一种常见的抱怨并没有看到一个答案。我以为lm.removeUpdates(LL)是解决方案,但它不工作。

Everything compiles and runs on a phone. I get GPS locations displayed. Trouble is that the goal is to get the GPS to turn off so that the battery doesn't go dead after leaving the application. But the GPS stays on. I see this is a common complaint and have not seen an answer. I thought lm.removeUpdates(ll) was the solution but it isn't working.

---------------原帖-------------------------

--------------- original post -------------------------

我想添加一些的onPause code停止我的GPS更新。我已经厌倦了计算器这两个问题和答案code中的至少一打的例子,所有在Eclipse中给出错误。

I want to add some OnPause code to stop my GPS updates. I have tired at least a dozen examples of code from stackoverflow both questions and answers and all give errors in Eclipse.

我的GPS code是在这里(正常工作):

My GPS code is here (it works fine):

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textLat = (TextView)findViewById(R.id.textLat);
    textLong = (TextView)findViewById(R.id.textLong);
    textTimex = (TextView)findViewById(R.id.textTimex);
    LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    LocationListener ll = new mylocationlistener();
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
}
    class mylocationlistener implements LocationListener{

        @Override
        public void onLocationChanged(Location location) {
            if (location != null)
            {
                double pLat = location.getLatitude();
                double pLong = location.getLongitude();
                long pTime = location.getTime();
                DecimalFormat df = new DecimalFormat("#.######");
                textLat.setText(df.format(pLat));
                textLong.setText(df.format(pLong));
                textTimex.setText(Long.toString(pTime));
                //textTimex.setText(Long.toString(pTime));
                //textTimex.setText(df.format(pLong));
            }
        }

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

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

        @Override
        public void onStatusChanged(String provider, int status,
                Bundle extras) {
            // TODO Auto-generated method stub
        } 
    }

我从网络上的例子以code和他们没有工作。这里有一个

I am taking code from examples on the web and none of them work. Here is one

@Override
public void onPause() {
    lm.removeUpdates(locationListener);
    super.onPause();
}

这真的不要紧,我称之为LM,它给出了一个错误,它无法解决它。不要紧,它例如code我用,他们都不是免费的错误。

It really doesn't matter what I call lm, it gives an error that it cannot resolve it. It doesn't matter which example code I use, none of them are error free.

我必须做一些非常愚蠢的简单...

I must be doing something very simply stupid...

推荐答案

声明 LocationListener的在全球范围(从活动的onCreate方法之外)为:

declare locationListener globally(outside from onCreate method of Activity) as:

    LocationListener ll;
     @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ////your code...
       ll = new mylocationlistener();
       ////your code..
    }


   @Override protected void onPause() 
 { 
  super.onPause();
   // TODO Auto-generated method stub 
   System.out.println("a intrat in onPause()"); 
  if(lm != null) { 
    lm.removeUpdates(ll); 
  } 
  ll = null; 
  lm = null;  
}

这篇关于在onPause停止GPS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆