Permissions in BroadcastReceiver on Android 6 - java

whats wrong with runtime permissions in broadcast receivers on Android 6?
I have call log broadcast receiver which I defined in AndroidManifest and
it starts when I launch this app. MainActivity contain onCreate method, in this method
I try to check permissions and call permission dialog, but it's too late, because
broadcast receiver is starting before onCreate method and then I don't have permission
to read call log.

Related

Will Application Class OnCreate be called if a Broadcast is recieved

I am using a JobIntentService perform some task after the task is ended I send a Broadcast which is also listened by one of my Activity.
Suppose in a case where the activity is closed by Android OS to free up some memory then if the Broadcast is sent from the JobIntentService will the OnCreate of the Application also be called?
I don't know if answer marked correct is still correct. On a phone running Android P, if you have a manifest declared broadcast receiver and an Application class defined in your <application> node, the onCreate() in your Application class is called before the onReceive in your broadcast receiver. I see no reason to believe other versions of Android would behave differently.
NO. It will never call the applications OnCreate() method (is called only during a cold start).
A cold start refers to an app’s starting from scratch: the system’s process has not, until this start, created the app’s process. Cold starts happen in cases such as your app’s being launched for the first time since the device booted, or since the system killed the app.
About BroadcastReceiver Lifecycle:
If your receiver is registered in activity then it's lifecycle is the activities lifecycle. So your receiver will not be able to listen once your activity gets destroyed.
If your receiver is registered in application then it's lifecycle will be applications lifecycle and it will be able to listen to the broadcasts as long as the application is not destroyed.
JobIntentService & Receiver
When you start a JobIntentServie (from a receiver) then your job intent service will not get killed by the OS as long as there is active jobs going on (please note that there may be a time limitation, to know more: How long is the "JobService execution time limit" mentioned in Android's JobIntentService docs?).
Now if your activity is destroyed in the meantime then your broadcast receiver will not listen to the broadcast because it's lifecyle has ended, if you don't unregister the receiver yourself, the system will kill it as system considers the BroadcastReceiver to be no longer active.
So if you want to listen to a broadcast as long as your application is not destroyed, you should register your broadcast receiver in applications onCreate() method.

Android BroadcastRecevier for App Permissions

How would you create a broadcast receiver for when there is a change in your app's permissions?
You cannot create a BroadcastReceiver for App permissions since there is no broadcast. As soon as there is a permission change, the app is restarted.
EDIT: You can check for permissions yourselves by following this.

Start Activity on Reboot Only

Basically I am trying to make an activity containing a button which reboots the device and after the reboot returns to the same activity.
I understand that this question may get confused with others about rebooting the device, but that is not the focus of this question as I can get the device to reboot fine.
I have made the button reboot the device but the only way I can get it to start the activity after it's finished rebooting is to register a broadcast receiver for BOOT_COMPLETED in the manifest. The trouble is that this method starts the activity every time the device boots which is undesirable. When I register the receiver on the button click listener it does not start the activity after the reboot.
I was wondering if there might be an extra in BOOT_COMPLETED that I could use to decide if it had been purposefully rebooted.
Any advice would be appreciated, thanks in advance!
Just save an integer corresponding to device purposely being rebooted through your activity. Use SharedPreference for the same. On reboot, in your broadcast receiver, check if the value is set. If it is set, start your activity, otherwise, let it go.
EDIT :
Always, unset this value when reboot is complete and your Activity is in front.
Your XML should be stored in a file named AndroidManifest.xml, not manifest.java.
Another reason your code is not being run, might be that your App is installed on external storage (sdcard). BOOT_COMPLETE is sent to applications before external storage is mounted. So if application is installed to external storage it won't receive BOOT_COMPLETE broadcast message.
If that isn't the problem, there is already a very good description of how to get boot completed receivers working on Android.
Trying to start a service on boot on Android

SMS Broadcast Receiver not working in KitKat

My app needs to receive SMS. So far it seems to work on real devices and emulators from Gingerbread to JellyBean. I'm now testing it in a KitKat emulator, and the Broadcast Receiver is not fired.
I'm registering a the receiver in the manifest using the classic sms action:
<receiver android:name=".receivers.MyReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
I've also added the SMS permission in the manifest:
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
I've read the blog post on SMS changes in KitKat, where they say this intent filter should still work:
Any filters for the SMS_RECEIVED_ACTION broadcast in existing apps will continue to work the same on Android 4.4, but only as an observer of new messages, because unless your app also receives the SMS_DELIVER_ACTION broadcast, you cannot write to the SMS Provider on Android 4.4.
Which is fine because I just need to listen for new messages, and I don't need my app to be the default SMS app nor modify the provider (This is a very simple app with no activities, only a receiver and a service that is called from the receiver).
The emulator is very laggy, so I think it might be a problem with the emulator only. Too bad I don't have any KitKat device to test it. For now I'm testing in KitKat using the DDMS telephony tab. I've added logging to the receiver but I don't see anything in logcat. There are no exceptions nor any other message that could indicate problems. It looks the receiver is not being notified, but on the other hand I've double-checked and the app is installed.
What could be going wrong?
As #Seshu Vinai has pointed in the comments, the problem turns out to be the security restriction introduced in Android 3.1. I thought it was not the case since i had tested it before on JellyBean devices and emulators and the app seemed to work. But it did so only because I had previously launched an activity fron some unit tests, and thus the app was activated.
You can find more info in the following questions:
How to start a Service when .apk is Installed for the first time
Android BroadcastReceiver won't work
Android BroadcastReceiver not working after install
The broadcast receiver will not be called unless your app is the default app in KITKAT. You will be able get the contents directly, but you will not be able to listen to new messages. I had the same problem. I got to know after making my App as default.
Try making your app the default. You will notice the difference.

How to get the content://mms-sms/conversations intent in Android?

Hi I want to know how to specify to a Broadcast Receiver that I want to listen for the content://mms-sms/conversations intent.
In Log cat you can see the Intent fired when you open the messages Application on the emulator or real device. I want to trigger an action when this Intent gets activated.
Thanks in advance for your help.
I want to know how to specify to a Broadcast Receiver that I want to listen for the content://mms-sms/conversations intent.
That is not possible.
In Log cat you can see the Intent fired when you open the messages Application on the emulator or real device.
Correct. However, that is starting an activity. You cannot register a BroadcastReceiver to find out when the user starts any particular activity.

Categories