初始化贝宝库只有一次的多项活动 [英] Initialize PayPal Library only once for multiple activities

查看:104
本文介绍了初始化贝宝库只有一次的多项活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用PayPal的MECL库:


  • 显示在活动A支付贝宝 - 按钮

  • 启动支付过程中的活动B

由于显而易见的原因,我不想(为每个活动一次)两次初始化贝宝库,因为这需要时间和不必要地使用户等待。

我怎么能分享我的两个活动之间的库的引用?

这是我的code的时刻(来自贝宝的例子所)($ P $在这两个活动A和B psent):

  @覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);    // ...    //显示贝宝按钮
    initializePayPal();}公共无效initializePayPal(){
    //时间启动库,但首先我们需要初始化
    setSupportProgressBarIndeterminateVisibility(真);    //创建一个单独的线程来执行初始化
    螺纹libraryInitializationThread =新主题(){
        公共无效的run(){
            //初始化库
            initLibrary();            //库是这样初始化让我们通过通知我们的处理程序启动它
            如果(PayPal.getInstance()。isLibraryInitialized()){
                hRefresh.sendEmptyMessage(INITIALIZE_SUCCESS);
            }
            其他{
                hRefresh.sendEmptyMessage(INITIALIZE_FAILURE);
            }
        }
    };
    libraryInitializationThread.start();
}私人无效initLibrary(){
    //这是主要的初始化调用发生在你的背景下,应用程序ID,服务器要连接到,你的PayPalListener
    PayPal.fetchDeviceReferenceTokenWithAppID(此,CompletePaymentActivity.appID,CompletePaymentActivity.server,新ResultDelegate());    // - 这些都是必要的设置。
    PayPal.getInstance()setLanguage(SharedFunctions.getCurrentLocaleString())。 //设置库中的语言。
    // -
}//这个处理程序将使我们能够正确地更新UI。你不能从一个非UI线程触摸意见。
处理器hRefresh =新的处理程序(){
    @覆盖
    公共无效的handleMessage(消息MSG){
        开关(msg.what){
            案例INITIALIZE_SUCCESS:
                //我们已经初始化的应用,关闭该对话框并显示PayPal按钮
                setSupportProgressBarIndeterminateVisibility(假);
                showPayPalButton();
                打破;
            案例INITIALIZE_FAILURE:
                //初始化失败,关闭对话框,更新页面,并显示举杯
                setSupportProgressBarIndeterminateVisibility(假);
                Toast.makeText(mContext,mContext.getString(R.string.paypal_initialization_failed),Toast.LENGTH_LONG).show();
                完();
                打破;
        }
    }
};公共无效showPayPalButton(){
    // ...    贝页= PayPal.getInstance();
    //获取checkoutbutton
    launchPayPalButton = pp.getCheckoutButton(这一点,PayPal.BUTTON_194x37,
            CheckoutButton.TEXT_PAY);
    // ...
}


解决方案

我最终宣布延长应用,在这里我初始化库一次,并从其中一类是可以由所有的活动中使用。

I'm using the PayPal MECL library to:

  • Display the "Pay with PayPal"-button in Activity A
  • Initiate the payment process in Activity B

For obvious reasons, I don't want to initialize the PayPal library twice (once for every activity), as this takes time and unnecessarily makes the user wait.

How can I share a reference to the library between my two Activities?

This is my code (present in both Activity A and B) at the moment (taken from the PayPal example):

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

    //...

    //Show PayPal-Button
    initializePayPal();

}

public void initializePayPal() {
    //Time to launch the library but first we need to initialize
    setSupportProgressBarIndeterminateVisibility(true);

    //Create a separate thread to do the initialization
    Thread libraryInitializationThread = new Thread() {
        public void run() {
            //Initialize the library
            initLibrary();

            // The library is initialized so let's launch it by notifying our handler
            if (PayPal.getInstance().isLibraryInitialized()) {
                hRefresh.sendEmptyMessage(INITIALIZE_SUCCESS);
            }
            else {
                hRefresh.sendEmptyMessage(INITIALIZE_FAILURE);
            }
        }
    };
    libraryInitializationThread.start();
}

private void initLibrary() {
    // This is the main initialization call that takes in your Context, the Application ID, the server you would like to connect to, and your PayPalListener
    PayPal.fetchDeviceReferenceTokenWithAppID(this, CompletePaymentActivity.appID, CompletePaymentActivity.server, new ResultDelegate());

    // -- These are required settings.
    PayPal.getInstance().setLanguage(SharedFunctions.getCurrentLocaleString()); // Sets the language for the library.
    // --
}

// This handler will allow us to properly update the UI. You cannot touch Views from a non-UI thread.
Handler hRefresh = new Handler(){
    @Override
    public void handleMessage(Message msg) {
        switch(msg.what){
            case INITIALIZE_SUCCESS:
                //We have initialized the application, close the dialog and show the PayPal Button
                setSupportProgressBarIndeterminateVisibility(false);
                showPayPalButton();
                break;
            case INITIALIZE_FAILURE:
                //Initialization failure, close the dialog, update the page and show a toast
                setSupportProgressBarIndeterminateVisibility(false);
                Toast.makeText(mContext, mContext.getString(R.string.paypal_initialization_failed), Toast.LENGTH_LONG).show();
                finish();
                break;
        }
    }
};

public void showPayPalButton() {
    //...

    PayPal pp = PayPal.getInstance();
    // get the checkoutbutton
    launchPayPalButton = pp.getCheckoutButton(this, PayPal.BUTTON_194x37,
            CheckoutButton.TEXT_PAY);
    //...
}

解决方案

I ended up declaring a class that extends Application, where I initialize the library once and from which it can be used by all activities.

这篇关于初始化贝宝库只有一次的多项活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