Android Services is something that is pretty much similar to your windows background services. It is something that will be running in the background that requires an minimal amount of memory usage. The following would be a simple demo that I had created to help you to understand it better.
Shows a notification appearing on the top menu bar | Shows the main notification. |
Time for some crazy source codes...
SimpleServicesActivity.java
package zcs.simpleServices; import android.app.Activity; import android.content.Intent; import android.os.Bundle; public class SimpleServicesActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //Set the layout of this App setContentView(R.layout.main); //Send out this broadcast to check for our //BroadcastReceiver now. Intent intent = new Intent ("zcs.simpleServices.BroadcastFromApp"); sendBroadcast(intent); } }
MainReceiver.java
package zcs.simpleServices.broadcaster; import zcs.simpleServices.service.zcsSimpleService; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class MainReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Intent service = new Intent(context, zcsSimpleService.class); //Run the service 'zcsSimpleService' now. context.startService(service); } }
zcsSimpleService.java
package zcs.simpleServices.service; import java.util.Date; import zcs.simpleServices.SimpleServicesActivity; import android.R; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.content.Intent; import android.os.IBinder; public class zcsSimpleService extends Service { private static final int HELLO_ID = 1; @Override public int onStartCommand(Intent intent, int flags, int startId) { Date now = new Date(); //If the number of minutes of the current time ends //with a multiple of 5 if(now.getMinutes() % 5 == 0) { //Create an instance of NotificationManager NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); //Set a simple icon for the notification that appears //on the top thin bar of a Android phone int icon = R.drawable.alert_light_frame; //And a simple message CharSequence tickerText = "Message from ZCSSimpleServices"; long when = System.currentTimeMillis(); //Create an instance of Notification and assign the //values in to above to it Notification notification = new Notification(icon, tickerText, when); //The subject of your message CharSequence contentTitle = "Hello there..."; now.setSeconds(0); //The body of your message CharSequence contentText = "The time now is " + now.toGMTString(); //What should this notification do upon clicking Intent notificationIntent = new Intent(this, SimpleServicesActivity.class); //Giving NotificationManager the rights to run your actions PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); //populating notification will all the other info //Note: this is what you will be getting when you //drag the top menu bar to the bottom of the screen. notification.setLatestEventInfo(this, contentTitle, contentText, pendingIntent); notification.flags = Notification.FLAG_AUTO_CANCEL; //Show the notification now. notificationManager.notify(HELLO_ID, notification); } return Service.START_NOT_STICKY; } @Override public IBinder onBind(Intent intent) { return null; } }
FirstRunReceiver.java
package zcs.utility.receiver; import zcs.simpleServices.broadcaster.MainReceiver; import android.app.AlarmManager; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class FirstRunReceiver extends BroadcastReceiver { // Restart service every minute private static final long REPEAT_TIME = 1000 * 60; @Override public void onReceive(Context context, Intent intent) { //Create a instance of AlarmManager, which is used //to make continuous action every REPEAT_TIME or minute AlarmManager service = (AlarmManager) context .getSystemService(Context.ALARM_SERVICE); //The class that will be executed after each interval Intent i = new Intent(context, MainReceiver.class); //Assigning rights to AlarmManager to run the actions PendingIntent pending = PendingIntent.getBroadcast( context, 0, i, PendingIntent.FLAG_CANCEL_CURRENT); //Fetch every minute and InexactRepeating allows //Android to optimize the energy consumption service.setInexactRepeating(AlarmManager.RTC_WAKEUP, 0, REPEAT_TIME, pending); } }
StartupReceiver.java
package zcs.utility.receiver; import java.util.Date; import java.util.Calendar; import zcs.simpleServices.broadcaster.MainReceiver; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; import android.app.AlarmManager; import android.app.PendingIntent; public class StartupReceiver extends BroadcastReceiver { private boolean hasRun = false; @Override public void onReceive(Context context, Intent intent) { //Validate whether a similar AlarmManager has //been created boolean alarmUp = (PendingIntent.getBroadcast(context, 0, new Intent(context, MainReceiver.class), PendingIntent.FLAG_NO_CREATE) != null); if (alarmUp) { } else { //If the AlarmManager have not been created, //we will need to create it. //Create a instance of AlarmManager, which is used //to make an action after some time AlarmManager Service = (AlarmManager) context. getSystemService(Context.ALARM_SERVICE); //The class that will be executed after each interval Intent i = new Intent(context, FirstRunReceiver.class); //Assigning rights to AlarmManager to run the actions PendingIntent pending = PendingIntent.getBroadcast( context, 0, i, PendingIntent.FLAG_CANCEL_CURRENT); //Some crazy calculation to find out how far are //we to the next minute. Date now = new Date(); int lapse = 0; lapse = 60 - now.getSeconds() + 1; //As this BroadcastReceiver will run own its own //after the phone boot up successfully, we will want //to wait more than 30 sec before we run the //action of the Alarm Manager //This is done so that the continuous action of //FirstRunReceiver will be executed when we //reaches 60 seconds. if(lapse < 30) { lapse += 60; } Calendar cal = Calendar.getInstance(); cal.add(Calendar.SECOND, lapse); //We will only be executing this action once //only. Service.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pending); hasRun = true; } } }
AndroidManifest.xml
* Click here for the source files.
No comments:
Post a Comment