2011-02-01 61 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中使用的對象不同的對象上調用的。