Thursday 22 March 2012

Android Alarm Example


import java.util.Calendar;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class AlarmDemoActivity 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) {

Intent intent= new Intent(AlarmDemoActivity.this,MyAlarmService.class);
pendingIntent =PendingIntent.getService(AlarmDemoActivity.this,1, intent,1);

AlarmManager manager=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
manager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+20000, pendingIntent);
//manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 5*1000, pendingIntent);
}
});

buttonCancel.setOnClickListener(new Button.OnClickListener() {

@Override
public void onClick(View arg0) {

AlarmManager manager=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
manager.cancel(pendingIntent);
}
});

}

}



MyAlarmService.java







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);

}



}

22 comments:

  1. please send me this complete code of alarm on my mail id:
    saparia.maharshi@gmail.com

    ReplyDelete
  2. can you please send me the complete code, this is my email :
    abdellah.felahi@gmail.com

    ReplyDelete
  3. can u plz send me the complete code. it will be verymuch helpfull for me.

    Thanks in advance :) please do send it :)

    email : ashwinimohan888@gmail.com

    ReplyDelete
  4. Please I need the Complete code very very Urgent ,i will be very grateful for u :) this is my email

    samar.abou.abeed.saad@gmail.com

    thanks very much for ur effort :)

    ReplyDelete
  5. May i have the complete code? ^^

    Email: Vivian63093@hotmail.com

    ReplyDelete
  6. ply send that complete code..

    ReplyDelete
  7. hi sir ,
    please tell me this condition ... if i am set Alarm ... come out from out Activity .....
    i will get particular alarm at time .
    and how can i use the service code for that .... ?

    Thanks
    Govind Rao

    ReplyDelete
  8. please send this complete code to me..
    samba.y8it866@gmail.com

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete
  10. please send this complete code to me...
    hoconlineit@gmail.com

    ReplyDelete
  11. plz can u send this code at

    prv.ashraf@gmail.com

    ReplyDelete
  12. plz send this code
    ankitsevakhr@gmail.com

    ReplyDelete
  13. please send this complete code
    stu.elitefriends@gmail.com

    ReplyDelete
  14. Can you please send this complete code
    mihir8589@gmail.com

    ReplyDelete
    Replies
    1. Can you please send this complete code
      taitien92@outlook.com

      Delete
  15. Can you please send this complete code
    taitien92@outlook.com

    ReplyDelete
  16. please send me this complete code of alarm on my mail id:
    javier-huchin@hotmail.com

    ReplyDelete
  17. can you please send this complete code giangphamvp@gmail.com

    ReplyDelete
  18. can u plz send complete code of this.....
    waqar.vky14@gmail.com
    this is my id....... thnx

    ReplyDelete
  19. can you please send this complete code:
    khuatvannhan@gmail.com

    ReplyDelete
  20. Can someone send me complte code please. I need urgently it.
    aslansemir@hotmail.con

    ReplyDelete