Create Repeating Alarm Start After Each 2 Minutes

In This example creating a simple repeating alarm system with the use of AlarmManager. Alarm will start after each 2 mins.

 

Steps :

  1. Using AlarmManager class to repeating and called a PendingIntent after each 2 min.
2. Call a media file on PendingIntent and start media file.
  3. So after each 2 min AlarmManager will call PendingIntent a media file start.

Project Structure :

 

create_alarm_alarmmanager_project_sketch

 

File : AndroidMainifest.xml

 

<?xml version="1.0" encoding="utf-8"?>
    package="com.example.alarmmanagerexample"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.alarmmanagerexample.AlarmManagerExample"
            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=".RingAlarm"
            android:label="@string/app_name" />
    </application>
</manifest>

 

File : src/AlarmManagerExample.java

 

Create a alarm with the use of AlarmManager which will call a pending indent RingAlarm after each 2 minutes.

 

import android.os.Bundle;
import android.os.SystemClock;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
public class AlarmManagerExample extends Activity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alarm_manager_example);
        
        
        try {
            
            //Create a new PendingIntent and add it to the AlarmManager
            Intent intent = new Intent(this, RingAlarm.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(this,
                12345, intent, PendingIntent.FLAG_CANCEL_CURRENT);
            AlarmManager am =
                (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
            am.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(),
                    2*60*60,pendingIntent);
            
          } catch (Exception e) {}
    }
}

 

File : src/RingAlarm.java

 

RingAlarm class create media player and start a audio file after each 2 minutes.

 

import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
public class RingAlarm extends Activity {
        
       MediaPlayer mp=null ;
      
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            
                    super.onCreate(savedInstanceState);
                    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
                    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                            WindowManager.LayoutParams.FLAG_FULLSCREEN);
                    
                    setContentView(R.layout.alarm);
                    Button stopAlarm = (Button) findViewById(R.id.stopAlarm);
                    
                    mp = MediaPlayer.create(getBaseContext(),R.raw.audio);
                    
                    
                    stopAlarm.setOnTouchListener(new OnTouchListener() {
                        
                        @Override
                        public boolean onTouch(View arg0, MotionEvent arg1) {
                            // TODO Auto-generated method stub
                            mp.stop();
                            finish();
                            return false;
                        }
                    });
             
                    playSound(this, getAlarmUri());
                }
             
                private void playSound(final Context context, Uri alert) {
                     
                    
                    Thread background = new Thread(new Runnable() {
                        public void run() {
                            try {
                                
                               mp.start();
                               
                            } catch (Throwable t) {
                                Log.i("Animation", "Thread  exception "+t);
                            }  
                        }
                 });
                 background.start();
               }
             
                @Override
                protected void onDestroy() {
                    super.onDestroy();
                    mp.stop();
                }               //Get an alarm sound. Try for an alarm. If none set, try notification,
                    //Otherwise, ringtone.
                private Uri getAlarmUri() {
                    
                    Uri alert = RingtoneManager
                            .getDefaultUri(RingtoneManager.TYPE_ALARM);
                    if (alert == null) {
                        alert = RingtoneManager
                                .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                        if (alert == null) {
                            alert = RingtoneManager
                                    .getDefaultUri(RingtoneManager.TYPE_RINGTONE);
                        }
                    }
                    return alert;
                }
            
}

http://androidexample.com/Create_Repeating_Alarm_Start_After_Each_2_Minutes/index.php?view=article_discription&aid=93&aaid=116

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s