Android N और उससे पहले के वर्शन में मिलने वाले सुझाव

आम तौर पर, लोग टीवी पर कॉन्टेंट देखने से पहले कम से कम इनपुट देना पसंद करते हैं कई टीवी उपयोगकर्ताओं के लिए, सबसे अच्छा तरीका यह है: बैठें, टीवी चालू करें, और कॉन्टेंट देखें. आम तौर पर, लोग कॉन्टेंट देखने के लिए कम से कम चरणों वाला तरीका पसंद करते हैं.

ध्यान दें: यहां बताए गए एपीआई का इस्तेमाल, सिर्फ़ Android के 7.1 (एपीआई लेवल 25) या इससे पहले के वर्शन पर चलने वाले ऐप्लिकेशन में सुझाव दिखाने के लिए करें. Android 8.0 (एपीआई लेवल 26) और इसके बाद के वर्शन पर चलने वाले ऐप्लिकेशन के लिए सुझाव देने के लिए, आपके ऐप्लिकेशन को सुझाव वाले चैनलों का इस्तेमाल करना होगा .

Android फ़्रेमवर्क, होम स्क्रीन पर सुझावों की पंक्ति दिखाकर, कम से कम इनपुट के साथ इंटरैक्ट करने में मदद करता है. डिवाइस का पहली बार इस्तेमाल करने के बाद, टीवी की होम स्क्रीन की पहली पंक्ति में कॉन्टेंट के सुझाव दिखते हैं. अपने ऐप्लिकेशन के कॉन्टेंट कैटलॉग से सुझाव देने पर, लोगों को आपके ऐप्लिकेशन पर वापस लाने में मदद मिल सकती है.

Android TV की होम स्क्रीन पर, सुझावों वाली लाइन का उदाहरण
पहली इमेज. सुझावों की पंक्ति का एक उदाहरण.

इस गाइड में, सुझाव बनाने और उन्हें Android फ़्रेमवर्क को उपलब्ध कराने का तरीका बताया गया है. इससे लोग आपके ऐप्लिकेशन का कॉन्टेंट ढूंढ सकते हैं और उसका आनंद ले सकते हैं. Leanback सैंपल के तौर पर मिला ऐप्लिकेशन में, लागू करने का नमूना भी देखें.

सुझाव देने के सबसे सही तरीके

सुझावों की मदद से, लोग अपनी पसंद का कॉन्टेंट और ऐप्लिकेशन आसानी से ढूंढ सकते हैं. अपने टीवी ऐप्लिकेशन के साथ लोगों को बेहतर अनुभव देने के लिए, यह ज़रूरी है कि आप अच्छी क्वालिटी के और लोगों के काम के सुझाव दें. इसलिए, आपको इस बात का ध्यान रखना चाहिए कि लोगों को कौनसे सुझाव दिखाए जाएं और उन्हें कैसे मैनेज किया जाए.

अलग-अलग तरह के सुझाव

सुझाव देते समय, आपको लोगों को अधूरी देखी गई गतिविधियों पर वापस ले जाना चाहिए या ऐसी गतिविधियां सुझानी चाहिए जो उससे जुड़े कॉन्टेंट तक ले जाती हों. यहां कुछ खास तरह के सुझाव दिए गए हैं, जिन पर आपको ध्यान देना चाहिए:

  • जारी रखने के लिए कॉन्टेंट के सुझाव. जैसे, किसी सीरीज़ को फिर से देखने के लिए, अगले एपिसोड का सुझाव. इसके अलावा, रोकी गई फ़िल्मों, टीवी शो या पॉडकास्ट के लिए, जारी रखने के सुझावों का इस्तेमाल करें, ताकि लोग कुछ ही क्लिक में रोके गए कॉन्टेंट को फिर से देख सकें.
  • नया कॉन्टेंट के सुझाव. जैसे, अगर किसी व्यक्ति ने कोई सीरीज़ पूरी देख ली है, तो उसे पहली बार रिलीज़ हुए नए एपिसोड का सुझाव दें. साथ ही, अगर आपका ऐप्लिकेशन लोगों को कॉन्टेंट की सदस्यता लेने, उसे फ़ॉलो करने या ट्रैक करने की सुविधा देता है, तो ट्रैक किए गए कॉन्टेंट की सूची में, न देखे गए आइटम के लिए, नए कॉन्टेंट के सुझावों का इस्तेमाल करें.
  • मिलते-जुलते कॉन्टेंट के सुझाव. ये सुझाव, लोगों के वीडियो देखने के इतिहास के आधार पर दिए जाते हैं.

