【发布时间】: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