1

我可以在 MainActivity 类之外编写 MyLocationListener 吗?如下例

MainActivity.class

public class MainActivity extends Activity{

    @Override
    ..onCreate(){
    .
    .
    loclis = new MyLocationListener(this); // Can i use it this way? 
    //if so, where is the best place to declare this line. 

    }
}

MyLocationListener.class

public class MyLocationListener implement LocationListener{
..
.//Constructor of MyLocationListener
}

谢谢你的帮助

4

1 回答 1

2

是的,您可以将独立类声明为位置侦听器。但是,如果这样做,您可能希望有一种机制,允许您的侦听器在位置更改时回调您的活动(否则您的活动将永远不会知道您获得了位置更新):

public class MyLocationListener implements LocationListner{

   private SomeInterface owner;

   public MyLocationListener(SomeInterface owner){
       this.owner = owner;
} 

}

然后您的活动可能如下所示:

public class MainActivity extends Activity implements SomeInterface{

    private LocationListner locListener;
    public void onCreate(Bundle savedInstanceState){
        locListener = new MyLocationListener(this);
    }

}
于 2011-11-30T17:17:15.577 回答