लोगों को बेहतर अनुभव देने के लिए, सुझाव वाले कार्ड डिज़ाइन करने के बारे में ज़्यादा जानने के लिए, Android TV Design Spec में Recommendation Row देखें.

वीडियो के सुझाव रीफ़्रेश करें

सुझावों को रीफ़्रेश करते समय, उन्हें सिर्फ़ हटाएं और फिर से पोस्ट न करें. ऐसा करने से, सुझावों की पंक्ति के आखिर में सुझाव दिखते हैं. फ़िल्म जैसे किसी कॉन्टेंट आइटम को देखने के बाद, उसे सुझावों से हटा दें.

पसंद के मुताबिक सुझाव पाएं

ब्रैंडिंग की जानकारी देने के लिए, सुझाव वाले कार्ड को पसंद के मुताबिक बनाया जा सकता है. इसके लिए, कार्ड की फ़ोरग्राउंड और बैकग्राउंड इमेज, रंग, ऐप्लिकेशन का आइकॉन, टाइटल, और सबटाइटल जैसे यूज़र इंटरफ़ेस एलिमेंट सेट किए जा सकते हैं. ज़्यादा जानने के लिए, Android TV Design Spec में Recommendation Row देखें.

ग्रुप के सुझाव

आपके पास, सुझाव के सोर्स के आधार पर, सुझावों को ग्रुप में बांटने का विकल्प होता है. उदाहरण के लिए, आपका ऐप्लिकेशन सुझावों के दो ग्रुप दिखा सकता है: पहला, उस कॉन्टेंट के सुझाव जिसकी सदस्यता व्यक्ति ने ली है. दूसरा, ट्रेंड में चल रहे नए कॉन्टेंट के सुझाव, जिसके बारे में व्यक्ति को शायद पता न हो.

सुझावों की पंक्ति बनाते या अपडेट करते समय, सिस्टम हर ग्रुप के सुझावों को अलग-अलग रैंक करता है और क्रम में लगाता है. सुझावों के लिए ग्रुप की जानकारी देने से, यह पक्का किया जा सकता है कि आपके सुझाव, मिलते-जुलते सुझावों के तहत क्रम में न लगाए जाएं.

किसी सुझाव के ग्रुप की कुंजी वाली स्ट्रिंग सेट करने के लिए, NotificationCompat.Builder.setGroup() का इस्तेमाल करें. उदाहरण के लिए, किसी सुझाव को ऐसे ग्रुप का हिस्सा बताने के लिए जिसमें ट्रेंड में चल रहा नया कॉन्टेंट शामिल है, setGroup("trending") को कॉल किया जा सकता है.

सुझाव देने वाली सेवा बनाना

कॉन्टेंट के सुझाव, बैकग्राउंड में प्रोसेसिंग करके बनाए जाते हैं. अगर आपको अपने ऐप्लिकेशन से सुझाव देने हैं, तो एक ऐसी सेवा बनाएं जो समय-समय पर, आपके ऐप्लिकेशन के कैटलॉग से लिस्टिंग को, सिस्टम के सुझावों की सूची में जोड़ती हो.

यहां दिए गए कोड के उदाहरण से पता चलता है कि अपने ऐप्लिकेशन के लिए, सुझाव देने वाली सेवा बनाने के लिए, IntentService को कैसे बढ़ाया जाए:

Kotlin

