Basically
if you want to start some actions at any specific time,you need to use alarm
service.For example, you want to start some activity at 1.30PM, then you have
to use alarm service.Before going to discuss about alarm service you need to
know about Pending Intent.
Pending Intent:
A Pending
Intent is a token you give to some app to perform an action on your apps'
behalf irrespective of
whether your application process is alive or not.
For example , at 1.30PM you want to download some songs from
any url. But at1.30PM you will not be there. So it’s need to give some one else(Alarm
Manager) to start downloading my song. Since you are giving intent to Alarm
Manager and intent will be pending till 1.30PM . That’s why we called it as
pending intent .
Now Let’s do the programming for Alarm Service:
Step1:
Create a new Project AlarmTest.
Step2:
Here I am taking class as Home.java
Step3:
Go to Manifest.xml, Create a receiver(Name:
Receiver1).
Step4:
Go to main.xml. Create a Button.
Step5:
Now come Back 2 Home.java(main activity). Create the
AlarmManager & PendingIntent.
AlarmManager
am = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Here we are calling ALARM_SERVICE by using Context and
storing it in alarm manager instance.
Now we need to see about PendingIntent. Here we created
Broadcast Receiver. So we need to get that Broadcast Receiver. See the bellow
code:
PeningIntent
pi =PendingIntent.getBroadcast(Context context,
int requestCode, Intent intent, int flags)
Now Observe the
Home.java
public class Home extends Activity
{
AlarmManager am;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn =
(Button)findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener()
{
public void
onClick(View v)
{
am =
(AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent i =
new Intent(getApplicationContext(), Receiver1.class);
PendingIntent
pi = PendingIntent.getBroadcast(getApplicationContext(), 0, i, 0);
am.set(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis()+ 10000, pi);
}
});
}
}
See Here in the bellow coding:
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+
10000, pi);
Here it is telling like that Please WAKE_UP the alarm after
10 seconds. After 10 seconds Receiver1 will started.
Receiver1.java
public class Receiver1 extends BroadcastReceiver
{
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context,
"Receiver1 is Activated", 0).show();
}
}
Main.xml
<?xml version="1.0"
encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Alarm
Testing" />
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Button" />
</RelativeLayout>
</LinearLayout>
Manifest.xml
<?xml version="1.0"
encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="alarm.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
>
<activity
android:name=".Home"
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="Receiver1"></receiver>
</application>
</manifest>
WakingNews Alarm Clock app
ReplyDeleteWakingNews Alarm Clock is yet another beta app release with some potential. It functions primarily as an alarm clock. You set an alarm as usual and it goes off on time as usual. However, this one reads you the news from a variety of sources when it goes off. So it functions a little bit like old school radio alarm clocks. It has several good news sources, including Yahoo Finances, Yahoo Sports, Engadget, etc. However, there are some lesser sources of words there as well. Thankfully, you can choose the sources that play when the alarm goes off. It's in beta so there are definitely bugs. It is also free and has potential.