如何在Android上启动NotificationListenerService

如何在Android上启动NotificationListenerService,android,notifications,android-service,android-service-binding,notification-listener,Android,Notifications,Android Service,Android Service Binding,Notification Listener,我想使用NotificationListenerService访问Android手机上的通知。我查阅了很多教程,但我找不到他们在哪里调用该服务 我应该在MainActivity上使用bindService还是startService?意图应该是什么样子?有人能告诉我这个的语法吗 查看我正在研究的一个服务实现: public class NLService extends NotificationListenerService { Context context; @Override publ

我想使用NotificationListenerService访问Android手机上的通知。我查阅了很多教程,但我找不到他们在哪里调用该服务

我应该在MainActivity上使用bindService还是startService?意图应该是什么样子?有人能告诉我这个的语法吗

查看我正在研究的一个服务实现:

public class NLService extends NotificationListenerService {

Context context;

@Override
public void onCreate() {

    super.onCreate();
    context = getApplicationContext();


}


@Override
public void onNotificationPosted(StatusBarNotification sbn) {
    String pack = sbn.getPackageName();
    String ticker ="";
    if(sbn.getNotification().tickerText !=null) {
        ticker = sbn.getNotification().tickerText.toString();
    }
    Bundle extras = sbn.getNotification().extras;
    String title = extras.getString("android.title");
    String text = extras.getCharSequence("android.text").toString();
    int id1 = extras.getInt(Notification.EXTRA_SMALL_ICON);
    Bitmap id = sbn.getNotification().largeIcon;


    Log.i("Package",pack);
    Log.i("Ticker",ticker);
    Log.i("Title",title);
    Log.i("Text",text);

    Intent msgrcv = new Intent("Msg");
    msgrcv.putExtra("package", pack);
    msgrcv.putExtra("ticker", ticker);
    msgrcv.putExtra("title", title);
    msgrcv.putExtra("text", text);
    if(id != null) {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        //id.compress(Bitmap.CompressFormat.PNG, 100, stream);
        byte[] byteArray = stream.toByteArray();
        msgrcv.putExtra("icon",byteArray);
    }
    LocalBroadcastManager.getInstance(context).sendBroadcast(msgrcv);

}
@Override

public void onNotificationRemoved(StatusBarNotification sbn) {
    Log.i("Msg","Notification Removed");

}

}

对于侦听传入通知,无需显式启动。首先使用以下权限在清单中定义您的服务-

android.permission.BIND\u通知\u侦听器\u服务


或者通过手动授予通知访问权限。

在这种情况下,您必须从android系统获得通知?我应该通过哪种方法传递此意图?上述意图用于打开通知设置。它将列出所有请求通知访问的应用程序。在使用通知侦听器服务之前,必须向appOk授予通知访问权限。我之前已授予应用程序访问通知的权限。但是,这项服务仍然没有启动……这个词“开火”并没有任何意义。应该是startActivity使用的意图。
 <service android:name=".MyNotificationListener"
      android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
     <intent-filter>
         <action android:name="android.service.notification.NotificationListenerService" />
     </intent-filter>
 </service>
Intent intent=new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");