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

গুগল প্লে সার্ভিস চেক করা

লোকেশন সার্ভিস গুগল প্লে সার্ভিস APK’র একটি অংশ। যেহেতু ইউজারের ডিভাইসের অবস্থা প্রত্যাশা অনুযায়ী হওয়া কঠিন, আপনার সবসময় চেক করা উচিত যে লোকেশন সার্ভিসের সাথে সংযুক্ত হওয়ার চেষ্টা করার পূর্বে APK ইনস্টল হয়েছে। APK ইনস্টল হয়েছে কিনা তা পরীক্ষা করতে GooglePlayServicesUtil.isGooglePlayServicesAvailable() কল করুন যা রেফারেন্স ডকুমেন্টেশনের মধ্যে লিস্ট করা ইন্টিজার রেজাল্ট কোডের একটি ফেরত দেয়। আপনি যদি একটি এররের সম্মুখিন হোন, স্থানীয় ডায়লগ ফিরে পেতে GooglePlayServicesUtil.getErrorDialog() কল করুন, যে ডায়লগ ইউজারকে সঠিক কার্যক্রম গ্রহণ করতে অনুপ্রাণিত করে, তারপর একটি DialogFragment এর মধ্যে ডায়লগটি প্রদর্র্শণ করে। ডায়লগটি ইউজারকে সমস্যাটি সঠিক করতে দিতে পারে, যেই ক্ষেত্রে গুগল প্লে সার্ভিস আপনার একটিভিটিতে একটি রেজাল্ট ব্যাক পাঠাতে পারে। এই রেজাল্ট পরিচালনা করতে পদ্ধতি onActivityResult() ওভাররাইড করুন।

নোট: আপনার অ্যাপকে প্লাটফর্ম সংস্করণ ১.৬ এবং তৎপরবর্তী সংস্করনের সাথে সামঞ্জস্যপূর্ন করতে, একটিভিটি যা DialogFragment প্রদর্শন করে তাকে Activity এর পরিবর্তে অবশ্যই FragmentActivity কে সাবক্লাস করতে হবে। FragmentActivity ব্যবহার করা আপনাকে DialogFragment প্রদর্শন করতে getSupportFragmentManager()কল করতে দেয়।

যেহেতু আপনার কোডে একাধিক স্থানে সাধাারণত গুগল প্লে সার্ভিসের জন্য আপনাকে চেক করতে হয়, একটি পদ্ধতি নির্ধারণ করুন যা চেক কে আবদ্ধ করে, তারপর প্রতিটা কানেকশন প্রচেষ্টার পুর্বে পদ্ধতিটি কল করুন। নিচের কোড চিত্রটি গুগল প্লে সার্ভিস চেক করার জন্য প্রয়োজনীয় সকল কোড ধারণ:

public class MainActivity extends FragmentActivity {
    ...
    // Global constants
    /*
     * Define a request code to send to Google Play services
     * This code is returned in Activity.onActivityResult
     */
    private final static int
            CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;
    ...
    // Define a DialogFragment that displays the error dialog
    public static class ErrorDialogFragment extends DialogFragment {
        // Global field to contain the error dialog
        private Dialog mDialog;
        // Default constructor. Sets the dialog field to null
        public ErrorDialogFragment() {
            super();
            mDialog = null;
        }
        // Set the dialog to display
        public void setDialog(Dialog dialog) {
            mDialog = dialog;
        }
        // Return a Dialog to the DialogFragment.
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            return mDialog;
        }
    }
    ...
    /*
     * Handle results returned to the FragmentActivity
     * by Google Play services
     */
    @Override
    protected void onActivityResult(
            int requestCode, int resultCode, Intent data) {
        // Decide what to do based on the original request code
        switch (requestCode) {
            ...
            case CONNECTION_FAILURE_RESOLUTION_REQUEST :
            /*
             * If the result code is Activity.RESULT_OK, try
             * to connect again
             */
                switch (resultCode) {
                    case Activity.RESULT_OK :
                    /*
                     * Try the request again
                     */
                    ...
                    break;
                }
            ...
        }
        ...
    }
    ...
    private boolean servicesConnected() {
        // Check that Google Play services is available
        int resultCode =
                GooglePlayServicesUtil.
                        isGooglePlayServicesAvailable(this);
        // If Google Play services is available
        if (ConnectionResult.SUCCESS == resultCode) {
            // In debug mode, log the status
            Log.d("Location Updates",
                    "Google Play services is available.");
            // Continue
            return true;
        // Google Play services was not available for some reason
        } 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(),
                        "Location Updates");
            }
        }
    }
    ...
}

নিচের অধ্যায়ের কোড চিত্রটি এই পদ্ধতিকে যাচাই করতে কল করে যে গুগল প্লে সার্ভিস রয়েছে।