1

LocationListener で onLocationChanged() を停止する方法、ボタンを押すと、 onLocationChanged の Toastbox が出続け、アプリケーションを離れても停止できません。

Button btn_location =(Button) findViewById(R.id.button4);
btn_location.setOnClickListener(new OnClickListener() {
public void onClick(View v) { 


               LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
               LocationListener mlocListener = new MyLocationListener(); 
               mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener); 
        }

    });

 public class MyLocationListener implements LocationListener     {  


            public void onLocationChanged(Location loc)       
            {   

                loc.getLatitude();         
                loc.getLongitude();          
                String Text = "My current location is: " +         "Latitud = " + loc.getLatitude() +         "Longitud = " + loc.getLongitude();          
                Toast.makeText( getApplicationContext(), Text, Toast.LENGTH_SHORT).show();       
             }        
            public void onProviderDisabled(String provider)       
            {         
                Toast.makeText( getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT ).show();       
             }        
            public void onProviderEnabled(String provider)       
            {         
                Toast.makeText( getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show();       
             }        
            public void onStatusChanged(String provider, int status, Bundle extras)       
            {        

            }     
         } 
4

2 に答える 2

0

次のコードは私のために働いた:

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    aLocationManager.removeUpdates(aLocationListener);
}

更新を停止したい場所で removeUpdates() メソッドを呼び出すだけです。

于 2014-04-18T14:22:51.403 に答える
0

これを適用できます:

mlocManager.removeUpdates(mlocListener);

ボタンに追加するか、onDestroy() または onPause() に入れます

参照: Android アクティビティ

于 2012-11-22T18:53:23.330 に答える