[Android] simple interface callback

Keywords: Mobile SDK

Application scenario

Tool class encapsulation
Take Chestnut: Baidu positioning

The following code is a tool class copied from Baidu positioning SDK

public class LocationTool {
    private Context context;
    public LocationClient mLocationClient = null;
    private mLocationListener myListener = new mLocationListener();
    
    public LocationTool(Context context){
        this.context = context;
        Init();
    }

    public void Init(){
        mLocationClient = new LocationClient(context);
        //Declare LocationClient class
        mLocationClient.registerLocationListener(myListener);
        //Register listening function
        LocationClientOption option = new LocationClientOption();
        option.setIsNeedAddress(true);
        //Optional. It is not required by default. That is, the parameter is false
        //If the developer needs to get the address information of the current point, it must be true here
        //option.setCoorType("gcj02");
        mLocationClient.setLocOption(option);
		//mLocationClient is the LocationClient object initialized in step 2
		//You need to pass the configured LocationClientOption object to the LocationClient object through the setLocOption method
		//For more configuration of LocationClientOption, please refer to the detailed description of LocationClientOption class in the class reference
    }

    public class mLocationListener extends BDAbstractLocationListener {
        @Override
        public void onReceiveLocation(BDLocation address) {
            	//todo
        }
    }
}

Design interface

You need to get data in mLocationListener, but such a structure can't get data
You can add a callback interface in mLocationListener for operation

public class LocationTool {
	......
	......
	......
	/**
     * Add a new custom interface and get it out
     */
    public AddressListener addressListener;
    public interface AddressListener{
        void address(BDLocation position);  //Determine the value of the process
    }
    
    //Set getter and setter
     public AddressListener getAddressListener(){
        return addressNoListener;
    }

    public void setAddressListener(AddressListener addressListener) {
        mLocationClient.start();
        this.addressListener = addressListener;
    }
    
    //Call this interface in mLocationListener
    public class mLocationListener extends BDAbstractLocationListener {
        @Override
        public void onReceiveLocation(BDLocation address) {
            	//todo
            	addressListener.address(address);
        }
    }
    
}

Interface callback

Call this tool class

public class HomePageActivity extends AppCompatActivity {

     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_page);

        LocationTool tool = new LocationTool(getApplicationContext());
        tool.setAddressListener(new LocationTool.AddressListener() {
            @Override
            public void address(BDLocation position) {
            	//Get data here
                lat = position.getLatitude();
                lng = position.getLongitude();
            }
        });
    }
}

The interface callback with data return is like this
It's a similar principle if you want to do something else

Posted by roxiroxi on Wed, 11 Dec 2019 08:54:49 -0800