how to run Boot Broadcast Receiver on android 11 - android

i am actually working on alarm app that set alarm ans store alarm data into room database,i want to Re Schedule my alarms after the phone reboot.
all work fine but its not working on android 11.In android 11 the boot reciver not trigger and in other devices boot receiver trigger and set all the alarm stored in database
thanks
i am using this code
override fun onReceive(context: Context?, intent: Intent?) {
if (Intent.ACTION_BOOT_COMPLETED == intent?.action)
{
}
}

I hope you have set the below permission
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
and added the intent filter for your receiver like below
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
If the above things are in place. Then try the below things as well
Click on the app after installing your app as until your app is enabled you won't be able to receive BOOT COMPLETE action
Check the condition like below
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction()))

In Android 11 you need to open your app after restarting to receive Boot_Complete_receiver

Related

BOOT_COMPLETED Broadcast not received when app was killed

I'm stuck at the development of our app. I want to receive the BOOT_COMPLETED broadcast so I can reschedule some Alarms. So I start my app, schedule the alarms and then reboot my device (Android 10). Everything works as expected, ie after reboot, my BroadcastReceiver is started and my alarms are rescheduled.
But if I stop my app before reboot (ie not just put it in background, really kill it), it doesn't receive the BOOT_COMPLETED broadcast anymore ...
I have the respective permissions in my manifest
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
I have the receiver in my manifest enabled with the respective actions
<receiver
android:name=".receivers.CustomBroadcastReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED"
android:enabled="true"
android:exported="true"
android:directBootAware="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
And this is my CustomBroadcastReceiver class
class CustomBroadcastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
Log.d(javaClass.simpleName, "onReceive ${intent.action}")
... //reschedule alarms read from SQLite
}
}
I can see the respective Log message after reboot, when the app was running before reboot. But I don't see the log, when the app was killed before the reboot ... Anything I'm missing here?
Yes, I'm aware, there are many other questions regarding BOOT_COMPLETED but they all more or less say, do it like shown above, then it will work. And, well, I know it in principle does work, because I see the receiver being called. But just not, when the app was once killed. If I start the app manually, put it in background and reboot the device, it works as expected ...
Use JobScheduler instead of alarms for this. JobScheduler jobs can be set as persistent across boot, so you don't have to do the bootup wiring yourself.

Local notifications not showing up when device is rebooted (or similar state) Android

I'm a bit new so I have a simple question, I have created a Xamarin.Android app and I have used local notifications with alarm manager, broadcast receivers, etc to schedule them, the problem that I have is that my scheduled notifications are not being showed when the device is rebooted, or completely off state and turn on again. My questions are:
Is that a problem inside Android?
otherwise
How can I solve it?
I hope help, thanks
By default, all alarms are canceled when a device shuts down.
https://developer.android.com/training/scheduling/alarms
By default, all alarms are canceled when a device shuts down. To prevent this from happening, you can design your application to automatically restart a repeating alarm if the user reboots the device. This ensures that the AlarmManager will continue doing its task without the user needing to manually restart the alarm.
You need to monitor for BOOT_COMPLETE and re-set your alarms.
https://developer.android.com/reference/android/Manifest.permission.html#RECEIVE_BOOT_COMPLETED
Manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<receiver android:name=".SampleBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
Receiver:
class SampleBootReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == "android.intent.action.BOOT_COMPLETED") {
// Re-set the alarm here.
}
}
}

Auto Start Service after booting device even app not opened atonce. Android

