gpt4 book ai didi

android - 来自应用程序的调用 Activity

转载 作者:太空狗 更新时间:2023-10-29 15:51:16 27 4
gpt4 key购买 nike

我正在尝试使用 Twitter 中的 DigitsAuthCallBack 在 Activity 和最近的 document 中使用时不会触发说要使用 Application 类中的 AuthCallBack。

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

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

最佳答案

您可以使用 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);
}

Activity 类:

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

关于android - 来自应用程序的调用 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37519668/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com