nerepla
nerepla

Reputation: 31

Android Notification Listener Service starting by itself

Im trying to make an app that getting all notifications and do the task. But my problem is NotifycationListener service starting automaticly even i didnt call the start service. Its starting as soon as I allow the app notification access on my phone. So service starting somehow and I cant stop it. Im already try simple services they working correctly. But this one with the NotificationListener is really painful. I just want to start and stop this service by my command.

Service start by itself when I allow this (screenshot)

enter image description here

MainActivity

package com.example.alperen.nservice2;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

Button startB,stopB;
Intent intent;
int count=0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    startB=(Button)findViewById(R.id.button);
    stopB=(Button)findViewById(R.id.button2);


    startB.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            intent = new Intent(MainActivity.this,MyService.class);
            startService(intent);
        }
    });

    stopB.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            stopService(intent);
        }
    });

}
}

MyService Class

package com.example.alperen.nservice2;

import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;

public class MyService extends NotificationListenerService{

@Override
public void onCreate() {
    super.onCreate();
    System.out.println("********* SERVICE STARTED ***********");
}

@Override
public void onNotificationPosted(StatusBarNotification sbn) {

    System.out.println("***** notification *****");
    String pack,title,text;
    Bundle extras;

    try {
        pack = sbn.getPackageName();
        extras = sbn.getNotification().extras;
        title = extras.getString("android.title").toString();
        text = extras.getCharSequence("android.text").toString();
    }catch (Exception e)
    {
        System.out.println("**** HATA NOTIFYSERVICE CLASS ****");
        pack="empty1";
        title="empty1";
        text="empty1";
        System.out.println("**** "+pack+" ****");
    }

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

    Toast.makeText(this,"title: "+title+" text: "+text,Toast.LENGTH_LONG).show();
}

@Override
public void onDestroy() {
    System.out.println("***** destroyed *****");
    super.onDestroy();
}
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.alperen.nservice2">

<uses-permission android:name="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"
    tools:ignore="ProtectedPermissions" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service android:name=".MyService"
        android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
        <intent-filter>
            <action android:name="android.service.notification.NotificationListenerService" />
        </intent-filter>
        ></service>

</application>

And the tricky part is in the Manifest. When I delete intent-filter part and run the app. It doesn't want to notification access anymore and it doesn't start by itself. I can start and stop the service from the MainActivity with buttons. But this time the app not getting the notification.

            // just delete this lines
            <intent-filter>
            <action android:name="android.service.notification.NotificationListenerService" />
        </intent-filter>

Upvotes: 2

Views: 7060

Answers (2)

keyboardr
keyboardr

Reputation: 851

This is probably a bit late, but for whoever else stumbles upon this, the correct approach would be to use PackageManager.setComponentEnabledSetting() to toggle whether the NotificationListenerService is enabled. If desired, it would also be a good idea to set android:enabled to false in the manifest so that it starts off disabled until you're ready to enable it.

Upvotes: 0

Pawel
Pawel

Reputation: 17258

This is an intended behavior.

When you grant notification access permission (or restart the phone) then SYSTEM is binding to your NotificationListenerService in order to send notification data.

If you override your services onBind method and log it, you will see it's being called.

Upvotes: 3

Related Questions