【问题标题】:NotificationListenerService in androidTvandroidTv 中的 NotificationListenerService
【发布时间】:2018-03-02 14:12:23
【问题描述】:

我正在开发一个将在 androidTv 中运行的 android 应用程序(目前我正在使用 MiBox 进行测试)
要求就像我需要捕获 android OS 收到的活动通知数量并将其显示在 APP 的某个位置。

我写的服务:

package org.libsdl.app;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Notification;
import android.app.Notification.Action;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import org.haxe.lime.HaxeObject;

public class AndroidNotificationListener extends NotificationListenerService {

    private static final int EVENT_UPDATE_CURRENT_NOS = 0;
    public static List<StatusBarNotification[]> mCurrentNotifications = new                 
    ArrayList<StatusBarNotification[]>();
    public static int mActiveNotificationsCount = 0;
    public static StatusBarNotification mPostedNotification;
    public static StatusBarNotification mRemovedNotification;

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return super.onBind(intent);
    }

    @Override
    public boolean onUnbind(Intent mIntent) {
        return super.onUnbind(mIntent);
    }

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        updateCurrentNotifications();
        mPostedNotification = sbn;

        // Get the text from notification
        // CharSequence notificationText = sbn.getNotification().extras.getCharSequence(Notification.EXTRA_TEXT);

        // Show a toast with your Notification Text
        // Toast.makeText(getApplicationContext(), notificationText, Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {
        updateCurrentNotifications();
        mRemovedNotification = sbn;
    }

    public void onListenerConnected() {
    // Add some prints here to check if our service is connected to OS or not?
    }

    private void updateCurrentNotifications() {
        try {
            StatusBarNotification[] activeNos = getActiveNotifications();
            if (mCurrentNotifications.size() == 0) {
                mCurrentNotifications.add(null);
            }
            mCurrentNotifications.set(0, activeNos);
            mActiveNotificationsCount = activeNos.length;
        } catch (Exception e) {
            System.out.println("Anil : AndroidNotificationListener  : Should not be here!!");
            e.printStackTrace();
        }
     }

     public static StatusBarNotification[] getCurrentNotifications() {
        if (mCurrentNotifications.size() == 0) {
            return null;
        }
        return mCurrentNotifications.get(0);
    }
    }

AndroidManifest.xml 部分是:

<service
android:name="org.libsdl.app.AndroidNotificationListener"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"
android:enabled="true" >
<intent-filter>
    <action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>

我的手机也可以使用相同的服务,但在我的 androidTv(MiBox) 中却无法使用。
和在手机中一样,我们可以通过设置启用/禁用我们的应用程序以接收通知,MiBox 中没有相同的选项。
我的 MiBox 有 Android M,我的手机有 Android N。在 Android 服务中运行此服务之前,我是否遗漏了一些我应该知道的内容?
所以我的问题是为什么这项服务在 androidTv 中不起作用?
对此的任何帮助将不胜感激..

【问题讨论】:

    标签: android notifications android-notifications android-tv


    【解决方案1】:

    我找到了我的虚拟应用程序在我的手机上运行而在 miBox 上运行的原因。 在我的手机上,当我安装虚拟应用程序时,它要求允许接收通知,当我授予权限时,它开始接收权限。 在 miBox 上,我们没有任何方式允许我们的应用接收通知。 简而言之,应用程序无法在miBox上接收通知,因为应用程序没有权限,因为或者附加了接收通知的侦听器/应用程序。

    并且我们无法将通知监听器附加到我们在 android tvs 中的应用程序。但是只有当应用程序被签名为系统应用程序时才能这样做。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-04
      • 1970-01-01
      • 1970-01-01
      • 2021-06-13
      • 1970-01-01
      • 2018-09-17
      相关资源
      最近更新 更多