Praca w tle, skanowanie

0

Cześć wszystkim, natknąłem się ostatnio na pewien problem.
Piszę aplikacje co ma skanować pojawiające się powiadomienia na pasku i liczyć ich występowanie z konkretnej apki.

No i moja aplikacja musi działać w "tle" z tego co się zorientowałem.
Czyli po zamknięciu jej, musi działać service.

No i tu jest problem, jest to temat chyba dość trudny do ogarnięcia.
W swoim Mainie w onCreate uruchamiam serwis, jednak nie wiem czemu on mi ginie po około minucie.
implementuję NotificationListenerService i uruchamiam go w mainie:

 Intent serviceIntent = new Intent(this, MainService.class);
 startService(serviceIntent);

Natomiast Service:

public class MainService extends NotificationListenerService {

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


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

       

        return START_NOT_STICKY;
    }

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
       

        super.onNotificationPosted(sbn);
    }
}

Według tego co rozumiem, w metodzie onNotificationPosted z obiektu sbn mógłbym sb pobierać informacje i coś z nimi robić ?

tylko czego mi ginie ten serwis po minucie ? i co to za metoda onBind ?

1

Musisz stworzyć serwis typu foreground. Serwisy działające tylko i wyłącznie w tle bez wiedzy użytkownika są zabijane przez system. Więcej info tutaj.

0

Okey, uruchomiłem ten serwis jak mówiłeś typu foreground.
Pojawia mi się powiadomienie że on pracuje, i teraz system go nie zabija.
Jednak dalej jest coś źle bo pojawiają się komunikaty:

