How To Use A Broadcastreceiver In Web Services
Our website is made possible by displaying online advertisements to our visitors. Please consider supporting usa by disabling your ad blocker.
Use A Broadcast Receiver For Groundwork Services In Android
As an Android developer, you'll often run into the scenario where you lot demand to perform tasks and display notifications for your app in the background.
To retain bombardment power on our users device we are going to run background tasks using a broadcast receiver. This will prevent a thread from constantly running in the background draining the bombardment quickly over time. Instead, the task will be run on defined intervals of the alarm.
packet com . nraboy . instance ; import android . app . * ; import android . content . * ; import android . os . * ; public class AlarmReceiver extends BroadcastReceiver { @ Override public void onReceive ( Context context , Intent intent ) { Intent background = new Intent ( context , BackgroundService . class ); context . startService ( background ); } }
The to a higher place code is our broadcast receiver. Every time it is called, our background service is executed.
So how practice we enable our broadcast receiver to work on Android's alert service? That is easy, and can exist seen in the following chunk of code:
parcel com . nraboy . example ; import android . app . Activity ; import android . bone . * ; import android . content . * ; import android . app . * ; public class ExampleActivity extends Activity { private Context context ; @ Override public void onCreate ( Bundle savedInstanceState ) { super . onCreate ( savedInstanceState ); setContentView ( R . layout . master ); this . context = this ; Intent alarm = new Intent ( this . context , AlarmReceiver . form ); boolean alarmRunning = ( PendingIntent . getBroadcast ( this . context , 0 , warning , PendingIntent . FLAG_NO_CREATE ) != goose egg ); if ( alarmRunning == fake ) { PendingIntent pendingIntent = PendingIntent . getBroadcast ( this . context , 0 , alarm , 0 ); AlarmManager alarmManager = ( AlarmManager ) getSystemService ( Context . ALARM_SERVICE ); alarmManager . setRepeating ( AlarmManager . ELAPSED_REALTIME_WAKEUP , SystemClock . elapsedRealtime (), 1800000 , pendingIntent ); } } }
The above lawmaking will prepare our circulate receiver and then cheque to see if it is already running. If it is already running, do naught because we don't want to register it multiple times. If the receiver is not already running, nosotros volition register information technology to repeat every 1800000 milliseconds, or every thirty minutes.
And so what exactly is the broadcast receiver doing when information technology wakes up? It is just calling our background service intent where nosotros exercise all of our heavy lifting.
package com . nraboy . example ; import android . app . * ; import android . content . * ; import android . os . * ; public class BackgroundService extends Service { private boolean isRunning ; private Context context ; @ Override public IBinder onBind ( Intent intent ) { return null ; } @ Override public void onCreate () { this . context = this ; this . isRunning = imitation ; this . backgroundThread = new Thread ( myTask ); } private Runnable myTask = new Runnable () { public void run () { // Do something here stopSelf (); } }; @ Override public void onDestroy () { this . isRunning = false ; } @ Override public int onStartCommand ( Intent intent , int flags , int startId ) { if (! this . isRunning ) { this . isRunning = true ; this . backgroundThread . start (); } return START_STICKY ; } }
You can come across in the to a higher place code that all of our variables get initialized in the onCreate
and the service starts to run in the onStartCommand
method. To prevent the users device from blowing up, all service work is going to be done in a thread. When the thread is complete, the service volition exist stopped until the circulate receiver fires it up over again. You lot'll likewise discover that the service thread will non re-burn if the service is already running. We wouldn't want threads to get jammed up and cease up with 100 running in the background. Your users wouldn't be too pleased with this.
To tie everything together, you need to add the circulate receiver and background service to your AndroidManifest.xml file. The following lines must be added to your file:
<application> <service android:name=".BackgroundService" /> <receiver android:name="AlarmReceiver"></receiver> </application>
And so why might you desire to utilise a circulate receiver and background service? Well, if you ever want to brandish notifications to your user based on events or calculations yous're going to need to do this from the background. You can for example have your background service execute a RESTful asking to a web service and display a notification depending on the response. Or maybe only update your apps database in the groundwork and so users don't need to wait when they open their application. There are many different possibilities.
A video version of this article can be seen below.
Nic Raboy
Nic Raboy is an advocate of modern spider web and mobile development technologies. He has feel in Java, JavaScript, Golang and a diversity of frameworks such equally Angular, NativeScript, and Apache Cordova. Nic writes most his development experiences related to making web and mobile development easier to empathize.
How To Use A Broadcastreceiver In Web Services,
Source: https://www.thepolyglotdeveloper.com/2014/10/use-broadcast-receiver-background-services-android/
Posted by: mccoysuchown.blogspot.com
0 Response to "How To Use A Broadcastreceiver In Web Services"
Post a Comment