AlarmManager Tutorial

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

AlarmManager Tutorial

Postby WWaldo » Thu May 26, 2011 9:04 pm

Hey everyone,

I have been working on an app that uses the Alarmmanager, but I haven't had much luck finding an example that shows how to use it very well. I was wondering if anyone here knew of a tutorial that showed how this worked pretty decently, or had a sample project they could share. If so that would be great. Thanks in advance!

WWaldo
WWaldo
Experienced Developer
Experienced Developer
 
Posts: 58
Joined: Sun Feb 21, 2010 8:25 pm

Top

Re: AlarmManager Tutorial

Postby raj_J2ME » Fri May 27, 2011 7:00 am

Hey check this link out, this is very much helpful, this is taljing about the overview of the same.
http://developer.android.com/reference/android/app/AlarmManager.html

Other than that if you are seeking a code sample for the same, you can check this example here,
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/AlarmService.html

Please check if these links helps you in any means.. :)
Thanks with Regards,
Raj - The Kernel
raj_J2ME
Master Developer
Master Developer
 
Posts: 311
Joined: Tue Feb 15, 2011 7:40 am
Location: The Capital of India,

Re: AlarmManager Tutorial

Postby WWaldo » Fri May 27, 2011 1:58 pm

The API and stuff didn't make much sense to me before, but I found an example and now all the stuff you posted makes sense. Here's what I found:
Code: Select all
import java.util.Calendar;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.ListActivity;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class TextScheduler extends ListActivity {
   /** Called when the activity is first created. */
   /* for logging - see my tutorial on debuggin Android apps for more detail */
   private static final String TAG = "SomeApp ";

   protected Toast mToast;

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      //setContentView(R.layout.alert_list);
      try {
         Calendar cal = Calendar.getInstance();

         Intent intent = new Intent(this, AReceiver.class);
         PendingIntent sender = PendingIntent.getBroadcast(this, 1234567, intent, 0);
         PendingIntent sende2 = PendingIntent.getBroadcast(this, 123123, intent, 0);

         AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
         am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis()+3000, sender); // to be alerted 30 seconds from now
         am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis()+10000, sende2); // to be alerted 15 seconds from now

         /* To show how alarms are cancelled we will create a new Intent and a new PendingIntent with the
          * same requestCode as the PendingIntent alarm we want to cancel. In this case, it is 1234567.
          * Note: The intent and PendingIntent have to be the same as the ones used to create the alarms.
          */
         Intent intent1 = new Intent(this, AReceiver.class);
         PendingIntent sender1 = PendingIntent.getBroadcast(this, 123123, intent1, 0);
         AlarmManager am1 = (AlarmManager) getSystemService(ALARM_SERVICE);
         am1.cancel(sender1);

      } catch (Exception e) {
         Log.e(TAG, "ERROR IN CODE:"+e.toString());
      }
   }
}
Code: Select all
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

public class AReceiver extends BroadcastReceiver{
   @Override
   public void onReceive(Context con, Intent in) {
      try {
         /* Display an alert */
         Toast.makeText(con, "How are you?", Toast.LENGTH_LONG).show();
      }
      catch (Exception r) {
         Toast.makeText(con, "You were supposed to do something"
               +" now but I can't retrieve what it was.",
               Toast.LENGTH_SHORT).show();
         Log.e("ALARM_RECEIVER", r.toString());
      }
   }
}


Thanks for all your help, now I have all the info I need
WWaldo
Experienced Developer
Experienced Developer
 
Posts: 58
Joined: Sun Feb 21, 2010 8:25 pm

Unable to set alarm time

Postby subashprasanna » Sat Jun 04, 2011 11:41 am

Dear Sir/Madam,

In my alarm application, i take time from time picker and set it in alarm manager. But the time which i set in the time picker was not set properly in alarm manager. so alarm is not activated on specified time. i show my coding here, could anybody help me out. Thanks in advance.

layout/main.xml
-----------------
<?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"
>
<Button
android:id="@+id/btn"
android:text="Alerm"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>

------------------
MainActivity.java
----
public class MainActivity extends Activity {
private Button btn = null;
private AlarmManager alarmManager = null;
Calendar cal = Calendar.getInstance();
static final int DIALOG_TIME = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DIALOG_TIME);
}
});
}

@Override
protected Dialog onCreateDialog(int id) {
Dialog dialog = null;
switch(id){
case DIALOG_TIME:
dialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Calendar c = Calendar.getInstance();
c.setTimeInMillis(System.currentTimeMillis());
c.clear();
//c.set(Calendar.YEAR, 2011);
//c.set(Calendar.MONTH, 6);
//c.set(Calendar.DAY_OF_MONTH, 4);
c.set(Calendar.HOUR, hourOfDay);
c.set(Calendar.MINUTE, minute);
c.set(Calendar.SECOND, 0);
//c.set(Calendar.MILLISECOND, 0);
Intent intent = new Intent(MainActivity.this,AlarmReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);
//alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES, pi);
Toast.makeText(MainActivity.this, "Alarm has been set..", Toast.LENGTH_LONG).show();
}
},cal.get(Calendar.HOUR_OF_DAY),cal.get(Calendar.MINUTE),false);
break;
}
return dialog;
}
}
-------------
AlarmReceiver.java
----
public class AlarmReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, AlarmActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
-------------------------------------------
AlarmActivity.java
-----
public class AlarmActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new AlertDialog.Builder(AlarmActivity.this).setTitle("Task").setMessage("Time to wake up").setPositiveButton("ok", new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
AlarmActivity.this.finish();
}
}).create().show();
}
}
-----------------------------------------
AndroidManifest.xml
----
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.phone"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />

<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".AlarmActivity"></activity>
<receiver android:name=".AlarmReceiver" android:process=":remote"></receiver>
</application>
</manifest>
---------------------------------
Kindly some one help me out to find a solution.
subashprasanna
Once Poster
Once Poster
 
Posts: 1
Joined: Sat Jun 04, 2011 11:32 am

Re: AlarmManager Tutorial

Postby Jayyu » Thu Apr 11, 2013 2:29 pm

@subashprasanna this might help you to solve your problem,i know it's late but it might help others

Code: Select all
   TimePicker myTimePicker;
   Button buttonstartSetDialog;
   TextView textAlarmPrompt;

   TimePickerDialog timePickerDialog;

   final static int RQS_1 = 1;

   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState)
   {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);

      textAlarmPrompt = (TextView)findViewById(R.id.alarmprompt);

      buttonstartSetDialog = (Button)findViewById(R.id.startSetDialog);
      buttonstartSetDialog.setOnClickListener(new OnClickListener()
      {
         @Override
         public void onClick(View v)
         {
            textAlarmPrompt.setText("");
            openTimePickerDialog(false);
         }
      });
   }

   private void openTimePickerDialog(boolean is24r)
   {
      Calendar calendar = Calendar.getInstance();

      timePickerDialog = new TimePickerDialog(
            AndroidTimeActivity.this,
            onTimeSetListener,
            calendar.get(Calendar.HOUR_OF_DAY),
            calendar.get(Calendar.MINUTE),
            is24r);
      timePickerDialog.setTitle("Set Alarm Time"); 

      timePickerDialog.show();

   }

   OnTimeSetListener onTimeSetListener = new OnTimeSetListener()
   {
      @Override
      public void onTimeSet(TimePicker view, int hourOfDay, int minute)
      {
         Calendar calNow = Calendar.getInstance();
         Calendar calSet = (Calendar) calNow.clone();

         calSet.set(Calendar.HOUR_OF_DAY, hourOfDay);
         calSet.set(Calendar.MINUTE, minute);
         calSet.set(Calendar.SECOND, 0);
         calSet.set(Calendar.MILLISECOND, 0);

         if(calSet.compareTo(calNow) <= 0)
         {
            //Today Set time passed, count to tomorrow
            calSet.add(Calendar.DATE, 1);
         }

         setAlarm(calSet);
      }
   };

   private void setAlarm(Calendar targetCal)
   {
      textAlarmPrompt.setText(
            "\n\n***\n"
                  + "Alarm is set@ " + targetCal.getTime() + "\n"
                  + "***\n");

      Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
      PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
      AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
      alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);
   }





import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.widget.Toast;

public class AlarmReceiver extends BroadcastReceiver
{
   @Override
   public void onReceive(Context arg0, Intent arg1)
   {
      Toast.makeText(arg0, "Alarm received!", Toast.LENGTH_LONG).show();
   }
}









Jayyu
Once Poster
Once Poster
 
Posts: 1
Joined: Thu Apr 11, 2013 2:07 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 18 guests