शुरू करना

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

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

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

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

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

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

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

    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 पासकोड डालें और उसे कोटेशन मार्क में रखें.

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

कॉल का उदाहरण

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

    // 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 में मौजूद अलग-अलग एपीआई के बारे में ज़्यादा जानें: