androidTv中的NotificationListenerService

问题描述 投票:1回答:1

我正在开发一个将在androidTv中运行的Android应用程序(目前我正在使用MiBox进行测试) 要求就像我需要捕获Android操作系统收到的号码活动通知并将其显示在APP的某个地方。

Service which i've written:

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个回答
0
投票

我找到了我的虚拟应用程序在我的手机上工作并且没有在miBox上工作的原因。在我的手机上,当我安装虚拟应用程序时,它要求获得接收通知的许可,当我给予许可时,它开始获得许可。在miBox上,我们没有办法授权我们的应用程序接收通知。简而言之,该应用程序无法接收miBox上的通知,因为该应用程序没有权限,因为或附加了通知的侦听器/应用程序。

并且我们无法在Android电视中附加监听器以通知我们的应用程序。但是,只有当应用程序作为系统应用程序签名时,我们才能这样做。

© www.soinside.com 2019 - 2024. All rights reserved.