使用界面,您可以实现这一点
写一个类似这样的接口
public interface onSuccessListner {
void onSuccess(DigitsSession session,String phoneNumber);
}
在您的 Main Activity 中实现此接口
public class MainActivity extends AppCompatActivity implements onSuccessListner{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onSuccess(DigitsSession session,String phoneNumber) {
//write your method calling or operations here
}
}
在您的应用程序类中实现 Application.ActivityLifecycleCallbacks 以检查是否创建了主活动,如果创建了主活动,则将主活动的上下文应用到监听器
在 on create 里面你的回调方法调用 MainActivity 的成功方法
public class MyApplctn extends Application implements Application.ActivityLifecycleCallbacks {
onSuccessListner onSuccessListner;
@Override
public void onCreate() {
super.onCreate();
registerActivityLifecycleCallbacks(this);
authCallback = new AuthCallback() {
@Override
public void success(DigitsSession session, String phoneNumber) {
//call myFunction() from MainActivity here
if(onSuccessListner!=null){
onSuccessListner.onSuccess(session,phoneNumber);
}
}
@Override
public void failure(DigitsException exception) {
}
};
}
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
if (activity instanceof MainActivity) {
onSuccessListner= (com.mmadapps.myapplication.onSuccessListner) activity;
}
}
@Override
public void onActivityStarted(Activity activity) {
}
@Override
public void onActivityResumed(Activity activity) {
}
@Override
public void onActivityPaused(Activity activity) {
}
@Override
public void onActivityStopped(Activity activity) {
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
}
@Override
public void onActivityDestroyed(Activity activity) {
}
}
希望对你有帮助