Google API অ্যাক্সেস করুন

আপনি যখন Google Play পরিষেবা দ্বারা চালিত, Google সাইন-ইন বা ML কিট দ্বারা চালিত একটি SDK-এর APIগুলির একটিতে কল করতে চান, তখন আপনাকে প্রথমে একটি API ক্লায়েন্ট অবজেক্টের একটি উদাহরণ তৈরি করতে হবে৷ এই বস্তুগুলি স্বয়ংক্রিয়ভাবে Google Play পরিষেবাগুলির সংযোগ পরিচালনা করে৷ যখন একটি সংযোগ উপলব্ধ থাকে, প্রতিটি API ক্লায়েন্ট অবজেক্ট ক্রমানুসারে অনুরোধগুলি চালায়। অন্যথায়, ক্লায়েন্ট অবজেক্ট অনুরোধগুলি সারিবদ্ধ করে। ডকুমেন্টেশন অন্যথায় ইঙ্গিত না করলে, ক্লায়েন্ট অবজেক্টগুলি নির্মাণ করা সস্তা; আপনি যখনই API পদ্ধতিগুলি ব্যবহার করতে চান তখনই নতুন API ক্লায়েন্ট তৈরি করা ভাল।

এই নির্দেশিকাটি দেখায় যে আপনি কীভাবে Google Play পরিষেবাগুলি দ্বারা চালিত যে কোনও SDK-তে API কল করতে পারেন, যেগুলির অনুমোদনের প্রয়োজন নেই এবং যেগুলির অনুমোদনের প্রয়োজন হয় সেগুলিকে কীভাবে অ্যাক্সেস করতে হয়৷

এবার শুরু করা যাক

শুরু করতে, Google Play পরিষেবাগুলি কীভাবে সেট আপ করতে হয় তার নির্দেশিকায় বর্ণিত আপনার অ্যাপ প্রকল্পে প্রয়োজনীয় সরঞ্জাম এবং নির্ভরতা যোগ করুন।

অনুমোদনের প্রয়োজন না হলে অ্যাক্সেস করুন

API অনুমোদনের প্রয়োজন নেই এমন একটি পরিষেবা অ্যাক্সেস করতে, পরিষেবাটির ক্লায়েন্ট অবজেক্টের একটি উদাহরণ পান, এটিকে বর্তমান Context বা বর্তমান Activity পাস করে। যেকোনো API কল চালানোর আগে, ব্যবহারকারীদের প্রয়োজনে Google Play পরিষেবাগুলি আপগ্রেড করার জন্য অনুরোধ করা হয়।

উদাহরণস্বরূপ, অ্যান্ড্রয়েডের জন্য ফিউজড লোকেশন প্রোভাইডার ব্যবহার করে ডিভাইসের সর্বশেষ পরিচিত অবস্থান পেতে, নিম্নলিখিত কোড স্নিপেটে দেখানো যুক্তি যোগ করুন:

কোটলিন

// Code required for requesting location permissions omitted for brevity.
val client = LocationServices.getFusedLocationProviderClient(this)

// Get the last known location. In some rare situations, this can be null.
client.lastLocation.addOnSuccessListener { location : Location? ->
    location?.let {
        // Logic to handle location object.
    }
}

জাভা

// Code required for requesting location permissions omitted for brevity.
FusedLocationProviderClient client =
        LocationServices.getFusedLocationProviderClient(this);

// Get the last known location. In some rare situations, this can be null.
client.getLastLocation()
        .addOnSuccessListener(this, location -> {
            if (location != null) {
                // Logic to handle location object.
            }
        });

অনুমোদনের প্রয়োজন হলে অ্যাক্সেস করুন

ব্যবহারকারীর অনুমোদনের প্রয়োজন এমন একটি পরিষেবা অ্যাক্সেস করতে, নিম্নলিখিত পদক্ষেপগুলি সম্পূর্ণ করুন:

  1. ব্যবহারকারী সাইন ইন করুন .
  2. পরিষেবার জন্য প্রয়োজনীয় স্কোপগুলি অ্যাক্সেস করার অনুমতির অনুরোধ করুন
  3. পরিষেবার ক্লায়েন্ট অবজেক্টের একটি উদাহরণ পান, এটি একটি Context বা Activity অবজেক্ট ছাড়াও ব্যবহারকারীর GoogleSignInAccount অবজেক্টে পাস করে৷

নিম্নলিখিত উদাহরণটি Google Fit API ব্যবহার করে একজন ব্যবহারকারীর দৈনিক পদক্ষেপগুলি পড়ার প্রয়োগ করে৷ একটি সম্পূর্ণ প্রকল্পের প্রেক্ষাপটে অনুরূপ বাস্তবায়ন দেখতে, GitHub-এ BasicHistoryApiKotlin অ্যাপের প্রধান কার্যকলাপ দেখুন।

কোটলিন

class FitFragment : Fragment() {
    private val fitnessOptions: FitnessOptions by lazy {
        FitnessOptions.builder()
            .addDataType(DataType.TYPE_STEP_COUNT_CUMULATIVE)
            .addDataType(DataType.TYPE_STEP_COUNT_DELTA)
            .build()
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        fitSignIn()
    }

    /*
     * Checks whether the user is signed in. If so, executes the specified
     * function. If the user is not signed in, initiates the sign-in flow,
     * specifying the function to execute after the user signs in.
     */
    private fun fitSignIn() {
        if (oAuthPermissionsApproved()) {
            readDailySteps()
        } else {
            GoogleSignIn.requestPermissions(
                this,
                SIGN_IN_REQUEST_CODE,
                getGoogleAccount(),
                fitnessOptions
            )
        }
    }