class UpdateRecommendationsService : IntentService("RecommendationService") {
    override protected fun onHandleIntent(intent: Intent) {
        Log.d(TAG, "Updating recommendation cards")
        val recommendations = VideoProvider.getMovieList()
        if (recommendations == null) return

        var count = 0

        try {
            val builder = RecommendationBuilder()
                    .setContext(applicationContext)
                    .setSmallIcon(R.drawable.videos_by_google_icon)

            for (entry in recommendations.entrySet()) {
                for (movie in entry.getValue()) {
                    Log.d(TAG, "Recommendation - " + movie.getTitle())

                    builder.setBackground(movie.getCardImageUrl())
                            .setId(count + 1)
                            .setPriority(MAX_RECOMMENDATIONS - count)
                            .setTitle(movie.getTitle())
                            .setDescription(getString(R.string.popular_header))
                            .setImage(movie.getCardImageUrl())
                            .setIntent(buildPendingIntent(movie))
                            .build()
                    if (++count >= MAX_RECOMMENDATIONS) {
                        break
                    }
                }
                if (++count >= MAX_RECOMMENDATIONS) {
                    break
                }
            }
        } catch (e: IOException) {
            Log.e(TAG, "Unable to update recommendation", e)
        }
    }

    private fun buildPendingIntent(movie: Movie): PendingIntent {
        val detailsIntent = Intent(this, DetailsActivity::class.java)
        detailsIntent.putExtra("Movie", movie)

        val stackBuilder = TaskStackBuilder.create(this)
        stackBuilder.addParentStack(DetailsActivity::class.java)
        stackBuilder.addNextIntent(detailsIntent)

        // Ensure a unique PendingIntents, otherwise all
        // recommendations end up with the same PendingIntent
        detailsIntent.setAction(movie.getId().toString())

        val intent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT)
        return intent
    }

    companion object {
        private val TAG = "UpdateRecommendationsService"
        private val MAX_RECOMMENDATIONS = 3
    }
}

Java

public class UpdateRecommendationsService extends IntentService {
    private static final String TAG = "UpdateRecommendationsService";
    private static final int MAX_RECOMMENDATIONS = 3;

    public UpdateRecommendationsService() {
        super("RecommendationService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Log.d(TAG, "Updating recommendation cards");
        HashMap<String, List<Movie>> recommendations = VideoProvider.getMovieList();
        if (recommendations == null) return;

        int count = 0;

        try {
            RecommendationBuilder builder = new RecommendationBuilder()
                    .setContext(getApplicationContext())
                    .setSmallIcon(R.drawable.videos_by_google_icon);

            for (Map.Entry<String, List<Movie>> entry : recommendations.entrySet()) {
                for (Movie movie : entry.getValue()) {
                    Log.d(TAG, "Recommendation - " + movie.getTitle());

                    builder.setBackground(movie.getCardImageUrl())
                            .setId(count + 1)
                            .setPriority(MAX_RECOMMENDATIONS - count)
                            .setTitle(movie.getTitle())
                            .setDescription(getString(R.string.popular_header))
                            .setImage(movie.getCardImageUrl())
                            .setIntent(buildPendingIntent(movie))
                            .build();

                    if (++count >= MAX_RECOMMENDATIONS) {
                        break;
                    }
                }
                if (++count >= MAX_RECOMMENDATIONS) {
                    break;
                }
            }
        } catch (IOException e) {
            Log.e(TAG, "Unable to update recommendation", e);
        }
    }

    private PendingIntent buildPendingIntent(Movie movie) {
        Intent detailsIntent = new Intent(this, DetailsActivity.class);
        detailsIntent.putExtra("Movie", movie);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(DetailsActivity.class);
        stackBuilder.addNextIntent(detailsIntent);
        // Ensure a unique PendingIntents, otherwise all
        // recommendations end up with the same PendingIntent
        detailsIntent.setAction(Long.toString(movie.getId()));

        PendingIntent intent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        return intent;
    }
}

इस सेवा को सिस्टम से पहचानने और चलाने के लिए, इसे अपने ऐप्लिकेशन के मेनिफ़ेस्ट का इस्तेमाल करके रजिस्टर करें. यहां दिए गए कोड स्निपेट से पता चलता है कि इस क्लास को सेवा के तौर पर कैसे बताया जाए:

<manifest ... >
  <application ... >
    ...

