[英]How to call a method of a 3rd class by calling the method of the 2nd class from the 1st class
[英]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.