I am working on alarms and making an app for task reminder.
I am showing notification at scheduled time by user.
But when alarm rings , notification appears . Then I switched off my phone , and again when I switched , on the notification get disappeared(doesnt show the notification).
while in the case of SMS notifiaction(default android implementation) it does not get disappeared until we drag the notification.
I want the same as like sms notification.
What should I do?
Notifications are not persistent across device boots. If they reappear for certain apps, it's because the app is starting on boot and re-creating them.
You should define a receiver that determines if the notification should be present and creates it if necessary, and start this receiver on device boot.
Set its intent filter for,
android.intent.action.BOOT_COMPLETED
and use permission,
android.permission.RECEIVE_BOOT_COMPLETED
and make sure your receiver has permission,
android.permission.RECEIVE_BOOT_COMPLETED
Like this,
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
...
<receiver ... android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
Related
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.
In my app whenever we get the message, need to display one pop up with pre filled message. For this i used the following code in manifest file
<receiver
android:name="com.cte.broadcast.SMS_Receive_BroadCast"
android:enabled="false">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
And whenever app is login, register the broadcast receiver by using the following code
ComponentName component = new ComponentName(getApplicationContext(),
SMS_Receive_BroadCast.class);
getApplicationContext().getPackageManager().setComponentEnabledSetting(component,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
And whenever app is login unregister the broadcast receiver by using the following code
ComponentName component_sms = new ComponentName(context.getApplicationContext(),
SMS_Receive_BroadCast.class);
getApplicationContext().getPackageManager().setComponentEnabledSetting(component_sms,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
so here everything is working fine. when ever we logged in and whenever we get the message broadcast receiver trigger and getting pop up even app is closed. and logged out not getting trigger broadcast receiver and not getting pop up. so up to this everything fine. But the problem is after one day whenever we get the message the broadcast receiver wont trigger even it is logged in. But whenever we open the app and close it then it will working fine. so the problem is after some time broadcast receiver is automatically unregisterd i think..
So how to resolve this problem..Thanks In Advance..
Some applications abort your Broadcast on the intent which will prevent other applications from receiving the intent.
The solution is to increase the android:priority attribute in the intent-filter tag:
<intent-filter android:priority="priority value">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
I had a similar trouble. I dont know exactly why, but depending on you put your registerBroadcast, android will kill that instance, it´s something related to processes execution stack on linux.
What i did to solve the problem: create a background service instead of a BroadcastReceiver, there you register your BR and it will work fine, several days =))) !
I was expecting my app to load the BroadcastReceiver AutoStartOnBoot when I reboot my device.
I uninstall and install the app. Which means that all existing settings are deleted. I then power down the phone. And power it back up, the Broadcast receiver is never called.
I now, power down the device one more time and power it up again. Yet, broadcast receiver is not called.
I now launch the app once. Clear data. And power it down. I power it up. Now, the broadcast receiver is called.
Manifest
<receiver
android:name=".AutoStartOnBoot"
android:enabled="true"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
I have the permission setup
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Edit:
From your First two Points
1.I uninstall and install the app. Which means that all existing settings are deleted. I then power down the phone. And power it back up, the Broadcast receiver is never called.
2.I now, power down the device one more time and power it up again. Yet, broadcast receiver is not called.
Why it is not working??
Here your just installed the app but not launched.In android after first launch only all your manifest data is registered and all receivers work.But in your third case its working because you are launched the app so here all receivers gets registered.
For more check here Broadcast Receiver not working immediately after package installation
You have to add this permission outside of the <application> tag in manifest file
instead of this
<android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
Add this like
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
The behavior that you describe is perfectly normal. Something needs to use an explicit Intent to start up one of your components before your manifest-registered receivers will work. Typically, that will be the user tapping on your home screen launcher icon.
See the Android 3.1 release notes for more about this.
All answers are correct. This behavior is as per expectation. The app is inactive when installed, until its manually launched by the owner. Only after that is the BOOT_COMPLETED broadcast receiver registered to the OS.
Unless we put the app in the system folder, which keeps all apps in active state. We are device company, adb push your.apk /system/app is possible for us.
Some interesting links, here and here
Can I schedule alarm manager (.set() methode) in android to a time which is one month later the current time
will it be alive for that mutch of time ?
what will be the OS effects on this alarmManager ?
Tip for a case when user reboots his phone. You can declare BroadcastReceiver with the following intent-filter:
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
and re-schedule your alarm in onReceive(...) of your receiver class. Intent will be delivered to the receiver after user reboots his phone. Don't forget to add the following permission to your manifest file:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Or you can use the library that will handle such cases for you:
https://github.com/commonsguy/cwac-wakeful
Theoretically yes, If the user keeps his phone on for that long, however the alarm doesn't survive reboots and will therefore not be scheduled when the phone restarts.
In my application, there's a feature that allows users to dial a specific number and brings up an activity to front. I have the following receiver, and the only receiver registered in AndroidManifest.xml.
<receiver android:name="com.example.myapp.OutgoingCallListener" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
Please note there's no BOOT_COMPLETED intent or service.
Now here's the thing I couldn't figure out. When I reboot my device, go check the Running Apps, my application is not listed there. But, if I dial the specific number, my application starts and the activity is brought to front.
My question is: If the app is not a service, and not started on boot, how could it recieve intent from Android? That is, in my case, how could my app listen to NEW_OUTGOING_CALL while it's not started at all?
A BroadcastReceiver that is registered in the manifest is always capable of responding to a matching broadcast. If your process is not running for any reason, Android will start up a process for you.