    <service
            android:name="com.example.android.tvleanback.UpdateRecommendationsService"
            android:enabled="true" />
  </application>
</manifest>

सुझाव बनाना

सुझाव देने वाली सेवा शुरू होने के बाद, उसे सुझाव बनाने और उन्हें Android फ़्रेमवर्क को पास करने होंगे. फ़्रेमवर्क को सुझाव, Notification ऑब्जेक्ट के तौर पर मिलते हैं. ये ऑब्जेक्ट, किसी खास टेंप्लेट का इस्तेमाल करते हैं और इन्हें किसी खास कैटगरी के साथ मार्क किया जाता है.

वैल्यू सेट करना

सुझाव वाले कार्ड के लिए, यूज़र इंटरफ़ेस एलिमेंट की वैल्यू सेट करने के लिए, आपको एक बिल्डर क्लास बनानी होगी. यह क्लास, यहां बताए गए बिल्डर पैटर्न को फ़ॉलो करती है. सबसे पहले, आपको सुझाव वाले कार्ड के एलिमेंट की वैल्यू सेट करनी होंगी.

Kotlin

class RecommendationBuilder {
    ...

    fun setTitle(title: String): RecommendationBuilder {
        this.title = title
        return this
    }

    fun setDescription(description: String): RecommendationBuilder {
        this.description = description
        return this
    }

    fun setImage(uri: String): RecommendationBuilder {
        imageUri = uri
        return this
    }

    fun setBackground(uri: String): RecommendationBuilder {
        backgroundUri = uri
        return this
    }

...

Java

public class RecommendationBuilder {
    ...

    public RecommendationBuilder setTitle(String title) {
            this.title = title;
            return this;
        }

        public RecommendationBuilder setDescription(String description) {
            this.description = description;
            return this;
        }

        public RecommendationBuilder setImage(String uri) {
            imageUri = uri;
            return this;
        }

        public RecommendationBuilder setBackground(String uri) {
            backgroundUri = uri;
            return this;
        }
...

सूचना बनाना

वैल्यू सेट करने के बाद, आपको सूचना बनानी होगी. इसके लिए, बिल्डर क्लास से वैल्यू को सूचना में असाइन करें और NotificationCompat.Builder.build() को कॉल करें.

इसके अलावा, setLocalOnly() को कॉल करना न भूलें, ताकि NotificationCompat.BigPictureStyle सूचना, अन्य डिवाइसों पर न दिखे.

यहां दिए गए कोड के उदाहरण से पता चलता है कि सुझाव कैसे बनाया जाए.

Kotlin

class RecommendationBuilder {
    ...

    @Throws(IOException::class)
    fun build(): Notification {
        ...

        val notification = NotificationCompat.BigPictureStyle(
        NotificationCompat.Builder(context)
                .setContentTitle(title)
                .setContentText(description)
                .setPriority(priority)
                .setLocalOnly(true)
                .setOngoing(true)
                .setColor(context.resources.getColor(R.color.fastlane_background))
                .setCategory(Notification.CATEGORY_RECOMMENDATION)
                .setLargeIcon(image)
                .setSmallIcon(smallIcon)
                .setContentIntent(intent)
                .setExtras(extras))
                .build()

        return notification
    }
}

Java

public class RecommendationBuilder {
    ...

