AlarmManager class provides access to the system alarm services. These allow you to schedule your application to be run at some point in the future. When an alarm goes off, the Intent that had been registered for it is broadcast by the system, automatically starting the target application if it is not already running.
In this exercise, a scheduled alarm of 10 seconds will start a service, MyAlarmService.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/startalarm" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Start" /> <Button android:id="@+id/cancelalarm" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Cancel" /> </LinearLayout>
AndroidAlarmService.java, the main activitypackage com.shir60bhushan.AndroidAlarmService; import java.util.Calendar; import android.app.Activity; import android.app.AlarmManager; import android.app.PendingIntent; import android.content.Intent; import android.os.Bundle; import android.os.SystemClock; import android.view.View; import android.widget.Button; import android.widget.Toast; public class AndroidAlarmService extends Activity { private PendingIntent pendingIntent; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button buttonStart = (Button)findViewById(R.id.startalarm); Button buttonCancel = (Button)findViewById(R.id.cancelalarm); buttonStart.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent myIntent = new Intent(AndroidAlarmService.this, MyAlarmService.class); pendingIntent = PendingIntent.getService(AndroidAlarmService.this, 0, myIntent, 0); AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); calendar.add(Calendar.SECOND, 10); alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); Toast.makeText(AndroidAlarmService.this, "Start Alarm", Toast.LENGTH_LONG).show(); }}); buttonCancel.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View arg0) { // TODO Auto-generated method stub AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); alarmManager.cancel(pendingIntent); // Tell the user about what we did. Toast.makeText(AndroidAlarmService.this, "Cancel!", Toast.LENGTH_LONG).show(); }}); } } this the MyAlarmService.java service for managing and triggering the alarm
}package com.Shir60bhushan.AndroidAlarmService; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.widget.Toast; public class MyAlarmService extends Service { @Override public void onCreate() { // TODO Auto-generated method stub Toast.makeText(this, "MyAlarmService.onCreate()", Toast.LENGTH_LONG).show(); } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub Toast.makeText(this, "MyAlarmService.onBind()", Toast.LENGTH_LONG).show(); return null; } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG).show(); } @Override public void onStart(Intent intent, int startId) { // TODO Auto-generated method stub super.onStart(intent, startId); Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG).show(); } @Override public boolean onUnbind(Intent intent) { // TODO Auto-generated method stub Toast.makeText(this, "MyAlarmService.onUnbind()", Toast.LENGTH_LONG).show(); return super.onUnbind(intent); }
Finally Add the service declaration in the manifest file of the application
<service android:name=".MyAlarmService" />
2 comments :
Most of you hunger to know more about the glitzy
life styles of glamorous celebrities and sensual models.
Aamir Khan and Amitabh Bachchan had a rough battle, when the movies
related to them were released at the same time. She married singer Mark Anthony in2005 and released her next
album called Rebirth.
my web blog - latest celeb news
Definitely believe that which you stated.
Your favorite reason seemed to be on the internet the easiest thing to be aware of.
I say to you, I certainly get irked while people consider worries that they just do not know about.
You managed to hit the nail upon the top as well as defined out
the whole thing without having side effect , people could take a signal.
Will probably be back to get more. Thanks
My weblog; web hosting canada reviews
Post a Comment