বাংলায় অ্যান্ড্রয়েড সহায়িকা - Bangla Android Guide

সংযোগ (connection) এরর পরিচালনা করা

লোকেশন সার্ভিস থেকে স্বাভাবিক কলব্যাক পরিচালনা করার পাশাপাশি, আপনাকে একটি কলব্যাক পদ্ধতি প্রদান করতে হবে যা লোকেশন সার্ভিস কল করে যদি একটি এরর সংঘটিত হয়। এই কলব্যাক পদ্ধতি DialogFragment ক্লাস পূণরায় ব্যবহার করতে পারেন যা আপনি গুগল প্লে সার্ভিসের জন্য চেক করাকে পরিচালনা করার কাজে নির্ধারিত করেছেন। এটা ওভাররাইডও পূণরায় ব্যবহার করতে পারে যা আপনি onActivityResult() এর জন্য নির্ধারিত করেছেন যা যে কোন গুগল প্লে সার্ভিস রেজাল্ট গ্রহণ করে যা ঘটে যখন ইউজার এরর ডায়লগের সাথে পারস্পরিকভাবে যোগযোগ করে। নিচের চিত্রটি একটি কলব্যাক পদ্ধতির নমুনা বাস্তবায়ন দেখায়:

public class MainActivity extends FragmentActivity implements
        ConnectionCallbacks,
        OnConnectionFailedListener,
        OnAddGeofencesResultListener {
    ...
    // Implementation of OnConnectionFailedListener.onConnectionFailed
    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        // Turn off the request flag
        mInProgress = false;
        /*
         * If the error has a resolution, start a Google Play services
         * activity to resolve it.
         */
        if (connectionResult.hasResolution()) {
            try {
                connectionResult.startResolutionForResult(
                        this,
                        CONNECTION_FAILURE_RESOLUTION_REQUEST);
            } catch (SendIntentException e) {
                // Log the error
                e.printStackTrace();
            }
        // If no resolution is available, display an error dialog
        } else {
            // Get the error code
            int errorCode = connectionResult.getErrorCode();
            // Get the error dialog from Google Play services
            Dialog errorDialog = GooglePlayServicesUtil.getErrorDialog(
                    errorCode,
                    this,
                    CONNECTION_FAILURE_RESOLUTION_REQUEST);
            // If Google Play services can provide an error dialog
            if (errorDialog != null) {
                // Create a new DialogFragment for the error dialog
                ErrorDialogFragment errorFragment =
                        new ErrorDialogFragment();
                // Set the dialog in the DialogFragment
                errorFragment.setDialog(errorDialog);
                // Show the error dialog in the DialogFragment
                errorFragment.show(
                        getSupportFragmentManager(),
                        "Geofence Detection");
            }
        }
    }
    ...
}