Date and Time picker set AlarmManager using Calendar

Put your problem here if it does not fit any of the other categories.

Date and Time picker set AlarmManager using Calendar

Postby iPaul Pro » Tue Apr 07, 2009 5:36 am

I'd like to have an alarm time set by the output of a DatePicker and TimePicker. Was wondering if there's an easy/straightforward way to do this without going nuts with Calendar and Date equations.

I'm using the exact Android DatePicker/TimePicker sample code found here,

And this is my AlarmManager:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. private OnClickListener mOneShotListener = new OnClickListener() {
  3.  
  4.                 public void onClick(View v) {
  5.  
  6.  
  7.  
  8.                     Intent intent = new Intent(DateWidgets1.this, OneShotNotif.class);
  9.  
  10.                     PendingIntent sender = PendingIntent.getBroadcast(DateWidgets1.this,
  11.  
  12.                             REQUEST_CODE, intent, 0);
  13.  
  14.        
  15.  
  16.                     // We want the alarm to go off 5 seconds from now.
  17.  
  18.                     Calendar calendar = Calendar.getInstance();
  19.  
  20.                     calendar.setTimeInMillis(System.currentTimeMillis());
  21.  
  22.                     calendar.add(Calendar.SECOND, 5);
  23.  
  24.                    
  25.  
  26.                     Date timeSet = calendar.getTime();
  27.  
  28.        
  29.  
  30.                     // Schedule the alarm!
  31.  
  32.                     AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
  33.  
  34.                     am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
  35.  
  36.                            
  37.  
  38.                     // Tell the user about what we did.
  39.  
  40.                     if (mToast != null) {
  41.  
  42.                         mToast.cancel();
  43.  
  44.                     }
  45.  
  46.                     mToast = Toast.makeText(DateWidgets1.this, "Alarm Set for " + timeSet,
  47.  
  48.                             Toast.LENGTH_LONG);
  49.  
  50.                     mToast.show();
  51.  
  52.  
  53.  
  54.                 }
  55.  
  56.             };
  57.  
  58.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


So i'd like to dynamically replace "calendar.getTimeInMillis()" with the date and time chosen in the DatePicker and TimePicker. It is important that the exact date and time are passed to the AlarmManager.

Any help would be greatly appreciated!

Thanks
iPaul Pro
Developer
Developer
 
Posts: 33
Joined: Sun Mar 15, 2009 9:01 pm

Top

Postby iPaul Pro » Tue Apr 07, 2009 3:00 pm

Figured it out - knew it had to be simple.

In order for this snippet to work, you must be using Android sample code for DatePicker and TimePicker (link above).

To set an AlarmManager from DatePicker and TimePicker output, using Calendar try this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  // New Calendar instance
  3.  
  4. Calendar calendar = Calendar.getInstance();
  5.  
  6.  // Pass in date from DatePicker and TimePicker output variables
  7.  
  8. calendar.set(mYear, mMonth, mDay, mHour, mMinute);
  9.  
  10.  // Set the long time variable to current Calendar
  11.  
  12. long timeSet = calendar.getTimeInMillis();
  13.  
  14.  // Schedule the alarm!
  15.  
  16.  AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
  17.  
  18.  am.set(AlarmManager.RTC_WAKEUP, timeSet, sender);
  19.  
  20.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
iPaul Pro
Developer
Developer
 
Posts: 33
Joined: Sun Mar 15, 2009 9:01 pm

AlarmManager

Postby shashankgr8 » Thu Aug 27, 2009 7:44 am

Hi

While creating and Intent u have Specified a Class called OneShotNotif.class
What should we write in that class

Waiting for Reply

Thanks and Regards
Shashank Shekhar
shashankgr8
Freshman
Freshman
 
Posts: 5
Joined: Fri Jul 17, 2009 12:53 pm

Postby shashankgr8 » Thu Aug 27, 2009 10:14 am

This is the Activity Class that i have written


package HomeSolution.NuHomes;

import java.text.DateFormat;
import java.util.Calendar;


import android.app.Activity;
import android.app.AlarmManager;
import android.app.DatePickerDialog;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;

import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

public class Clock extends Activity
{
DateFormat fmtDateAndTime=DateFormat.getDateTimeInstance();
TextView dateAndTimeLabel;
Calendar dateAndTime=Calendar.getInstance();
Button Alarm;
Clock instance;

Toast mToast=null;
DatePickerDialog.OnDateSetListener d=new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
dateAndTime.set(Calendar.YEAR, year);
dateAndTime.set(Calendar.MONTH, monthOfYear);
dateAndTime.set(Calendar.DAY_OF_MONTH, dayOfMonth);

}
};
TimePickerDialog.OnTimeSetListener t=new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
dateAndTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
dateAndTime.set(Calendar.MINUTE, minute);

view.setCurrentHour(Calendar.HOUR_OF_DAY);

}
};
@Override
public void onCreate(Bundle icicle) {

super.onCreate(icicle);
setContentView(R.layout.clock);
instance = this;
Button btn=(Button)findViewById(R.id.Date);
Alarm = (Button)findViewById(R.id.Alarm);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new DatePickerDialog(Clock.this,
d,
dateAndTime.get(Calendar.YEAR),
dateAndTime.get(Calendar.MONTH),
dateAndTime.get(Calendar.DAY_OF_MONTH)).show();
}
});
btn=(Button)findViewById(R.id.time);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new TimePickerDialog(Clock.this,
t,
dateAndTime.get(Calendar.HOUR_OF_DAY),
dateAndTime.get(Calendar.MINUTE),
true).show();
}
});

Alarm.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{

Intent intent = new Intent(instance, OnetimeAlarmReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(instance,
0, intent, 0);
dateAndTime.add(Calendar.SECOND, 5);
long timeSet = dateAndTime.getTimeInMillis();

AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, timeSet, sender);

if (mToast != null) {
mToast.cancel();
}
mToast = Toast.makeText(instance, "Alarm Set for " + dateAndTime.getTime(),
Toast.LENGTH_LONG);
mToast.show();
}
});
}
}[fade]



and this is the service class For AlertChecking

package HomeSolution.NuHomes;




import android.app.Service;
import android.app.AlertDialog.Builder;
import android.content.Intent;

import android.os.IBinder;
import android.util.Log;

public class OnetimeAlarmReceiver extends Service{

@Override
public void onCreate() {
super.onCreate();
Builder builder = new Builder(this);
builder.setMessage("Started").create().show();
Log.d("Value ", "Service Started");
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Builder builder = new Builder(this);
builder.setMessage("Started").create().show();
Log.d("Value ", "Service Started");
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}


This is the XMl File I am Using

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="wrap_content">
<AnalogClock android:id="@+id/analog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
/>
<DigitalClock android:id="@+id/digital"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@id/analog"
/>


<Button
android:layout_height="wrap_content"
android:layout_below="@+id/digital" android:layout_marginTop="40px" android:text="Set The Date" android:id="@+id/Date" android:layout_width="wrap_content" android:layout_centerHorizontal="false" android:layout_marginLeft="60px">
</Button>
<Button
android:text="Set Alarm"
android:id="@+id/Alarm"
android:layout_height="wrap_content"
android:layout_centerHorizontal="false"
android:layout_width="wrap_content"
android:layout_below="@+id/Date" android:layout_centerInParent="false" android:layout_marginLeft="60px" android:minWidth="205px">
</Button>
<Button
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Set The Time" android:id="@+id/time" android:layout_width="wrap_content" android:layout_toRightOf="@+id/Date" android:layout_below="@+id/digital" android:layout_marginTop="40px">
</Button>

</RelativeLayout>

Please Help Me On ALarmManager
shashankgr8
Freshman
Freshman
 
Posts: 5
Joined: Fri Jul 17, 2009 12:53 pm

Postby iPaul Pro » Thu Aug 27, 2009 2:06 pm

Hi shashankgr8,

I did not specify the OneShotNotif.class because that was the specific service I needed to run when the alarm went off. In my application, it adds a new notification to the status bar. For testing purposes, your OnetimeAlarmReceiver should be fine.

Perhaps you could tell me what the specific issue is. Is your OnetimeAlarmReceiver being fired on time (in logcat)? If you are more specific, I could probably be of some assistance.

Paul
iPaul Pro
Developer
Developer
 
Posts: 33
Joined: Sun Mar 15, 2009 9:01 pm

Postby iPaul Pro » Thu Aug 27, 2009 2:20 pm

A couple of things I was thinking about while reviewing your code:

Have you specified the OnetimeAlarmReceiver in your Manifest? I know this sounds silly, but it's happened to me.

To avoid issues with multiple alarms, you are going to want to give the PendingIntent a unique ID, and possibly a "one shot" flag. eg:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. PendingIntent sender = PendingIntent.getBroadcast(instance, 1234, intent, PendingIntent.FLAG_ONE_SHOT);
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


Try that and let me know how it goes (with specific details).
iPaul Pro
Developer
Developer
 
Posts: 33
Joined: Sun Mar 15, 2009 9:01 pm

Top

Postby iPaul Pro » Thu Aug 27, 2009 2:33 pm

I figured it out!

You are trying to start a Service from the AlarmManager, instead of a BroadcastReceiver.

Your OnetimeAlarmReceiver should extend a BroadcastReceiver. e.g.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class OnetimeAlarmReceiver extends BroadcastReceiver {
  3.  
  4.          @Override
  5.  
  6.          public void onReceive(Context context, Intent intent) {
  7.  
  8.                  Toast.makeText(context, "Alarm Received", Toast.LENGTH_SHORT).show();
  9.  
  10.          }
  11.  
  12. }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


(I still suggest giving your PendingIntent a unique ID, and remember to update your Manifest)

Good luck,

Paul
iPaul Pro
Developer
Developer
 
Posts: 33
Joined: Sun Mar 15, 2009 9:01 pm

Postby shashankgr8 » Fri Aug 28, 2009 7:03 am

what should i specify in that AndroidMenifest i should declare it as a receiver ??
shashankgr8
Freshman
Freshman
 
Posts: 5
Joined: Fri Jul 17, 2009 12:53 pm

Postby shashankgr8 » Fri Aug 28, 2009 7:05 am

no, my OnetimeAlarmReceiver not being fired on time. I have seen in logcat .. its not at all going
shashankgr8
Freshman
Freshman
 
Posts: 5
Joined: Fri Jul 17, 2009 12:53 pm

Postby arungupta2jan » Fri Aug 28, 2009 7:22 am

I think your Android manifest needs to be modified

<receiver android:name=".your reciever class" android:process=":remote" />

just check once
arungupta2jan
Developer
Developer
 
Posts: 28
Joined: Tue Jul 28, 2009 12:53 pm

Postby shashankgr8 » Fri Aug 28, 2009 8:13 am

hey thanks buddy!!

actually no problem with the code i was putting receiver outside of that Application in androidMenifest
Thanks, Thanks a lot.

Now one more issue.. i want to set it as a Reminder so ... it may happen that in between some period of time mobile get switch of so i want to start that reminder on the boot.. what should i do for

sorry for my poor English

thanks and regards
Shashank Shekhar
shashankgr8
Freshman
Freshman
 
Posts: 5
Joined: Fri Jul 17, 2009 12:53 pm

Postby iPaul Pro » Fri Aug 28, 2009 7:42 pm

If you are dealing with only one alarm, you can use the SharedPreferences to hold the data associated, otherwise I would suggest using a SQL database.

Implementing a BroadcastReciever to listen for boot complete, that fires your alarm-restore service.

Autostart an app at Boot (AndroidSnippets)

Good luck,

Paul
iPaul Pro
Developer
Developer
 
Posts: 33
Joined: Sun Mar 15, 2009 9:01 pm

need help with the same

Postby shishir.bobby » Tue Mar 23, 2010 8:12 am

hi team,

i am also looking fwd to create this type of app, i need to set alarm through date n time picker.

any help would be appreciated, looking fwd for an quick reply


regards
shishir.bobby
shishir.bobby
Experienced Developer
Experienced Developer
 
Posts: 76
Joined: Thu Apr 09, 2009 11:41 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: MSNbot Media, Yahoo [Bot] and 19 guests