I am making a system app. In that I have a requirement is to run a service after boot load WITHOUT A SINGLE TIME LUNCHING THE APP.
this question is bit similar to this
System App auto starting
But it does not have any appropriate solution.
Also read that BOOT_COMPLETE_RECEIVER works only when app launched at once.
Use Broadcast Receiver for getting action after that start service from that broad cast receiver and use START_STICKY service so that if it is killed because of some priority than it's recreate and if you want to continuously run this service in background than WAKE_Lock that service and using Alarm Manager check it is runnig or not.
Set this in manifest
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver
android:name="AutoStart"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
AutoStart class
public class AutoStart extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
// Start your service here..
}
}
}
Thanks all for your effort, I finally got answer.
Solution:
As I stated my app is system app, System work even they not opened at once. Because they are not in stopped state i.e enforce after android 3.1.
Secondly If a user app wants this then Its manifest don't have any "android.intent.category.LAUNCHER" category in activity.
Also by adb you can enable your app by using this command
adb shell am broadcast -a com.example.demo.action.LAUNCH --include-stopped-packages (This is not tested)
Some good link to this:
http://droidyue.com/blog/2014/01/04/package-stop-state-since-android-3-dot-1/
Static BroadcastReceiver not Working after Installation from ADB

Launch activity by dialing a number

I've created an appropriate BoradcastReceiver, registered it in Manifest.xml and here is my problem: if my application has already been launched and hanging in background, then dialing a number would bring it to front. If it has not been launched then dialing a number would have no effect.
How can I fix this? I test this on Xiaomi Mi4 with MIUI6 if that's important.
Here's the code (I use Scala):
manifest.xml:
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
...
<receiver android:name="DialerGate" android:enabled="true" android:exported="true">
<intent-filter android:priority="1">
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
</intent-filter>
</receiver>
BroadcastReceiver:
class DialerGate extends BroadcastReceiver {
def onReceive(context: Context, intent: Intent) =
if (intent.getAction equals Intent.ACTION_NEW_OUTGOING_CALL) {
val phoneno = intent.getExtras getString Intent.EXTRA_PHONE_NUMBER
val prefs = context.getSharedPreferences("prefs", Context.MODE_PRIVATE)
val number = prefs.getString(AbstractKit.LAUNCH_NUMBER, null)
Log.d("WALLET-PHONE", s"Dialed number: $phoneno, saved number: $number")
Log.d("WALLET-PHONE-OK", (number == phoneno).toString)
val i = new Intent
i.setClassName("com.app.wallet", "com.app.wallet.MainActivity")
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP)
val appContext = context.getApplicationContext
appContext.startActivity(i)
//if (number == phoneno) context startActivity new Intent(context, target)
//context stopService intent
}
}
From a simple user perspective, that cannot be done (its a security feature).
Starting from HONEYCOMB Android doesn't allow any broadcast receivers to be invoked until application is run at least once.
Its basically simpler to allow the program to be run at least once (during boot its the most common one), and then have the intent close the app if its not the time to use it.
Check this for further details on how to implement additional receivers that may do what you need it to do.
create a Listener in your Broadcast Receiver and listen to ON_BOOT_COMPLETED, then start your app, in silent mood and you will be resolved to your normal workings.
side note If Activities was to be waken up that way, then Keylogging apps and hacking apps will be very very very cheap to create - hence make android vulnerable.
http://android-developers.blogspot.in/2013/05/handling-phone-call-requests-right-way.html
Listening for outgoing call requests
Apps that provide phone calling services (such as VOIP or number management) can set up Intent filters to handle outgoing call requests, such as those made from the Dialer or other installed apps. This provides a seamless integration for the user, who can transition directly to the calling service without having to redial or launch another app.
When the user initiates a call, the system notifies interested apps by sending an ordered broadcast of the NEW_OUTGOING_CALL Intent, attaching the original phone number, URI, and other information as extras. This gives apps such as Google Voice and others a chance to modify, reroute, or cancel the call before it’s passed to the system’s default phone app.
If you want your phone calling app to be able to handle outgoing call requests, implement a broadcast receiver that receives the NEW_OUTGOING_CALL Intent, processes the number, and initiates a call as needed. Make sure to declare an intent filter for NEW_OUTGOING_CALL in the receiver, to let the system know that your app is interested in the broadcast. You’ll also need to request the PROCESS_OUTGOING_CALLS permission in order to receive the Intent.
Note that the system broadcasts NEW_OUTGOING_CALL only for numbers that are not associated with core dialing capabilities such as emergency numbers. This means that NEW_OUTGOING_CALL can not interfere with access to emergency services the way your use of CALL_PRIVILEGED might.
Here’s an example broadcast receiver declared in an app’s manifest file:
<manifest>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<application>
...
<receiver android:name=MyOutgoingCallHandler">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
...
</application>
</manifest>
And I am sure that <category android:name="android.intent.category.DEFAULT" /> will do the trick for you. check this question too for more details about category tag.here
You could try a few things.
Try using java, if not try the following.
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
...
<receiver android:name="DialerGate">
<intent-filter android:priority="2147483648">
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
</intent-filter>
</receiver>
Changed priority and removed unnecessary stuff.
Also though I am good at Broadcast Receivers I don't have experience in Scala, so I can only suggest a few ideas. Remove the if statement. It is not required as you have already have an <intent-filter>. Also change the intent as in the paste bin code.
Have a look here.
You may use a service, but just care about one thing: when the app is closed the service get closed also because they are in a one thread, so the service should be on another thread in order fot it not to be closed
You can keep it alive with AlarmManager.
In the link there are also some samples :)
Hope it helps!
The application might not have the permissions to the "phone", either ask for permissions at runtime or go to application settings and enable all the permissions asked by the application.
This worked for me..

