是否可以将googleAPIClient连接回调发送到另一个类?

时间:2017-11-27 12:40:13

标签: android callback

是否可以将回调发送到另一个类?

我有app类如下:

public class MyApp extends Application {
    private GoogleApiClient mGoogleApiClient;

    public MyApp(){
        // bla... and initialize google client here...
        this.mGoogleApiClient = new GoogleApiClient.Builder(this.getApplicationContext())
                .addApi(Auth.GOOGLE_SIGN_IN_API, this.GSO()).build();
    }
}

我的主要课程如下:

public class MyActivity extends AppCompatActivity {
    private MyApp app;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        app = (MyApp) this.getApplication();
    }

    protected void onStart(){
        super.onStart();

        GoogleApiClientUtility googleApiClientUtility = new GoogleApiClientUtility(app, new GoogleApiClient.ConnectionCallbacks(){
            @Override
            public void onConnected(@Nullable Bundle bundle) {
                // i want app.mGoogleApiClient ConnectionCallbacks listen to this code
            }

            @Override
            public void onConnectionSuspended(int i) {
                // i want app.mGoogleApiClient ConnectionCallbacks listen to this code
            }
        });
        googleApiClientUtility.connect();
    }
}

并使用GoogleApiClientUtility类,如下所示:

public class GoogleApiClientUtility {

    private MyApp mApp;

    public GoogleApiClientUtility(MyApp app, GoogleApiClient.ConnectionCallbacks callbacks){
        mApp = app;
    }

    public final void connect() {
        /* how to do this below???
         * mApp.getGoogleApiClient().registerConnectionCallbacks(????) 
         * so it can send to onConnect listener in MyActivity Class above
         */
        mApp.getGoogleApiClient().connect();
    }
}

我有办法将回拨功能设置为'来电者类吗?

谢谢,

2 个答案:

答案 0 :(得分:1)

是的,你可以这样做,

  1. 编写接口类。
  2. 实施您想要回电的界面
  3. 在构造函数中传递该接口引用
  4. 通过您传递的参考调用接口方法(成功或相应的错误情况)
  5. 希望你能得到这个想法。此外,我正在为我的一个项目中实现的参考添加代码。

       private void startScan() {
        BarcodeReaderUtility barcodeReaderUtility = new BarcodeReaderUtility(mContext, new BarcodeReaderUtility.BarcodeTextProvider() {
            @Override
            public void onReadingSuccessfully(String barcode) {
                etBarcode.setText(barcode);
                etBarcode.setSelection(barcode.length());
            }
        });
        barcodeReaderUtility.getBarCode();
    }
    

    以下是具有接口的类和通过构造函数传递的引用。如果您发现任何问题,请查看一下。请问 公共类BarcodeReaderUtility {

    private BarcodeTextProvider mBarcodeProvider;
    private Context mContext;
    
    public BarcodeReaderUtility(Context context, BarcodeTextProvider barcodeTextProvider) {
        mContext = context;
        mBarcodeProvider = barcodeTextProvider;
    }
    
    public final void getBarCode() {
        final MaterialBarcodeScanner materialBarcodeScanner = new MaterialBarcodeScannerBuilder()
                .withActivity((Activity) mContext)
                .withEnableAutoFocus(true)
                .withBleepEnabled(true)
                .withBackfacingCamera()
                .withCenterTracker()
                .withText(mContext.getResources().getString(R.string.scanning))
                .withResultListener(new MaterialBarcodeScanner.OnResultListener() {
                    @Override
                    public void onResult(Barcode barcode) {
                        String barCodeResult  = barcode.rawValue;
                        mBarcodeProvider.onReadingSuccessfully(barCodeResult);
                    }
                })
                .build();
        materialBarcodeScanner.startScan();
    }
    
    public interface BarcodeTextProvider {
        void onReadingSuccessfully(String barcode);
    }
    

    }

答案 1 :(得分:0)

来自@Abdul Waheed(+1)的线索,

我修复如下: 创建实用程序类:

class GoogleApiClientUtility {

    private GoogleApiClient mClient;
    private GoogleApiClient.ConnectionCallbacks mCallbacks;

    GoogleApiClientUtility(GoogleApiClient app, GoogleApiClient.ConnectionCallbacks callbacks) {
        mClient = app;
        mCallbacks = callbacks;
    }

    final void connect() {
        mClient.registerConnectionCallbacks(mCallbacks);
        mClient.connect();
    }

}

在我的主要活动课程中:

// private MyApp app; <-- declared within main activity class
GoogleApiClientUtility googleApiClientUtility = new GoogleApiClientUtility(
                    app.getGoogleApiClient(),
                    new GoogleApiClient.ConnectionCallbacks() {
                        @Override
                        public void onConnected(@Nullable Bundle bundle) {
                            Log.e("GoogleApiClient: %s", "onConnected");
                        }

                        @Override
                        public void onConnectionSuspended(int i) {
                            Log.e("GoogleApiClient: %s", "onConnectionSuspended");
                        }
                    }
            );
            googleApiClientUtility.connect();