Android-Alarm-Manager-Service
Hi Friend,
Today i am going to discuss alarm service, that provide by Android.
Using android alarm manager service we can start any activity at a particular time.
we can create time related utility application.
i am going to example of simple alarm application using date pecker and time pecker widgets and alarmservice.
Splash Activity code:-
In this activity i had used animation and custom design so you have to create
i. res/anim folder and make a blink.xml file, code given below:-
android:toAlpha="1.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:duration="600"
android:repeatMode="reverse"
android:repeatCount="3"/>
Today i am going to discuss alarm service, that provide by Android.
Using android alarm manager service we can start any activity at a particular time.
we can create time related utility application.
i am going to example of simple alarm application using date pecker and time pecker widgets and alarmservice.
Splash Activity code:-
In this activity i had used animation and custom design so you have to create
i. res/anim folder and make a blink.xml file, code given below:-
android:toAlpha="1.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:duration="600"
android:repeatMode="reverse"
android:repeatCount="3"/>
ii. res/drawable flolder and make a design.xml, code given below
android:centerColor="#ccffcc"
android:endColor="#fffccc"/>
iii. using btndesign.xml file i have maintain click event effect on button.
res/drawable folder create another btndesign.xml , write code that given below:-
xmlns:android="http://schemas.android.com/apk/res/android">
android:color="#00ff00" />
android:width="5dp"
android:color="#ff0000"
android:dashWidth="3dp"
android:dashGap="2dp" />
android:endColor="#ffffff"
android:centerColor="#ffffff"
android:startColor="#ffffff"
android:angle="270" />
android:width="3dp"
android:color="#00ff00" />
android:radius="5dp" />
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
android:endColor="#ffffff"
android:centerColor="#ffffff"
android:startColor="#ffffff"
android:angle="270" />
android:width="5dp"
android:color="#00ff00" />
android:radius="5dp" />
XML Source Code :-
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".AlarmExample" >
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/design"
android:text="Developed By : SHIVENDRA SRIVASTWA"
android:textColorHint="#ffccff"
android:textSize="22sp" />
java Source code :-
package in.androidshivedra.alarmserviceex;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;
public class AlarmExample extends Activity implements AnimationListener {
TextView tv;
// Animation
Animation animBlink, animflip;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm_example);
tv = (TextView)findViewById(R.id.textview1);
// load the animation
animBlink = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.blink);
// set animation listener
animBlink.setAnimationListener(this);
// button click event
tv.setVisibility(View.VISIBLE);
// start the animation
tv.startAnimation(animBlink);
StringBuffer sb = new StringBuffer();
StringBuilder sbu = new StringBuilder();
}
@Override
public void onAnimationEnd(Animation animation) {
// Take any action after completing the animation
// check for blink animation
if (animation == animBlink) {
startActivity(new Intent(AlarmExample.this, MainActivity.class));
}
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationStart(Animation animation) {
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}
Source Code for MainActivity:-
package in.androidshivedra.alarmserviceex;
import java.util.Calendar;
import java.util.Date;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;
public class MainActivity extends Activity {
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
////***********************************************///////
Calendar cal = Calendar.getInstance();
TimePicker tp = (TimePicker)findViewById(R.id.timePicker1);
DatePicker dp = (DatePicker)findViewById(R.id.datePicker1);
Date d = new Date((dp.getYear()-1900), dp.getMonth(), dp.getDayOfMonth(), tp.getCurrentHour(), tp.getCurrentMinute());
cal.setTime(d);
Intent i = new Intent(arg0.getContext(),MainActivity2.class);
PendingIntent pending = PendingIntent.getActivity(MainActivity.this,0,i,PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),pending);
Toast.makeText(getApplicationContext(), "Your alarm set for :"+cal.getTime().toGMTString(), Toast.LENGTH_LONG).show();
////******************************************///////
}
});
}
}