    private fun oAuthPermissionsApproved() =
        GoogleSignIn.hasPermissions(getGoogleAccount(), fitnessOptions)

    /*
     * Gets a Google account for use in creating the fitness client. This is
     * achieved by either using the last signed-in account, or if necessary,
     * prompting the user to sign in. It's better to use the
     * getAccountForExtension() method instead of the getLastSignedInAccount()
     * method because the latter can return null if there has been no sign in
     * before.
     */
    private fun getGoogleAccount(): GoogleSignInAccount =
        GoogleSignIn.getAccountForExtension(requireContext(), fitnessOptions)

    /*
     * Handles the callback from the OAuth sign in flow, executing the function
     * after sign-in is complete.
     */
    override fun onActivityResult(
            requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (resultCode) {
            RESULT_OK -> {
                readDailySteps()
            }
            else -> {
                // Handle error.
            }
        }
    }

    /*
     * Reads the current daily step total.
     */
    private fun readDailySteps() {
        Fitness.getHistoryClient(requireContext(), getGoogleAccount())
            .readDailyTotal(DataType.TYPE_STEP_COUNT_DELTA)
            .addOnSuccessListener { dataSet ->
                val total = when {
                    dataSet.isEmpty -> 0
                    else -> dataSet.dataPoints.first()
                            .getValue(Field.FIELD_STEPS).asInt()
                }

                Log.i(TAG, "Total steps: $total")
            }
            .addOnFailureListener { e ->
                Log.w(TAG, "There was a problem getting the step count.", e)
            }
    }

    companion object {
        const val SIGN_IN_REQUEST_CODE = 1001
    }
}

জাভা

public class FitFragment extends Fragment {
    private final FitnessOptions fitnessOptions = FitnessOptions.builder()
            .addDataType(DataType.TYPE_STEP_COUNT_CUMULATIVE)
            .addDataType(DataType.TYPE_STEP_COUNT_DELTA)
            .build();

    @Override
    public void onViewCreated(
            @NotNull View view, @Nullable Bundle savedInstanceState) {
        fitSignIn();
    }

    /*
     * Checks whether the user is signed in. If so, executes the specified
     * function. If the user is not signed in, initiates the sign-in flow,
     * specifying the function to execute after the user signs in.
     */
    private void fitSignIn() {
        if (oAuthPermissionsApproved()) {
            readDailySteps();
        } else {
            GoogleSignIn.requestPermissions(this, SIGN_IN_REQUEST_CODE,
                    getGoogleAccount(), fitnessOptions);
        }
    }

    private boolean oAuthPermissionsApproved() {
        return GoogleSignIn.hasPermissions(getGoogleAccount(), fitnessOptions);
    }

    /*
     * Gets a Google account for use in creating the fitness client. This is
     * achieved by either using the last signed-in account, or if necessary,
     * prompting the user to sign in. It's better to use the
     * getAccountForExtension() method instead of the getLastSignedInAccount()
     * method because the latter can return null if there has been no sign in
     * before.
     */
    private GoogleSignInAccount getGoogleAccount() {
        return GoogleSignIn.getAccountForExtension(
                requireContext(), fitnessOptions);
    }

    /*
     * Handles the callback from the OAuth sign in flow, executing the function
     * after sign-in is complete.
     */
    @Override
    public void onActivityResult(
            int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            readDailySteps();
        } else {
            // Handle error.
        }
    }

    /*
     * Reads the current daily step total.
     */
    private void readDailySteps() {
        AtomicInteger total = new AtomicInteger();
        Fitness.getHistoryClient(requireContext(), getGoogleAccount())
                .readDailyTotal(DataType.TYPE_STEP_COUNT_DELTA)
                .addOnSuccessListener(dataSet -> {
                    if (!dataSet.isEmpty())
                        total.set(Integer.parseInt(dataSet.getDataPoints()
                                .get(0).getValue(FIELD_STEPS).toString()));
                        Log.i(TAG, "Total steps: $total");
                })
                .addOnFailureListener(e -> {
                    Log.w(TAG, "There was a problem getting the step count.", e);
                });
    }

    private static final int SIGN_IN_REQUEST_CODE = 1001;
}

API প্রাপ্যতা জন্য পরীক্ষা করুন

আপনি আপনার অ্যাপে এমন একটি বৈশিষ্ট্য সক্ষম করার আগে যা একটি Google Play পরিষেবা API-এর উপর নির্ভর করে, ডিভাইসে API-এর উপলব্ধতার জন্য একটি চেক অন্তর্ভুক্ত করুন। এটি করতে, checkApiAvailability() কল করুন।

নিম্নোক্ত কোড স্নিপেট দেখায় কিভাবে ফিউজড অবস্থান প্রদানকারীর উপলব্ধতা পরীক্ষা করতে হয়।

কোটলিন

fun getLastLocationIfApiAvailable(context: Context?): Task<Location>? {
    val client = getFusedLocationProviderClient(context)
    return GoogleApiAvailability.getInstance()
        .checkApiAvailability(client)
        .onSuccessTask { _ -> client.lastLocation }
        .addOnFailureListener { _ -> Log.d(TAG, "Location unavailable.")}
}

জাভা

public Task<Location> getLastLocationIfApiAvailable(Context context) {
    FusedLocationProviderClient client =
            getFusedLocationProviderClient(context);
    return GoogleApiAvailability.getInstance()
            .checkApiAvailability(client)
            .onSuccessTask(unused -> client.getLastLocation())
            .addOnFailureListener(e -> Log.d(TAG, "Location unavailable."));
}