Some doubts regarding `BroadcastReceiver` in Android - java

I have been scratching my head for the past few days. I am trying to intercept incoming calls in Android. For that I have used a BroadcastReceiver and registered it in my AndroidManifest.xml as follows
<receiver android:name="org.myapp.IncomingCallReceiver"
android:permission="android.permission.READ_PHONE_STATE" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
I am able to get the incoming call number in the onReceive method of my IncomingCallReceiver. But the broadcast receiver does not work if I close my app. I have seen some libraries that does work in the background. For example telephony is a flutter plugin that receives incoming SMSs using broadcast receiver. When my app using the telephony plugin is killed, it continues to receives messages in its IncomingSmsReceiver. I am not quite sure how this is happening. From adb logcat I have seen that a new proecess is created with the broadcast receiver when a new Sms is received (and the app is closed).
D Zygote : Forked child process 6287
I ActivityManager: Start proc 6287:org.sudipta.sms_handler_example/u0a525 for broadcast {org.sudipta.sms_handler_example/com.shounakmulay.telephony.sms.IncomingSmsReceiver}
One thing to notice is that the plugin does not use any Service. There is a closed issue in github regarding this. The author of the plugin replied that
Yes listening in the background will work even if you don't open the app. The reason this works is we are registering a static broadcast receiver in the app's manifest file. That means the android system can initialize that broadcast receiver independent of the app.
My question is that if this really is the case. Will Android system keep the broadcast receiver alive (by forking a new process)? My understanding is that by default this is not going to work, al least that is not happening with my code, the receiver does not get hit when a call is received and the app is not running.
The author of the plugin further replied that
In most cases the device manufacturers and custom android OS environments block most of the app from performing operations in the background. That means your app won't receive any broadcasts and if it tries to start services in the background those will be killed as well.
But I have created a single sample app and using Telephony plugin and my own code (a simple receiver with the above manifest entry). While the plugin works in the background, my code doesn't.
Some help would be nice.
Thanks

Related

onNotificationPosted not being called after some time

I use NotificationListenerService to catch notifications and elaborate them in my application. App works great and onNotificationPosted is always called, but after a while (Few hours) it stops being called and the app stops working.
It gets back to work only if I reboot my device or if I revoke and grant again notification access to the application.
Important info
App does have notification access enabled.
App does have service on AndroidManifest.
<service
android:name=".NotificationService"
android:label="#string/app_name"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService"/>
</intent-filter>
</service>
App is a release version (Same problem on debug version though).
It has been tested on Android Lollipop, Marshmallow and Nougat, same result.
It has been tested in different devices running the same Android version.
App stops working in both background and activity displayed on screen.
It works again if I reinstall the application, but this is already included in 'revoke and grant notification access again' previously mentioned.
App includes AdMob banner on bottom, is it a possible problem?
I read Android has a cache problem or something like that. Is this the problem? Is there any permanent solution that I couldn't find anywhere?
I found the answer myself here:
https://gist.github.com/xinghui/b2ddd8cffe55c4b62f5d8846d5545bf9
Just create a new class as in the link above, then put in onCreate:
startService(new Intent(this, NotificationCollectorMonitorService.class));
And in AndroidManifest:
<service android:name=".NotificationCollectorMonitorService"/>
This will restart your NotificationListenerService whenever Android kills it.

Broadcast intents not working?

I have been following TheNewBoston tutorials on Broadcast intents, but I have an issue. Basically, I have two apps:
1) Recieve Broadcast
2)Send_Broadcast.
They do what their name suggests using intents, and Recieve Broadcast responds to the broadcast by displaying a toast on the screen. At this point of time, I have built both apps, and am getting no errors. After running both though, nothing happens when I send the broadcast by pushing the button. Please help me with this issue, as I have been trying to fix this for almost two hours now! (I have also re-set the api from 8-14)
This is the java class for send broadcast, it sends the broadcast when you push this button:
This is the XML for send broadcast, it sends the broadcast when you push this button:
Here is the receiver:
And the manifest which filters out certain broadcasts:
I have not added a layout to recieve broadcast, it works behind the scenes. Thank you so much for all of your help, I really appreciate it. If you need more information, feel free to ask!
I would appreciate any feedback! :)
Did you intend to put your filter on your receiver?
<receiver android:name=".RecieveBroadcast">
<intent-filter>
<action android:name="com.example.xxx.send_broadcast" />
</intent-filter>
</receiver>

Get notification history for android application

I am trying to do something that I am not sure is possible or not . I have created my android application "ABC" . I have used GCM to issue notifications from my 3rd party service.
Now my client asks that he wants a history of all notifications issued by the 3rd party server . I am sceptical about this .
I believe I might be able to show a list of all notifications issued by the server WHICH ARE RESPONDED upon by the user . However I think the notifications on which the user has not responded cannot be recorded by my application .
Is this true ?
I have this broadcast receiver in my code that responds to GCM messages.
<receiver
android:name="com.mob.uae.notifications.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.evento.uae" />
</intent-filter>
</receiver>
Shouldn't this be called regardless of the user's action upon the notification ? . If this is called even when my application is shut down , then I think we do have a way to record all notifications targeted for me .
Any idea geniuses ?
The broadcast receiver is activated every time a GCM message reaches the device, regardless of the user's action. Therefore, if you wish to record the message locally on each device, you can do it.
You can even record it in the server side, if the code in the broadcast receiver would trigger a call to your server to notify it that the message was received by the app (in this case it would be better to start an intent service from the receiver, and let the service do the communication with the server).

Will I receive GCM messages if Android kill my app and if I do a Force Close from the settings?

I'm a newbie to Android development and I'm interested in two things which is connected to Google Cloud Messaging.
Does Android absolutely kill applications if they run for a long time in background as iOS does? And if so will I receive GCM notifications after my app was killed by Android?
Is there some difference between Force Close (from the settings menu) and when the app is killed by Android? And if I do Force Close will I receive GCM notifications?
1 - yes, but if you install in your manifest a broadcast receiver that listens for the gcm, it will triggered anyway. What you will do with that event depends on your app. A common practice is to start an intent service that handles the message. It's up to you to make interact with the activities of your app.
2 - From android 3.1, if the user force closes your app, it will stop to be notified of any broadcast until the user does not start your app again. Check "Launch controls on stopped applications" here for more details.

How to Detect an Application launch and Exit in android

I would like to find out how our Android app can listen when some application is launch (and exit) in an Android device by a user(How to listen to it through Broadcast Receivers or any other way)?
I have searched a lot but not find any satisfactory solution. Please help in this regard. Thanks a lot for your time.
I don't think there's any default way to do that by broadcasts.
You could try using ActivityManager.getRunningAppProcesses
Correction: Listen for the broadcasts android.intent.action.MAIN and android.intent.category.LAUNCHER. Create a broadcast receiver and in the onReceive method check to see the name of the application that is started.

Categories

Resources