2020-11-29 14:02:27.719 19692-19778/xyz.pl I/AdrenoGLES: Build Config                     : S P 8.0.11 AArch64
2020-11-29 14:02:27.724 19692-19778/xyz.pl I/AdrenoGLES: PFP: 0x016ee187, ME: 0x00000000
2020-11-29 14:02:27.726 19692-19778/xyz.pl W/AdrenoUtils: <ReadGpuID_from_sysfs:194>: Failed to open /sys/class/kgsl/kgsl-3d0/gpu_model
2020-11-29 14:02:27.726 19692-19778/xyz.pl W/AdrenoUtils: <ReadGpuID:218>: Failed to read chip ID from gpu_model. Fallback to use the GSL path
2020-11-29 14:02:27.719 19692-19692/xyz.pl W/RenderThread: type=1400 audit(0.0:417614): avc: denied { search } for name="kgsl" dev="sysfs" ino=46510 scontext=u:r:untrusted_app:s0:c46,c257,c512,c768 tcontext=u:object_r:sysfs_kgsl:s0 tclass=dir permissive=0
2020-11-29 14:02:27.734 19692-19692/xyz.pl D/ForceDarkHelper: updateByCheckExcludeList: pkg: xyz.pl activity: xyz.pl.MainActivity@c209744
2020-11-29 14:02:27.736 19692-19692/xyz.pl D/ForceDarkHelper: updateByCheckExcludeList: pkg: xyz.pl activity: xyz.pl.MainActivity@c209744
2020-11-29 14:02:27.739 19692-19692/xyz.pl D/ForceDarkHelper: updateByCheckExcludeList: pkg: xyz.pl activity: xyz.pl.MainActivity@c209744
2020-11-29 14:02:27.748 19692-19692/xyz.pl I/chatty: uid=10302(xyz.pl) identical 3 lines
2020-11-29 14:02:27.752 19692-19692/xyz.pl D/ForceDarkHelper: updateByCheckExcludeList: pkg: xyz.pl activity: xyz.pl.MainActivity@c209744
2020-11-29 14:02:27.753 19692-19788/xyz.pl I/FA: Tag Manager is not found and thus will not be used
2020-11-29 14:02:27.753 19692-19692/xyz.pl D/ForceDarkHelper: updateByCheckExcludeList: pkg: xyz.pl activity: xyz.pl.MainActivity@c209744
2020-11-29 14:02:27.760 19692-19692/xyz.pl I/chatty: uid=10302(xyz.pl) identical 3 lines
2020-11-29 14:02:27.761 19692-19692/xyz.pl D/ForceDarkHelper: updateByCheckExcludeList: pkg: xyz.pl activity: xyz.pl.MainActivity@c209744
2020-11-29 14:02:27.774 19692-19778/xyz.pl W/Gralloc3: mapper 3.x is not supported
2020-11-29 14:02:27.844 19692-19778/xyz.pl E/LB: fail to open file: No such file or directory
2020-11-29 14:02:27.849 1740-1928/? I/Timeline: Timeline: Activity_windows_visible id: ActivityRecord{1b2629c u0 xyz.pl/.MainActivity t917} time:574629339
2020-11-29 14:02:27.852 1740-1953/? I/ActivityTaskManager: Displayed xyz.pl/.MainActivity: +1s343ms
2020-11-29 14:02:27.910 19692-19788/xyz.pl D/FA: Connected to remote service
2020-11-29 14:02:28.093 1740-5494/? W/BroadcastQueue: Background execution not allowed: receiving Intent { act=android.intent.action.PACKAGE_REMOVED dat=package:xyz.pl flg=0x4000010 (has extras) } to com.xiaomi.xmsf/com.xiaomi.push.service.receivers.PkgUninstallReceiver
2020-11-29 14:02:28.117 1740-5495/? W/BroadcastQueue: Background execution not allowed: receiving Intent { act=android.intent.action.PACKAGE_REMOVED dat=package:xyz.pl flg=0x4000010 (has extras) } to com.google.android.apps.photos/com.google.android.libraries.social.peoplekit.thirdparty.viewcontrollers.ThirdPartyReceiver
2020-11-29 14:02:28.122 1740-1954/? W/BroadcastQueue: Background execution not allowed: receiving Intent { act=android.intent.action.PACKAGE_REMOVED dat=package:xyz.pl flg=0x4000010 (has extras) } to com.microsoft.office.excel/com.microsoft.office.notification.SystemBroadcastReceiver
2020-11-29 14:02:28.122 1740-1954/? W/BroadcastQueue: Background execution not allowed: receiving Intent { act=android.intent.action.PACKAGE_REMOVED dat=package:xyz.pl flg=0x4000010 (has extras) } to com.microsoft.office.powerpoint/com.microsoft.office.notification.SystemBroadcastReceiver
2020-11-29 14:02:28.122 1740-1954/? W/BroadcastQueue: Background execution not allowed: receiving Intent { act=android.intent.action.PACKAGE_REMOVED dat=package:xyz.pl flg=0x4000010 (has extras) } to com.microsoft.office.word/com.microsoft.office.notification.SystemBroadcastReceiver
2020-11-29 14:02:28.122 1740-1954/? W/BroadcastQueue: Background execution not allowed: receiving Intent { act=android.intent.action.PACKAGE_ADDED dat=package:xyz.pl flg=0x4000010 (has extras) } to com.lbe.security.miui/com.lbe.security.service.BootReceiver
2020-11-29 14:02:28.123 1740-1954/? W/BroadcastQueue: Background execution not allowed: receiving Intent { act=android.intent.action.PACKAGE_ADDED dat=package:xyz.pl flg=0x4000010 (has extras) } to com.google.android.packageinstaller/com.android.packageinstaller.PackageInstalledReceiver
2020-11-29 14:02:28.125 1740-5495/? W/BroadcastQueue: Background execution not allowed: receiving Intent { act=android.intent.action.PACKAGE_ADDED dat=package:xyz.pl flg=0x4000010 (has extras) } to com.android.vending/com.google.android.finsky.packagemanager.impl.PackageMonitorReceiverImpl$RegisteredReceiver
2020-11-29 14:02:28.128 1740-1954/? W/BroadcastQueue: Background execution not allowed: receiving Intent { act=android.intent.action.PACKAGE_ADDED dat=package:xyz.pl flg=0x4000010 (has extras) } to com.facebook.appmanager/com.facebook.oxygen.common.packages.event.manifest.ManifestPackageEventReceiver
2020-11-29 14:02:28.128 1740-1954/? W/BroadcastQueue: Background execution not allowed: receiving Intent { act=android.intent.action.PACKAGE_ADDED dat=package:xyz.pl flg=0x4000010 (has extras) } to com.google.android.gms/.games.chimera.GamesSystemBroadcastReceiverProxy
2020-11-29 14:02:28.129 1740-1954/? W/BroadcastQueue: Background execution not allowed: receiving Intent { act=android.intent.action.PACKAGE_ADDED dat=package:xyz.pl flg=0x4000010 (has extras) } to com.google.android.gms/.gass.chimera.PackageChangeBroadcastReceiver
2020-11-29 14:02:28.129 1740-1954/? W/BroadcastQueue: Background execution not allowed: receiving Intent { act=android.intent.action.PACKAGE_ADDED dat=package:xyz.pl flg=0x4000010 (has extras) } to com.google.android.gms/.chimera.GmsIntentOperationService$PersistentTrustedReceiver
2020-11-29 14:02:28.129 1740-1954/? W/BroadcastQueue: Background execution not allowed: receiving Intent { act=android.intent.action.PACKAGE_ADDED dat=package:xyz.pl flg=0x4000010 (has extras) } to com.google.android.googlequicksearchbox/com.google.android.apps.gsa.googlequicksearchbox.GelStubAppWatcher
2020-11-29 14:02:28.133 1740-5495/? W/BroadcastQueue: Background execution not allowed: receiving Intent { act=android.intent.action.PACKAGE_ADDED dat=package:xyz.pl flg=0x4000010 (has extras) } to com.miui.audioeffect/com.android.musicfx.Compatibility$Receiver
2020-11-29 14:02:28.134 1740-1954/? W/BroadcastQueue: Background execution not allowed: receiving Intent { act=android.intent.action.PACKAGE_ADDED dat=package:xyz.pl flg=0x4000010 (has extras) } to com.miui.global.packageinstaller/.AppInstalledReceiver
2020-11-29 14:02:28.134 1740-1954/? W/BroadcastQueue: Background execution not allowed: receiving Intent { act=android.intent.action.PACKAGE_ADDED dat=package:xyz.pl flg=0x4000010 (has extras) } to com.xiaomi.discover/com.xiaomi.market.data.MyPackageMonitor
2020-11-29 14:02:28.143 1740-5495/? W/BroadcastQueue: Background execution not allowed: receiving Intent { act=android.intent.action.PACKAGE_ADDED dat=package:xyz.pl flg=0x4000010 (has extras) } to com.GMA.Ball.Sort.Puzzle/com.mintegral.msdk.click.AppReceiver
2020-11-29 14:02:28.148 1740-5495/? W/BroadcastQueue: Background execution not allowed: receiving Intent { act=android.intent.action.PACKAGE_ADDED dat=package:xyz.pl flg=0x4000010 (has extras) } to com.fitatu.tracker/com.appodeal.ads.AppodealPackageAddedReceiver
2020-11-29 14:02:28.149 1740-1954/? W/BroadcastQueue: Background execution not allowed: receiving Intent { act=android.intent.action.PACKAGE_ADDED dat=package:xyz.pl flg=0x4000010 (has extras) } to com.microsoft.office.excel/com.microsoft.office.notification.SystemBroadcastReceiver
2020-11-29 14:02:28.149 1740-1954/? W/BroadcastQueue: Background execution not allowed: receiving Intent { act=android.intent.action.PACKAGE_ADDED dat=package:xyz.pl flg=0x4000010 (has extras) } to com.microsoft.office.powerpoint/com.microsoft.office.notification.SystemBroadcastReceiver
2020-11-29 14:02:28.149 1740-1954/? W/BroadcastQueue: Background execution not allowed: receiving Intent { act=android.intent.action.PACKAGE_ADDED dat=package:xyz.pl flg=0x4000010 (has extras) } to com.microsoft.office.word/com.microsoft.office.notification.SystemBroadcastReceiver
2020-11-29 14:02:28.154 19459-19459/? I/Finsky: [2] yoy.g(1): Invalidating cached PackageState for xyz.pl
2020-11-29 14:02:28.863 19846-19846/? I/FindDeviceGNReceiver: isReplace: true, packageName: package:xyz.pl. 
2020-11-29 14:02:28.864 1740-5494/? W/BroadcastQueue: Background execution not allowed: receiving Intent { act=android.intent.action.PACKAGE_REPLACED dat=package:xyz.pl flg=0x4000010 (has extras) } to com.facebook.appmanager/com.facebook.oxygen.common.packages.event.manifest.ManifestPackageEventReceiver
2020-11-29 14:02:28.867 1740-1954/? W/BroadcastQueue: Background execution not allowed: receiving Intent { act=android.intent.action.PACKAGE_REPLACED dat=package:xyz.pl flg=0x4000010 (has extras) } to com.google.android.gms/.gass.chimera.PackageChangeBroadcastReceiver
2020-11-29 14:02:28.867 1740-1954/? W/BroadcastQueue: Background execution not allowed: receiving Intent { act=android.intent.action.PACKAGE_REPLACED dat=package:xyz.pl flg=0x4000010 (has extras) } to com.google.android.gms/.chimera.GmsIntentOperationService$PersistentTrustedReceiver
2020-11-29 14:02:28.867 1740-1954/? W/BroadcastQueue: Background execution not allowed: receiving Intent { act=android.intent.action.PACKAGE_REPLACED dat=package:xyz.pl flg=0x4000010 (has extras) } to com.google.android.googlequicksearchbox/com.google.android.apps.gsa.googlequicksearchbox.GelStubAppWatcher
2020-11-29 14:02:28.867 1740-1954/? W/BroadcastQueue: Background execution not allowed: receiving Intent { act=android.intent.action.PACKAGE_REPLACED dat=package:xyz.pl flg=0x4000010 (has extras) } to com.miui.audioeffect/com.android.musicfx.Compatibility$Receiver
2020-11-29 14:02:28.867 1740-1954/? W/BroadcastQueue: Background execution not allowed: receiving Intent { act=android.intent.action.PACKAGE_REPLACED dat=package:xyz.pl flg=0x4000010 (has extras) } to com.google.android.apps.photos/.account.full.FetchAccountPropertiesAppUpgradeBroadcastReceiver
2020-11-29 14:02:28.867 1740-1954/? W/BroadcastQueue: Background execution not allowed: receiving Intent { act=android.intent.action.PACKAGE_REPLACED dat=package:xyz.pl flg=0x4000010 (has extras) } to com.google.android.apps.photos/.account.full.SyncAccountsForLoginBroadcastReceiver
2020-11-29 14:02:28.868 1740-1954/? W/BroadcastQueue: Background execution not allowed: receiving Intent { act=android.intent.action.PACKAGE_REPLACED dat=package:xyz.pl flg=0x4000010 (has extras) } to com.google.android.apps.photos/.notificationchannels.AppUpdateBroadcastReceiver
2020-11-29 14:02:32.965 19692-19788/xyz.pl W/ConnectionTracker: Exception thrown while unbinding
    java.lang.IllegalArgumentException: Service not registered: lp@1976c92
        at android.app.LoadedApk.forgetServiceDispatcher(LoadedApk.java:1759)
        at android.app.ContextImpl.unbindService(ContextImpl.java:1786)
        at android.content.ContextWrapper.unbindService(ContextWrapper.java:751)
        at ci.f(:com.google.android.gms.dynamite_measurementdynamite@[email protected] (120400-0):1)
        at ci.d(:com.google.android.gms.dynamite_measurementdynamite@[email protected] (120400-0):2)
        at lq.D(:com.google.android.gms.dynamite_measurementdynamite@[email protected] (120400-0):10)
        at lc.a(:com.google.android.gms.dynamite_measurementdynamite@[email protected] (120400-0):2)
        at ee.run(:com.google.android.gms.dynamite_measurementdynamite@[email protected] (120400-0):3)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:462)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at ix.run(:com.google.android.gms.dynamite_measurementdynamite@[email protected] (120400-0):6)
