2011-02-01 64 views
1

我有一个应用程序,使用位置管理功能,它运行良好,直到应用程序停止或暂停。位置侦听器功能仍在后台进行。下面是相关的代码。当我单击home或back时,正在正确触发onstop()函数。我无法获取mlocManager.removeUpdates(mlocListener);工作

package uk.cr.anchor; 

import android.app.Activity; 
import android.content.Context; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.media.MediaPlayer; 
import android.media.RingtoneManager; 
import android.net.Uri; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.TableRow; 
import android.widget.Toast; 
import android.widget.ToggleButton; 
import android.content.SharedPreferences; 
import android.graphics.Color; 

public class main extends Activity { 

    /** Called when the activity is first created. */ 


    private LocationManager mlocManager; 
    private LocationListener mlocListener; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
     LocationListener mlocListener = new MyLocationListener(); 
     mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener); 
} 
    @Override 
    protected void onStop(){ 


     stoplistening(); 
     super.onStop(); 
    } 

/* Class My Location Listener */ 

public class MyLocationListener implements LocationListener 
{ 


    @Override 

    public void onLocationChanged(Location loc) 
    { 

     loc.getLatitude(); 

     loc.getLongitude(); 

     etc etc etc 
    } 
    private void stoplistening() { 
     if (mlocManager != null) { 
      Toast.makeText(getApplicationContext(), 
       "kill", 
       Toast.LENGTH_SHORT).show(); 
      mlocManager.removeUpdates(mlocListener); 
     } 
     else { 
      Toast.makeText(getApplicationContext(), 
       " not kill", 
       Toast.LENGTH_SHORT).show();  
     } 
    } 
} 

我总是得到“不杀”消息。

任何人都可以帮助我!

+0

在你的onCreate函数中,你声明了一个新的变量mlocManager和mlocListener。不要将该类型放在参数前面,因为您没有使用类中声明的类型(从onCreate开始)。 – 2012-10-16 16:54:57

回答

-2

我可以通过改变

private LocationManager mlocManager; 
private LocationListener mlocListener; 

private LM mlocManager; 
private LL mlocListener; 

并添加

LM = mlocManager; 
LL = mlocListener; 

然后更改为mlocManager其随后所有的引用LMal引用或mlocListener为了避开这个LL

虽然这个工作似乎很笨拙。我相信有更好的办法?

+0

什么是LM和LL?你是否在某个地方宣布这些课程?这是完全错误的。 – 2012-10-16 16:59:25

0

更改此

LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 

     LocationListener mlocListener = new MyLocationListener(); 

为了这

mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 

      mlocListener = new MyLocationListener(); 

对不起没有任何想法,为什么是这样,但它的作品。

1

在你给出的代码中,你在类的顶部声明了模块级变量,并且你希望它们被设置为位置管理器和侦听器。然而,在onCreate中,你声明了一些新的私有变量,它们恰好与模块级别的变量具有相同的名称。一旦onCreate执行完成,这些变量将从堆栈中取出,因此您将模块级别的变量留空,因为它们从未初始化。塞思的改变将解决这个问题。

0

这是一个范围界定问题。在你的onCreate函数中,你声明了新的变量mlocManager和mlocListener,它们只能在这个函数中生效,并且不能在其他地方看到。不要把这个类型放在你声明的参数前面,因为那样你实际上隐藏了你的类变量。您现在调用removeUpdates(mLocListener)的方法是在与您在onCreate中使用的对象不同的对象上调用的。