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

লোকেশন সার্ভিস কলব্যাক নির্ধারণ

আপনার লোকেশন ক্লায়েন্ট তৈরী করার পূর্বে, ইন্টারফেস বাস্তবায়ন করুন যা লোকেশন সার্ভিস ব্যবহার করে আপনার অ্যাপের সাথে যোগাযোগ করতে :

ConnectionCallbacks

পদ্ধতি নির্দিষ্ট করে যাকে লোকেশন সার্ভিস কল করে যখন একটি লোকেশন ক্লায়েন্ট সংযুক্ত (কানেক্ট) হয় বা বিযুক্ত (ডিসকানেক্টেড) হয়।

OnConnectionFailedListener

পদ্ধতি নির্দিষ্ট করে যাকে লোকেশন সার্ভিস কল করে যখন লোকেশন ক্লায়েন্ট সংযুক্ত করার সময় একটি এরর সংঘটিত হয়। এই পদ্ধতি একটি এরর ডায়লগ যা গুগল প্লে সার্ভিস ব্যবহার করে সমস্যাটি সমাধানের চেষ্টা করে তা প্রদর্শন করতে পূর্বে নির্ধারিত showErrorDialog পদ্ধতি ব্যবহার করে।

নিচের কোড চিত্রটি দেখায় কীভাবে ইন্টারফেস নির্দিষ্ট করতে হয় এবং পদ্ধতি নির্ধারিত করতে হয়:

public class MainActivity extends FragmentActivity implements
        GooglePlayServicesClient.ConnectionCallbacks,
        GooglePlayServicesClient.OnConnectionFailedListener {
    ...
    /*
     * Called by Location Services when the request to connect the
     * client finishes successfully. At this point, you can
     * request the current location or start periodic updates
     */
    @Override
    public void onConnected(Bundle dataBundle) {
        // Display the connection status
        Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show();
    }
    ...
    /*
     * Called by Location Services if the connection to the
     * location client drops because of an error.
     */
    @Override
    public void onDisconnected() {
        // Display the connection status
        Toast.makeText(this, "Disconnected. Please re-connect.",
                Toast.LENGTH_SHORT).show();
    }
    ...
    /*
     * Called by Location Services if the attempt to
     * Location Services fails.
     */
    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        /*
         * Google Play services can resolve some errors it detects.
         * If the error has a resolution, try sending an Intent to
         * start a Google Play services activity that can resolve
         * error.
         */
        if (connectionResult.hasResolution()) {
            try {
                // Start an Activity that tries to resolve the error
                connectionResult.startResolutionForResult(
                        this,
                        CONNECTION_FAILURE_RESOLUTION_REQUEST);
                /*
                * Thrown if Google Play services canceled the original
                * PendingIntent
                */
            } catch (IntentSender.SendIntentException e) {
                // Log the error
                e.printStackTrace();
            }
        } else {
            /*
             * If no resolution is available, display a dialog to the
             * user with the error.
             */
            showErrorDialog(connectionResult.getErrorCode());
        }
    }
    ...
}