2020-11-29 14:02:35.235 19692-19765/xyz.pl I/xyz.pl: ProcessProfilingInfo new_methods=2260 is saved saved_to_disk=1 resolve_classes_delay=8000
2020-11-29 14:02:42.043 19692-19692/xyz.pl D/serwismaciek: start Serwis
2020-11-29 14:02:42.163 4088-4088/? D/StatusBar: onNotificationPosted key=0|xyz.pl|1|null|10302 isUpdate=false
2020-11-29 14:02:42.193 4088-4088/? D/StatusBar: No peeking: unimportant notification: 0|xyz.pl|1|null|10302
2020-11-29 14:02:42.247 4088-4088/? D/StatusBar: No peeking: unimportant notification: 0|xyz.pl|1|null|10302
2020-11-29 14:03:42.605 19692-20166/xyz.pl W/BpBinder: Slow Binder: BpBinder transact took 203 ms, interface=android.app.IActivityManager, code=27 oneway=false
2020-11-29 14:03:42.656 19692-20166/xyz.pl D/FA: Connected to remote service
2020-11-29 14:03:42.837 1740-1928/? I/Timeline: Timeline: Activity_windows_visible id: ActivityRecord{1b2629c u0 xyz.pl/.MainActivity t917} time:574704327
2020-11-29 14:03:43.324 6787-6952/? D/PowerCheckerService: foreground, uid = 10302, app = xyz.pl
2020-11-29 14:03:44.612 19692-20166/xyz.pl D/FA: Application going to the background
2020-11-29 14:03:49.689 19692-20166/xyz.pl W/ConnectionTracker: Exception thrown while unbinding
    java.lang.IllegalArgumentException: Service not registered: lp@1976c92
        at android.app.LoadedApk.forgetServiceDispatcher(LoadedApk.java:1759)
        at android.app.ContextImpl.unbindService(ContextImpl.java:1786)
        at android.content.ContextWrapper.unbindService(ContextWrapper.java:751)
        at ci.f(:com.google.android.gms.dynamite_measurementdynamite@[email protected] (120400-0):1)
        at ci.d(:com.google.android.gms.dynamite_measurementdynamite@[email protected] (120400-0):2)
        at lq.D(:com.google.androi d.gms.dynamite_measurementdynamite@[email protected] (120400-0):10)
        at lc.a(:com.google.android.gms.dynamite_measurementdynamite@[email protected] (120400-0):2)
        at ee.run(:com.google.android.gms.dynamite_measurementdynamite@[email protected] (120400-0):3)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:462)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at ix.run(:com.google.android.gms.dynamite_measurementdynamite@[email protected] (120400-0):6)

