如何将第二类代码添加到第一类

时间:2016-10-28 18:14:12

标签: java android google-maps

我的应用程序所有代码都在此类

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

我发现堆栈上的应用程序代码非常重要,但此代码在其他类中:

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 
        }
}

我不知道如何将类2中的代码添加到1.帮助。

1 个答案:

答案 0 :(得分:0)

您可以使用界面。

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();
    }
}

在您的主要活动(您的第一堂课!)上添加implements GpsLocationReciever

并覆盖onRecieveCallback(在第一堂课上!):

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