All of us know, what Alarm does. Alarms will run in the background, whenever it counter the time, plays nice Music. Alarm wake-up us. Every phone has this feature from low end to high end mobiles.
Steps to Create background task :
In Android, we can use Alarms to schedule the tasks. Android has Alarm manager to manage these Alarms.
Service, perform long-running operations in the background and does not provide a user interface.
We need to use Alarm manger as well as service for scheduling background task like downloading information at particular time.
Steps to Create background task :
1. Create project, specify default activity. The following code to set schedule and cancel schedule for background task
public void btnStartSchedule(View v) {
try {
AlarmManager alarms = (AlarmManager) this
.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(getApplicationContext(),
AlaramReceiver.class);
intent.putExtra(AlaramReceiver.ACTION_ALARM,
AlaramReceiver.ACTION_ALARM);
final PendingIntent pIntent = PendingIntent.getBroadcast(this,
1234567, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarms.setRepeating(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis(), 10000, pIntent);
toast("Started...");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void btnCancelSchedules(View v) {
Intent intent = new Intent(getApplicationContext(),
AlaramReceiver.class);
intent.putExtra(AlaramReceiver.ACTION_ALARM,
AlaramReceiver.ACTION_ALARM);
final PendingIntent pIntent = PendingIntent.getBroadcast(this, 1234567,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarms = (AlarmManager) this
.getSystemService(Context.ALARM_SERVICE);
alarms.cancel(pIntent);
toast("Canceled...");
}
2. Create AlarmReceiver, for waking up the taskpublic static String ACTION_ALARM = "com.alarammanager.alaram";
@Override
public void onReceive(Context context, Intent intent) {
Log.i("Alarm Receiver", "Entered");
Toast.makeText(context, "Entered", Toast.LENGTH_SHORT).show();
Bundle bundle = intent.getExtras();
String action = bundle.getString(ACTION_ALARM);
if (action.equals(ACTION_ALARM)) {
Log.i("Alarm Receiver", "If loop");
Intent inService = new Intent(context,TaskService.class);
context.startService(inService);
}
else
{
Log.i("Alarm Receiver", "Else loop");
Toast.makeText(context, "Else loop", Toast.LENGTH_SHORT).show();
}
}
3. Create a Service do background taskpublic class TaskService extends IntentService {
public TaskService() {
super("TaskService");
// TODO Auto-generated constructor stub
}
@Override
protected void onHandleIntent(Intent arg0) {
// Do some task
Log.i("TaskService","Service running");
}
}
4. Specify the receiver and service in the Manifest file.<application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".AlaramScheduleActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name="AlaramReceiver" android:process=":remote" > </receiver> <service android:name=".TaskService" > </service> </application>References : download_source_code
