NotificationListener.requestInterruptionFilter示例

时间:2015-06-16 20:30:39

标签: android android-service android-notifications android-5.0-lollipop

我一直在寻找一个样本或教程来改变Lollipop的中断。但我没有找到任何完整的。我看到了想法或提示,但不完整。有了这些想法,我创建了一个NotificationListener,但是我得到了这个错误:

java.lang.ClassCastException: android.service.notification.NotificationListenerService $ INotificationListenerWrapper无法强制转换为My.App.MyNotificationListenerService

我的宣言:

    <service android:name=".MyNotificationListenerService"
          android:label="Some text here"
          android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
          <intent-filter>
             <action android:name="android.service.notification.NotificationListenerService" />
          </intent-filter>
      </service>  

My MyNotificationListenerService类:

public class MyNotificationListenerService extends NotificationListenerService
{  
   @Override public void onCreate() 
   {  super.onCreate();       
   }


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


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

}

我的活动:

Intent serviceIntent = new Intent(MyActivity.this, MyNotificationListenerService.class);
MyServiceConnection oMyServiceConnection = new MyServiceConnection();
bindService(serviceIntent, oMyServiceConnection, Context.BIND_AUTO_CREATE);
//Now we wait onconnect


class MyServiceConnection implements ServiceConnection 
{  @Override public void onServiceConnected(ComponentName name, IBinder binder) 
   {  MyNotificationListenerService oMyNotificationListenerService= ((MyNotificationListenerService ) binder).getService(); <-- here is where I get the ClassCastException

      int iFilter = NotificationListenerService.INTERRUPTION_FILTER_PRIORITY; //Or INTERRUPTION_FILTER_ALL or INTERRUPTION_FILTER_NONE
      oMyNotificationListenerService.requestInterruptionFilter(iFilter);              

   }

   @Override  public void onServiceDisconnected(ComponentName name) 
   {  
   }
}

1 个答案:

答案 0 :(得分:1)

source of NotificationListenerService所示,NotificationListenerService已实施onBind()NotificationListenerService与系统进行通信的方式。

通过重写onBind()来返回您自己的界面,与系统通信的方法都不起作用 - 您应该看到"Unable to contact notification manager"行的详细级别日志消息。

您可以使用其他方法与您的服务进行通信,例如LocalBroadcastManager