2

我正在尝试使用来自Twitter的Digits。从活动中使用AuthCallBack时不会触发,最近的文档说要使用Application类中的 AuthCallBack。

现在我的AuthCallBack工作正常,并且 onSuccess 我需要从我的 MainActivity 调用一个方法。我如何从 Application 类中实现它。请帮忙。我已经给出了下面的代码。

public class MyApplication extends Application {

    private AuthCallback authCallback;

    @Override
    public void onCreate() {
        super.onCreate();

        authCallback = new AuthCallback() {
            @Override
            public void success(DigitsSession session, String phoneNumber) {

                //call myFunction() from MainActivity here

            }

            @Override
            public void failure(DigitsException exception) {

            }
        };
    }

    public AuthCallback getAuthCallback(){
        return authCallback;
        }
}
4

2 回答 2

8

您可以使用 BroadcastManager 来存档。以下是您可以使用的示例代码

从应用程序:

    @Override
    public void success(DigitsSession session, String phoneNumber) {
    Intent intent = new Intent(Constants.FILTER_LOGIN_SUCCESS);
    intent.putExtra(Constants.EXTRA_PHONE_NUMBER, phoneNumber);
    LocalBroadcastManager.getInstance(mInstance).sendBroadcast(intent);
}

活动课:

@Override
    protected void onResume() {
        super.onResume();
        LocalBroadcastManager.getInstance(SignUpActivity.this).registerReceiver(broadcastReceiver,
                new IntentFilter(Constants.FILTER_LOGIN_SUCCESS));
    }

@Override
protected void onDestroy() {
    super.onDestroy();
    LocalBroadcastManager.getInstance(SignUpActivity.this).unregisterReceiver(broadcastReceiver);
}

private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String phoneNumber = intent.getStringExtra(Constants.EXTRA_PHONE_NUMBER);
            navigateToAnotherActivty();
        }
    };
于 2016-05-30T07:17:45.887 回答
4

使用界面,您可以实现这一点

写一个类似这样的接口

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) {

    }
}

希望对你有帮助

于 2016-05-30T07:57:10.717 回答