I metoda onStartCommand wykonuje się bo wświetla na końcu się log.
Jednak mam metodę onNotificationPosted - która powinna przechwytywać powiadomienia. I ona nie reaguje na nic.

Ktoś wie gdzie popełniam błąd ?

0

Ktoś wie, dlaczego jak przechwytuję powiadomienia np od aplikacji sms to mam tego znacznie więcej niż jedno powiadomienie? Tutaj akurat złapałem 3, ale chodzi mi tylko o ostatnie. A tak nie jest tylko z tą aplikacją, tak jest z każdą. Generalnie chodzi mi tylko o te powiadomienia widoczne dla użytkownika.
Bo te dwa pierwsze nie pojawiają się widocznie na pasku.

  1. D/VISIBILITY: 0 D/PACKAGENAME: com.xiaomi.simactivate.service
    D/NOTYFICATION: Notification(channel=com.xiaomi.simactivate.service.CHANNEL_ID pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x72 color=0x00000000 vis=PRIVATE)
    D/czas: Tue Dec 01 12:49:10 GMT+01:00 2020
    I/Package: com.xiaomi.simactivate.service
    I/appname: Xiaomi SIM card activation service
    I/Title: Getting activation information ...
    I/Text: Getting activation information ...

D/VISIBILITY: 0 D/PACKAGENAME: com.android.mms
D/NOTYFICATION: Notification(channel=Mms_Default pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x62 color=0xff607d8b vis=PRIVATE)
D/czas: Tue Dec 01 12:49:11 GMT+01:00 2020
I/Package: com.android.mms I/appname: SMS
I/Title: The SMS application is running
I/Text: Click to learn more or stop the application.

  1. D/VISIBILITY: 0 D/PACKAGENAME: com.android.mms
    D/NOTYFICATION: Notification(channel=Channel_Msg_Default1583586283170 pri=1 contentView=null vibrate=default sound=content://settings/system/sms_received_sound tick defaults=0x2 flags=0x11 color=0x00000000 category=msg vis=PRIVATE)
    D/czas: Tue Dec 01 12:49:11 GMT+01:00 2020
    I/Package: com.android.mms
    I/appname: SMS
    I/Title: +EHX90006ABC
    I/Text: test text
0

W jaki sposób skanujesz powiadomienia?

0

@Maciej123321:
my mamy taki system ktory wlasnie "skanuje" wszysykie powiadomienia z telefonowu na ktorych jest nasz software i wysyla to do API, akurat czescia android sie nie zajmuje ale moge ci powiedziec ze np jesli sms jest dlugi bedziesz go widzial jako kilka powiadomien, jesli chodzi o inne aplikacje to nie wiem bo akurat nie zajmuje sie ta czescia.

0

Uruchamiam serwis rozszerzając NotificationListenerService - tam jest metoda onNotificationPosted(StatusBarNotification sbn) dzięki której mam dostęp do powiadomień jakie się pojawiają. Akurat nie ma z tym problemu już ponieważ problem rozwiązałem filtrując widoczne powiadomienia po sbn.isClearable(). Bywa że czasem jest duplikat treści tego samego powiadomienia. Mimo że widzimy jedno, były dwa te same a stare zostało nadpisane. onNotyficationPosted reaguje wtedy kiedy się pojawia nowe, zostaje zmieniona treść powiadomienia. Bądź jego ID - często Gmail wysyła dwa razy to samo powiadomienie.
Nie mam problemu z tego typu smsami - ponieważ dostając takiego sms-a dostajesz jedno powiadomienie, a nie np trzy jak jest długi. Więc tutaj w tym serwis sobie spokojnie radzi @marcio

1 użytkowników online, w tym zalogowanych: 0, gości: 1