从应用程序调用 activity

Call activity from Application

我正在尝试使用来自 TwitterDigitsAuthCallBack 在从 activity 和最近的 document 中使用时不会触发 Application class.

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

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 Class :

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

使用接口你可以实现这个

像这样写一个界面

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

在您的应用程序中 class 执行 Application.ActivityLifecycleCallbacks 以检查是否创建了 main activity,如果创建了 main activity,则将 main activity 的上下文应用到听众

在on create里面你的回调方法调用Main的success方法Activity

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

    }
}

希望对您有所帮助