    public Notification build() throws IOException {
        ...

        Notification notification = new NotificationCompat.BigPictureStyle(
                new NotificationCompat.Builder(context)
                        .setContentTitle(title)
                        .setContentText(description)
                        .setPriority(priority)
                        .setLocalOnly(true)
                        .setOngoing(true)
                        .setColor(context.getResources().getColor(R.color.fastlane_background))
                        .setCategory(Notification.CATEGORY_RECOMMENDATION)
                        .setLargeIcon(image)
                        .setSmallIcon(smallIcon)
                        .setContentIntent(intent)
                        .setExtras(extras))
                .build();

        return notification;
    }
}

सुझाव देने वाली सेवा चलाना

आपके ऐप्लिकेशन की सुझाव देने वाली सेवा को समय-समय पर चलना चाहिए, ताकि मौजूदा सुझाव बनाए जा सकें. अपनी सेवा चलाने के लिए, एक ऐसी क्लास बनाएं जो टाइमर चलाती हो और उसे नियमित अंतराल पर लागू करती हो. यहां दिए गए कोड के उदाहरण में, BroadcastReceiver क्लास को बढ़ाकर, सुझाव देने वाली सेवा को हर आधे घंटे में समय-समय पर चलाने का तरीका बताया गया है:

Kotlin

class BootupActivity : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        Log.d(TAG, "BootupActivity initiated")
        if (intent.action.endsWith(Intent.ACTION_BOOT_COMPLETED)) {
            scheduleRecommendationUpdate(context)
        }
    }

    private fun scheduleRecommendationUpdate(context: Context) {
        Log.d(TAG, "Scheduling recommendations update")
        val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
        val recommendationIntent = Intent(context, UpdateRecommendationsService::class.java)
        val alarmIntent = PendingIntent.getService(context, 0, recommendationIntent, 0)
        alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                INITIAL_DELAY,
                AlarmManager.INTERVAL_HALF_HOUR,
                alarmIntent
        )
    }

    companion object {
        private val TAG = "BootupActivity"
        private val INITIAL_DELAY:Long = 5000
    }
}

Java

public class BootupActivity extends BroadcastReceiver {
    private static final String TAG = "BootupActivity";

    private static final long INITIAL_DELAY = 5000;

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "BootupActivity initiated");
        if (intent.getAction().endsWith(Intent.ACTION_BOOT_COMPLETED)) {
            scheduleRecommendationUpdate(context);
        }
    }

    private void scheduleRecommendationUpdate(Context context) {
        Log.d(TAG, "Scheduling recommendations update");

        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent recommendationIntent = new Intent(context, UpdateRecommendationsService.class);
        PendingIntent alarmIntent = PendingIntent.getService(context, 0, recommendationIntent, 0);

        alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                INITIAL_DELAY,
                AlarmManager.INTERVAL_HALF_HOUR,
                alarmIntent);
    }
}

BroadcastReceiver क्लास का यह लागू करने का तरीका, टीवी डिवाइस के चालू होने के बाद चलना चाहिए. इस क्लास को टीवी डिवाइस पर इंस्टॉल किया गया है. ऐसा करने के लिए, इस क्लास को अपने ऐप्लिकेशन के मेनिफ़ेस्ट में, इंटेंट फ़िल्टर के साथ रजिस्टर करें. यह फ़िल्टर, डिवाइस के बूट होने की प्रोसेस पूरी होने पर सुनता है. यहां दिए गए सैंपल कोड से पता चलता है कि इस कॉन्फ़िगरेशन को मेनिफ़ेस्ट में कैसे जोड़ा जाए:

<manifest ... >
  <application ... >
    <receiver android:name="com.example.android.tvleanback.BootupActivity"
              android:enabled="true"
              android:exported="false">
      <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
      </intent-filter>
    </receiver>
  </application>
</manifest>

अहम जानकारी: बूट होने की प्रोसेस पूरी होने की सूचना पाने के लिए, आपके ऐप्लिकेशन को RECEIVE_BOOT_COMPLETED अनुमति का अनुरोध करना होगा. ज़्यादा जानकारी के लिए, ACTION_BOOT_COMPLETED देखें.

सुझाव देने वाली सेवा की क्लास के onHandleIntent() तरीके में, सुझाव को मैनेजर को इस तरह पोस्ट करें:

Kotlin

val notification = notificationBuilder.build()
notificationManager.notify(id, notification)

Java

Notification notification = notificationBuilder.build();
notificationManager.notify(id, notification);