Android: Fire all missed alarms when device turns on

In my application the user has set some reminders and I have to alarm them when the time has come. Currently I'm using AlarmManager.RTC as a type of AlarmManager
am.set(AlarmManager.RTC_WAKEUP, date.getTimeInMillis(), pendingIntent);
it in working as per instruction
// Sets an alarm - note this alarm will be lost if the phone is turned
// off and on again
is there any way the I can fire missed alarms when I turn on the device?
Please note that I don't want to wakeup the device, I just wanted to remind them when they turn on the device.
PS: I have read the documentation http://developer.android.com/reference/android/app/AlarmManager.html#RTC, but I didn't find my option.
is there any way the I can fire missed alarms when I turn on the device?
Step #1: Have some way of tracking, yourself, what is missed, such as by tracking event status in a database.
Step #2: Set up a BOOT_COMPLETED BroadcastReceiver to find when the phone is rebooted. In there, schedule any alarm(s) that are still in the future, and decide what to do about alarms that occurred in the past but were missed (e.g., raise a Notification pointing out the missed events).
You would have to do this manually.
One way to do this would be to listen for the phone shutdown event:
<action android:name="android.intent.action.ACTION_SHUTDOWN" />
and before the device shuts down, save the shutdown time.
Then listen for device bootup:
<action android:name="android.intent.action.BOOT_COMPLETED" />
and write your own logic that determines which alarms would have fired during that downtime, and fire those alarms immediately with AlarmManager.
AlarmManager will alarm all of your alarms till your phone is on.
Registered alarms are retained while the device is asleep (and can
optionally wake the device up if they go off during that time), but
will be cleared if it is turned off and rebooted.
Now that only way fits is to kepp tracks through database.
Put an additional class that extends BroadcastReceiver. Where in its OnReceive() method you can re-generate your alarms when device reboots. (obviously if you have some track, i.e database).
Setting BroadcastReceiver for your purpose can be seen here How to start an Application on startup?.
Hope this helps.
Register broadcastReceiver in your Manifest.xml
<receiver
android:name=".AlarmRestartReceiver"
android:enabled="true">
<intent-filter>
<action
android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
After this, listen event when device wake up.
override fun onReceive(context: Context, intent: Intent?) {
if (intent?.action == "android.intent.action.BOOT_COMPLETED") {
// re-register all alarm here..
}
}

Categories

Resources