BroadCastReceiver | android Broadcast Receiver examples
kandi X-RAY | BroadCastReceiver Summary
kandi X-RAY | BroadCastReceiver Summary
android Broadcast Receiver examples
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Sends a notification .
- Set the status .
- Set the alarm .
- Called when a notification is received .
- Initializes the main fragment .
- On createView .
- Process work .
- This method is called when an intent is executed .
BroadCastReceiver Key Features
BroadCastReceiver Examples and Code Snippets
Community Discussions
Trending Discussions on BroadCastReceiver
QUESTION
Tested on Pixel 3a (both emulator and physical device (my used device)) API 30 (11.0).
Repo: https://github.com/amdreallyfast/AndroidStartOnBootExample
Goal: I'm trying to launch an android app on startup, but failing.
Note: I am well aware that this should not be done for apps designed for the general public. This is not that. At the moment, this is just concept exploration for a personal project. The larger goal is to tinker with a used Pixel 3a and turn it into a home utility device. I don't want to start the app manually every time I need to turn it on and would rather have it starting automatically, so I'm trying to find a way to launch the app at startup.
Also Note: Because this is a project to start on boot, I can't use the debugger for most of this and have to rely on notifications instead to detect progress.
Progress: I've got a BroadcastReceiver that responds to the BOOT_COMPLETED intent by launching a Service. The service's onCreate(...) function creates a simple intent to start another app (at the moment, just Google Maps, which is readily available without additional software installation).
I've also got MainActivity, a simple program that has a button that uses an intent to launch the same Service. I use this to compare behavior between starting the service at startup and starting the service from an already-running activity.
Already tried setting the intent flag Intent.FLAG_ACTIVITY_NEW_TASK
.
Problem: Google Maps does not launch from the service when called during startup. I know that the service's code is correctly set up to launch the map intent because launching the MainActivity and pressing the button will launch the service and then start Google Maps just fine. I also know that the code running on startup got through to the point where it launched the map intent because notifications indicate as such.
The only difference that I'm noticing between not working and working seems to be the manner in which the service is started.
Documentation: I found this android docs page: https://developer.android.com/guide/components/activities/background-starts. It says (in part):
Apps running on Android 10 or higher can start activities only when one or more of the following conditions are met:
- The app has a visible window, such as an activity in the foreground.
Why doesn't this start? Am I misunderstanding this? Google Maps most certainly has a visible window, so I am expecting to be able to start it.
Note: Again, I'm not planning on releasing my app to the general public. This is just for me.
...ANSWER
Answered 2022-Apr-16 at 23:26The larger goal is to tinker with a used Pixel 3a and turn it into a home utility device
Write a launcher app (i.e., have your activity respond to ACTION_VIEW
/CATEGORY_HOME
) and set it as your default launcher. It will automatically start when the phone starts, just as your current default launcher does. And, you can put a button or something in your own launcher that launches the real launcher via an explicit Intent
.
This would allow you to skip the on-boot receiver and the service.
Why doesn't this start?
You do not qualify for a background activity start.
Google Maps most certainly has a visible window, so I am expecting to be able to start it.
First, presumably it does not have a visible window right after boot.
Second, you are not writing the Google Maps app, and Google Maps is not trying to start an activity from the background. You are writing your own app, and your own app is trying to start an activity from the background. And, since your own app does not have a visible window right after boot, it does not qualify for a background activity start under the "app has a visible window" rule.
QUESTION
I'm have some project, realtime sending RSSI and MAC of Bluetooth Low Energy. I want try using http protocol, and i found OkHttp library. But when i run with OkHttp, my software crash.
My error
...ANSWER
Answered 2022-Apr-02 at 04:40The error is caused due to execution of network request on main thread (a.k.a. UI thread). In general, thread blocking operations (like file read, network requests) are prohibited on main thread. This is done to avoid App Not responding
error.
In your mainActivity.java
, since BroadcastReceiver#onReceive
is executed on main thread. (Ref: https://stackoverflow.com/a/5676888/5309486 ). And inside this onReceive
function, you are trying to make the network request.
Just, use handler and post the network call to background thread (or any other thread) and that will fix your issue.
QUESTION
I'm developing an app where users can create routines and set reminders for these routines.
I use alarms scheduled by AlarmManager and a BroadcastReceiver to call a foregroud service that sends a notification by using the notification manager. Here's some code:
...ANSWER
Answered 2022-Mar-31 at 07:49You should check the specifications of the device that is laggy and monitor RAM and CPU usage of the device, before, after and during your app runtime, speatially the RAM usage, maybe Android 30 is putting too much pressure on device and that is causing the issue.
In General the issue that are seen on some devices and not on another are caused ether by the difference between Android Versions and their resource handling methods or bottleneck between Hardware and Software on same OS version on different devices.
You can monitor device resource usage ether by Android Studio's own resource monitor tool "Android Profiler" or third party Resource Monitoring Apps found on Google Play.
If there is a process or function in your App that causing the resource leak you can fix it easily by detecting it from android profiler but if the issue is caused by OS Resource handling or Hardware and Software bottleneck you should skip that device.
QUESTION
I am showing a custom notification (FCM). custom notification has two buttons approve
and deny
.
I have managed to create pending intent and a broadcast receiver and now I know in onReceive
when the button is clicked.
How can i differentiate between button clicks in onReceive, I tried to pass extras on the intent, but intent.getStringExtra("clicked")
gave me null value.
what is the right way of knowing which button is clicked approve
, deny
This is the code I tried. Thanks for your help in advance R
...ANSWER
Answered 2022-Mar-26 at 14:36You need to use two distinct, up-to-date PendingIntent
objects, wrapped around different Intent
objects (e.g., ones with differing extras).
For "distinct", you need the IDs of the PendingIntent
objects to be different. The ID is the second parameter to the PendingIntent.getBroadcast()
call.
For "up-to-date", you need to update any existing PendingIntent
that your code might have created previously. For that, pass PendingIntent.FLAG_UPDATE_CURRENT
as the fourth parameter to the PendingIntent.getBroadcast()
call.
QUESTION
Having android sdk which intercept the push notification, and has been using notification trampoline to further open the end activity. In the case of deeplink the app who uses this sdk will open the configured deeplink handler activity.
Snippet for the trampoline
:
ANSWER
Answered 2022-Mar-21 at 12:26You are better off launching an Activity
directly from the Notification
. The Activity
could then do the analytics and figure out what Activity
needs to be launched and then delegate to that. As an alternative, the launched Activity
could send the broadcast Intent
to your BroadcastReceiver
which could do the work.
NOTE: If you launch an Activity
directly from a Notification
, that is not a trampoline. The trampoline occurs when you launch a Service
or a BroadcastReceiver
directly from the Notification
and that component then launches an Activity
. The idea is that the user needs to be in control of what pops onto his screen. If he taps a notification, he expects that something will appear on his screen, if your Service
launches an Activity
, that can happen at any time and could possibly interrupt him.
QUESTION
targetSdkVersion: 30
In our App we have a feature, where we download files (mostly pdf) to the public download folder and start an intent afterwards to open it. Our code works fine for android apps with api >= 28 and >= 30. Just our app on Android 10 (sdkVersion 29) will try to open the document and instantly closes the activity that tried to display the pdf. The logcat shows following error:
...ANSWER
Answered 2022-Mar-17 at 13:44You should not use FileProvider to obtain an uri for your file.
You can get an uri from DownloadManager and use it to serve your file.
Code would be the same for all Android versions.
Not a single permission needed.
QUESTION
It seems there are a few questions related to the subject topic but I haven't found a clear yes/no answer to this.
I have a foreground service that calls setExactAndAllowWhileIdle
to start a BroadcastService. Below is the code in the Broadcast Receiver.
ANSWER
Answered 2022-Mar-21 at 04:37Yes, Doze will ignore your wakelock. However with setExactAndAllowWhile Idle you will be worken up at the correct time, and you'll have that 10s window to do any processing you wish.
QUESTION
I went down a rabbit hole of trying to make my service work and I believe I have just made an amalgamation. I am trying to make a step counter service, it uses a handler which has a step counter sensor implemented onto it. Every 6 hours i want to update my server with the step count inforrmation, reset the step count near to the end of the day and set another alarm on my handler to do the same action every 6 hours. The handler also routinely sends data to an activity to read the number of steps its recorded.
However, everything works but I am unable to get an instance of my handler in my broadcast receiver. So another alarm is never set and the steps are never reset. Can anyone help me with this?
To be specific, the first line of code in the broadcast receiver (StepCounterHandler handler = StepCounterHandler.getInstance();
) will always return null from my testing.
This is my code:
Android Manifest.xml:
...ANSWER
Answered 2022-Mar-13 at 00:23If the zero-parameter getInstance()
is returning null
, you have no singleton.
You have two processes. The StepCounterService
is in the :stepCounterService
process. StepCountUpdaterAlarm
is in the default process. These processes share no objects, and so they will have separate StepCounterHandler
singleton instances. If you have not done something previously to set up the StepCounterHandler
in the default process, it will be null
.
If you are expecting to share the singleton between StepCounterService
and StepCountUpdaterAlarm
, they will need to be in the same process. Either move StepCountUpdaterAlarm
into the :stepCounterService
process or move StepCounterService
into the default process.
QUESTION
I am creating a research app that should prompt the user 4 times a day to enter their mood - by sending a notification, which when clicked launches the correct Activity
. I am able to schedule these notifications using AlarmManager
, however only the last scheduled notification ever shows. So although I schedule them for 9AM, 2PM, 5PM, and 8PM, it only ever sends a notification at 8PM.
How can I get all of the scheduled notifications to show?
Here is my code for setting (one of) the alarms (from a notification manager class). Note that al alarms are set using the same instance of AlarmManager
:
ANSWER
Answered 2022-Mar-10 at 17:35Can you try with same id in pending intent and notify.? Notification id in createNotification() method is random id.
QUESTION
My app is pushing a notification at a specific date in the future. In the notification you shall have two options:
- Click the notification body --> open the app normally
- Click the action button in the notification --> open the app and perform an action
In order to do so, I want to add an extra to the intent which can be read when starting the app. Therefore I set up the notification receiver as following:
...ANSWER
Answered 2022-Feb-24 at 22:10You are attempting to create two different PendingIntent
s, but your code actually only creates one PendingIntent.
The first call creates a new PendingIntent
:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install BroadCastReceiver
You can use BroadCastReceiver like any standard Java library. Please include the the jar files in your classpath. You can also use any IDE and you can run and debug the BroadCastReceiver component as you would do with any other Java program. Best practice is to use a build tool that supports dependency management such as Maven or Gradle. For Maven installation, please refer maven.apache.org. For Gradle installation, please refer gradle.org .
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page