Android SMS Broadcast Receiver - android

Register BroadcastReceiver for SMS_RECEIVE in Manifest, on Android 4.2 my broadcast receiver is called even when app is closed (from recent also).
But on Android 7.0, its called when app is running or is in recent, if app is closed it is not being called. I tried to use service and registered Broadcast inside Service still same scenario.
My Receiver is:
<receiver
android:name=".BroadcastReceiver.SMSBroadcastReceiver"
android:enabled="true"
android:exported="true"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter android:priority="999">
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
Is something missing, or any other way, or now its how it will work?

https://developer.android.com/guide/components/broadcasts provides the following information:
Beginning with Android 8.0 (API level 26), the system imposes additional restrictions on manifest-declared receivers. If your app targets API level 26 or higher, you cannot use the manifest to declare a receiver for most implicit broadcasts (broadcasts that do not target your app specifically). You can still use a context-registered reciever when the user is actively using your app.
There is also a video about declaring static services/receivers in the manifest.
You probably need to update the code to use job scheduler or work manager. Google i/o was full of work manager videos.

I think you are missing runtime permissions. It was added after Android 6.0
check this .

Related

Receiver for DownloadManager.ACTION_DOWNLOAD_COMPLETE in Manifest

Is it possible to listen for DownloadManager.ACTION_DOWNLOAD_COMPLETE in Manifest.xml?
All the examples I found use registerReceiver(downloadCompleteReceiver,new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); from a class but I would like to receive it in Manifest.xml so I can listen when the app is closed.
I can't find this action when I try to set the intent-filter for my receiver in Manifest.xml
As the official documentation states:
Beginning with Android 8.0 (API level 26), the system imposes additional restrictions on manifest-declared receivers.
If your app targets Android 8.0 or higher, you cannot use the manifest to declare a receiver for most implicit broadcasts (broadcasts that don't target your app specifically). You can still use a context-registered receiver when the user is actively using your app.
From Is android.intent.action.DOWNLOAD_COMPLETE an explicit broadcast? we learn that android.intent.action.DOWNLOAD_COMPLETE seems to be an explicit broadcast, therefore there should be no issue defining a <receiver> for it in the manifest, even if it's not autocompleted. So just add it with an action of android.intent.action.DOWNLOAD_COMPLETE.
<receiver
android:name=".your.DownloadCompleteReceiver"
android:permission="android.permission.SEND_DOWNLOAD_COMPLETED_INTENTS"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
</intent-filter>
</receiver>

Android Background execution not allowed on Oreo (8.) for intent android.intent.action.EVENT_REMINDER

I have an app which has registered BroadcastReceiver which listens for intents android.intent.action.EVENT_REMINDER. From Oreo it doesnt work at all, until you add app as an exception to energy saving.
But that is in my opinion bug in android, because they explicitely say in documentation:
As part of the Android 8.0 (API level 26) Background Execution Limits,
apps that target the API level 26 or higher can no longer register
broadcast receivers for implicit broadcasts in their manifest.
However, several broadcasts are currently exempted from these
limitations
And ACTION_EVENT_REMINDER is in the list!
So if it has an exception, why am I not receiving broadcast, until I whitelist my app to energy optimizations? This should not be necessary, yet it is from Oreo.
Am I doing anything wrong? My manifest:
<receiver android:name=".services.MyReceiver" >
<intent-filter>
<action android:name="android.intent.action.EVENT_REMINDER" />
<data android:scheme="content" />
</intent-filter>
</receiver>
Works perfectly fine pre-Oreo. After Oreo ONLY if I add app as an exception to battery saving.

How reliable are Broadcast Receivers across different brand phones?

I have manifest declared Broadcasts for android.intent.action.PHONE_STATE and android.intent.action.NEW_OUTGOING_CALL. I tested my app after I removed it from Recent apps screen. I tested it on two phones:
MOTO G4 Play(Nougat)- After I removed my app from Recent apps screen on this phone I was receiving broadcasts.
ASUS(Lollipop)- After I removed my app from Recent apps screen on this phone I was not receiving broadcasts.
One way after reading such questions on SO I got is that I can do it by starting a sticky service so what it will do is not terminate my process and I will continue to get broadcasts.
What should I do to ensure that I receive broadcasts on every phone without using a service ?
Edit-1:I register my broadcasts as follows in Manifest
`
<receiver android:name=".PhonecallReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"
android:enabled="true"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL"
android:enabled="true"/>
</intent-filter>
</receiver>
`
#Override
public void onReceive(Context context, Intent intent) {
Log.e("onReceive ","Called"); //This is how I check whether broadcast was called or not
//... other code
}
Think of notification broad cast that can be used to generate notification for your app, so broadcast is pretty reliable.
You are using manifest declared broad cast so theoretically, you should be able to receive broadcast regardless of whether your app is in foreground or background ( similar behavior was expected in all phone since your app targets same API). But there has been cases where mobile devices has ignored/bug some of the contract as a result some expected behavior breaks. It is very likely that similar thing happened in your ASUS based test.
From the broadcast manager documentation ,
If you declare a broadcast receiver in your manifest, the system
launches your app (if the app is not already running) when the
broadcast is sent.
However there is exception .Android has some updates on this behavior recently to restrict some of this features.
Note: If your app targets API level 26 or higher, you cannot use the manifest to declare a receiver for implicit broadcasts (broadcasts that do not target your app specifically), except for a few implicit broadcasts that are exempted from that restriction. In most cases, you can use scheduled jobs instead.
So what I believe is you should be receiving all broadcasts as long as you target API level less than 26 regardless of your app being active or not. Moving forward to API 26 or greater, you will receive broadcast regardless only if it is explicit OR exempted broadcast as per above quote.
What you encountered is probably that device specific issue which was not supposed to happen. Try testing in few more devices to rule this out.
You may want to read this for more clarification.
p.s. try not using error logging for info/debug like Log.e("onReceive ","Called");

AnalyticsReceiver - what is that for? And why it causes 'Receiver does not require permission' warning?

In my Android application in manifest file i have following receiver and service declaration:
<receiver
android:name="com.google.android.gms.analytics.AnalyticsReceiver"
android:enabled="true">
<intent-filter>
<action android:name="com.google.android.gms.analytics.ANALYTICS_DISPATCH" />
</intent-filter>
</receiver>
<service
android:name="com.google.android.gms.analytics.AnalyticsService"
android:enabled="true"
android:exported="false" />
To be honest it's quite old app and I don't remember why I've put that there. It was probably taken from Google Analytics docs. But now I can't find any up-to-date information about it.
What is more Android Studio shows me warning with that receiver:
Receiver does not require permission
Exported receivers (receivers
which either set exported=true or contain an intent-filter and do not
specify exported=false) should define a permission that an entity must
have in order to launch the receiver or bind to it. Without this, any
application can use this receiver.
Do I really need that receiver and service in my code? What is this responsible for? Is it still actual?
If you are using the latest version of Google Analytics, no, you do not need to manually specify the service and receiver in your manifest file.
Here is the Google Analytics getting start guide for Android. Note that if you are upgrading from a significantly older version, you may have additional work to perform elsewhere to upgrade. You should read through the entire guide to ensure that your app is still configured properly.

ACTION_USER_PRESENT in manifest with BroadcastReceiver

There seems to be different opinions on whether it is possible to catch the ACTION_USER_PRESENT screen unlock through the manifest.
This thread implies no it can't be done:
Android Broadcast Receiver Not Working
This thread implies yes it can be done:
Broadcast Receiver for ACTION_USER_PRESENT,ACTION_SCREEN_ON,ACTION_BOOT_COMPLETED
I'm not able to get the event working with either a 2.3.3 or 3.2 emulator.
Does anyone else have recent experience with this? And perhaps a code sample to share?
Use a receiver:
public class Receive extends BroadcastReceiver {
if (intent.getAction() != null) {
if
( intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
Intent s = new Intent(context, MainActivity.class);
s.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(s);
}}
And in your manifest:
<receiver
android:name=".Receive"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.USER_PRESENT"/>
</intent-filter>
</receiver>
The official document says:
Beginning with Android 8.0 (API level 26), the system imposes
additional restrictions on manifest-declared receivers.
If your app targets Android 8.0 or higher, you cannot use the manifest
to declare a receiver for most implicit broadcasts (broadcasts that
don't target your app specifically). You can still use a
context-registered receiver when the user is actively using your app.
so only some exception can receive implicit, manifest-defined events.
Short answer:
so it's not possible anymore to declare it in the manifest. but it's available by context-registering.

Categories

Resources