簡體   English   中英

如何將代碼從第二類添加到第一類

[英]How to add code from 2nd class to 1st class

我的應用程序的所有代碼均在此類中

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

我不知道如何將代碼從Class 2添加到1。幫助。

您可以使用界面。

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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM