在我的应用程序中,我弹出一个对话框并有一个按钮。我想按钮在每次单击时更改中断过滤器(无,优先,全部)。单击按钮时,通知模式不会更改,控制台会显示尚未绑定的通知侦听器服务。我在通知访问设置中启用了应用。我究竟做错了什么?有没有更好的方法来更改通知模式?
myDialog.java
中的 onCreate()
:
final NotificationService notifs = new NotificationService();
getContext().bindService(new Intent(getContext(), NotificationService.class), new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
}, Context.BIND_AUTO_CREATE);
getContext().startService(new Intent(getContext(), NotificationService.class));
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(notifs.getCurrentInterruptionFilter() == NotificationListenerService.INTERRUPTION_FILTER_NONE){
//set all
button.setBackground(ContextCompat.getDrawable(getContext(), volume));
notifs.requestInterruptionFilter(NotificationService.INTERRUPTION_FILTER_ALL);
} else if(notifs.getCurrentInterruptionFilter() == NotificationService.INTERRUPTION_FILTER_PRIORITY) {
//set none
button.setBackground(ContextCompat.getDrawable(getContext(), none));
notifs.requestInterruptionFilter(NotificationService.INTERRUPTION_FILTER_NONE);
} else {
//set priority
button.setBackground(ContextCompat.getDrawable(getContext(), priority));
notifs.requestInterruptionFilter(NotificationService.INTERRUPTION_FILTER_PRIORITY);
}
}
});
NotificationService.java
:
public class NotificationService extends NotificationListenerService {
public NotificationService() {
}
@Override
public void onCreate() {
super.onCreate();
Log.d("NOTIF", "Started");
}
}
在我的AndroidManifest.xml
:
<service
android:name=".NotificationService"
android:enabled="true"
android:exported="true"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
答案 0 :(得分:1)
我做错了什么?
NotificationListenerService
是Service
。因此,您不会创建NotificationListenerService
的匿名内部类实现。相反,您为它创建一个常规的public
Java类,并在清单中注册它,如the documentation所示。此服务将由Android自动设置,当且仅当用户选择启用您的应用以收听通知时,进入设置并启用它。