简体   繁体   中英

How to add code from 2nd class to 1st class

All code my app is in this class

    public class MapsActivity extends FragmentActivity implements
            OnMapReadyCallback,
            GoogleApiClient.ConnectionCallbacks,
            GoogleApiClient.OnConnectionFailedListener
{
my_app
}

I found very important for my app code on stack, but this code is in other class:

public class GpsLocationReceiver extends BroadcastReceiver implements LocationListener 
...

@Override
public void onReceive(Context context, Intent intent)
{
        if (intent.getAction().matches("android.location.PROVIDERS_CHANGED"))
        { 
            // react on GPS provider change action 
        }
}

I do not know how to add the code from Class 2 to 1. Help.

You can use an interface.

public class GpsLocationReceiver extends BroadcastReceiver implements LocationListener 
...

private ReceiverListener mListener;

public GpsLocationReciever(Activity activity){(RecieverListener) mListener = activity;}

interface RecieverListener {
void onRecieveCallback();
}

@Override
public void onReceive(Context context, Intent intent)
{
    if (intent.getAction().matches("android.location.PROVIDERS_CHANGED"))
    { 
        // react on GPS provider change action
        mListener.onRecieveCallback();
    }
}

And on your main activity (your 1st class!) add implements GpsLocationReciever

and override onRecieveCallback (on your 1st class!):

@override
public void onRecieveCallback(){
   //Here you code whatever you would be coding on onRecieve from your second class
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM