शुरू करना

इस दस्तावेज़ में, Android पर Awareness API का इस्तेमाल करके डेवलपमेंट शुरू करने का तरीका बताया गया है. Awareness API, Google Play services का हिस्सा है.

Awareness API का इस्तेमाल करने के लिए, आपके पास Google खाता होना चाहिए. अगर आपके पास पहले से कोई खाता है, तो आप इसे इस्तेमाल करने के लिए तैयार हैं. आपको जांच के लिए, अलग Google खाते की ज़रूरत भी पड़ सकती है.

शुरू करने से पहले

एपीआई पासकोड पाना

अगर आपने अब तक Awareness API चालू नहीं किया है और Google API कुंजी नहीं पाई है, तो ऐसा करने के लिए साइन अप करना और एपीआई कुंजियां में दिया गया तरीका अपनाएं.

अपने ऐप्लिकेशन को कॉन्फ़िगर करना

  1. अपने प्रोजेक्ट-लेवल की build.gradle फ़ाइल में, Google की Maven रिपॉज़िटरी को अपने buildscript और allprojects, दोनों सेक्शन में शामिल करें:

    buildscript {
        repositories {
            google()
        }
    }
    
    allprojects {
        repositories {
            google()
        }
    }
    
  2. अपने मॉड्यूल की ऐप्लिकेशन-लेवल की Gradle फ़ाइल में, Awareness API के लिए डिपेंडेंसी जोड़ें. यह फ़ाइल आम तौर पर app/build.gradle होती है:

    dependencies {
      implementation 'com.google.android.gms:play-services-awareness:19.1.0'
    }
    
  3. अपने ऐप्लिकेशन की AndroidManifest.xml फ़ाइल में, Awareness API पासकोड जोड़ें. इसके लिए, android:name="com.google.android.awareness.API_KEY" के साथ <meta-data> टैग जोड़ें. android:value के लिए, अपनी Awareness API Key डालें. इसे कोटेशन मार्क में रखें.

    <manifest>
        <application>
            <meta-data
                android:name="com.google.android.awareness.API_KEY"
                android:value="API_KEY"/>
        </application>
    </manifest>
  4. अपने ऐप्लिकेशन की AndroidManifest.xml फ़ाइल में ज़रूरी अनुमतियां जोड़ें. आपके ऐप्लिकेशन के इस्तेमाल किए जाने वाले एपीआई के तरीकों और फ़ेंस के टाइप के आधार पर, ज़रूरी अनुमतियां अलग-अलग होती हैं.

कॉल का उदाहरण

getDetectedActivity() को कॉल करने वाले इस उदाहरण में, Google Play services के कनेक्शनलेस मॉडल को Awareness API के साथ इस्तेमाल करने का तरीका बताया गया है:

    // Each type of contextual information in the snapshot API has a corresponding "get" method.
    // For instance, this is how to get the user's current Activity.
    Awareness.getSnapshotClient(this).getDetectedActivity()
        .addOnSuccessListener(new OnSuccessListener<DetectedActivityResponse>() {
            @Override
            public void onSuccess(DetectedActivityResponse dar) {
                ActivityRecognitionResult arr = dar.getActivityRecognitionResult();
                // getMostProbableActivity() is good enough for basic Activity detection.
                // To work within a threshold of confidence,
                // use ActivityRecognitionResult.getProbableActivities() to get a list of
                // potential current activities, and check the confidence of each one.
                DetectedActivity probableActivity = arr.getMostProbableActivity();

                int confidence = probableActivity.getConfidence();
                String activityStr = probableActivity.toString();
                mLogFragment.getLogView().println("Activity: " + activityStr
                    + ", Confidence: " + confidence + "/100");
            }
        })

अगले चरण

Awareness API में मौजूद अलग-अलग एपीआई के बारे में ज़्यादा जानें: