Search code examples
androideclipsealarmmanager

how to use alarm manager with specific dates?


hi i had created my first android app to play sound when specific times i did my code well and i added list of days and times which the sound play . when i searched more about how to play the sound when the mobile locked as alarm . finally i reached that i must use alarm manager . please i need someone help me as i couldn't do it . as i want when the specific times that i added below the sound playing .

Note: i use interface class to call call html files to android

public class WebAppInterface    {
Context mContext;
public  MediaPlayer mp = null;
 public static boolean checked = false;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
    mContext = c;
    mp = MediaPlayer.create(mContext,R.raw.sound);

}


@JavascriptInterface
public void playsound(String value  ) throws ParseException {


   //get current date time with Date()

    String dateStr = "0, 3, 11, 9, 7, 2013"; 

    SimpleDateFormat dateFormat = new SimpleDateFormat("ss,hh,mm,yyyy,MM,dd"); 
    Time time = new Time();
    System.out.println(dateFormat.format(time));

    //1day
     time.set(0, 3, 11, 9, 7, 2013);
     time.set(0, 3, 18, 9, 7, 2013);
     time.set(0, 5, 0, 9, 7, 2013);
     time.set(0, 12, 1, 9, 7, 2013);
     time.set(0, 3, 37, 9, 7, 2013);
     time.set(0, 7, 0, 9, 7, 2013);
     time.set(0, 8, 32, 9, 7, 2013);
     //2day
     time.set(0, 3, 11, 10, 7, 2013);
     time.set(0, 3, 18, 10, 7, 2013);
     time.set(0, 5, 0, 10, 7, 2013);
     time.set(0, 12, 1, 10, 7, 2013);
     time.set(0, 3, 37, 10, 7, 2013);
     time.set(0, 7, 0, 10, 7, 2013);
     time.set(0, 8, 31, 10, 7, 2013);

}

public class x extends Activity {

private MediaPlayer mediaplayer;
private String TAG;
Context mContext;
private IntentListener listener = new IntentListener();

WebAppInterface wb=null;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity);


    //Call HTML Files
    WebView myWebView = (WebView) findViewById(R.id.web_engine);

    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    myWebView.loadUrl("file:///android_asset/index.html");

    //Initialise WebAppInterface and pass this ref..
    wb=new WebAppInterface(this);
    myWebView.addJavascriptInterface(wb, "Android");

}

}


Solution

  • first you need to create a class that extends BroadcastReceiver:

    public class AlarmManagerBroadcastReciever extends BroadcastReceiver {
    
    @Override
    public void onReceive(Context context, Intent intent) {
        PowerManager pm = (PowerManager) context
                .getSystemService(Context.POWER_SERVICE);
        PowerManager.WakeLock wl = pm.newWakeLock(
                PowerManager.PARTIAL_WAKE_LOCK, "YOUR TAG");
        // Acquire the lock
        wl.acquire();
        MediaPlayer mp=MediaPlayer.create(context,R.raw.<your audio>);
        mp.start();
        // Release the lock
        /// do your playing sound here
        wl.release();
    }
    
    public void SetAlarm(Context con, int id, int hour, int min) {
        AlarmManager am = (AlarmManager) con
                .getSystemService(Context.ALARM_SERVICE);
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
                cal.get(Calendar.DAY_OF_MONTH), hour, min);
        Intent intent = new Intent(con, AlarmManagerBroadcastReciever.class);
    
        PendingIntent sender = PendingIntent.getBroadcast(con, id, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
    }
    
    public void CancelAlarm(Context context, int size) {
                // canceling all alarms 
        for (int i = 0; i < size; i++) {
            Intent intent = new Intent(context,
                    AlarmManagerBroadcastReciever.class);
            PendingIntent sender = PendingIntent.getBroadcast(context, i,
                    intent, 0);
            AlarmManager alarmManager = (AlarmManager) context
                    .getSystemService(Context.ALARM_SERVICE);
            alarmManager.cancel(sender);
        }
    }
    

    }

    in your MainActivity make an object of this class:

    AlarmManagerBroadcastReciever alarm = new AlarmManagerBroadcastReciever();
    

    and add methods below:

    public void startTimer(int hour, int min, int id) {
        Context context = this.getApplicationContext();
        if (alarm != null) {
            alarm.SetAlarm(context, id, hour, min);
        } else {
            Toast.makeText(context, "Alarm is null", Toast.LENGTH_SHORT).show();
        }
    }
    
    public void cancelTimer() {
        Context context = this.getApplicationContext();
        if (alarm != null) {
            alarm.CancelAlarm(context, <number of alarms>);
        } else {
            Toast.makeText(context, "Alarm is null", Toast.LENGTH_SHORT).show();
        }
    }
    

    use startTimer() to set specific alarm and in onRecieve() of broadcastReciever do your things.Don't forget to add your broadcastReciever to manifest file:

    <receiver android:name="<package name>.AlarmManagerBroadcastReciever"
            >
        </receiver>
    

    good luck ;)