Friday, 10 June 2016

Repeating Alarm for Every 2 Minutes in Android

activity_alarm_manager_example.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".AlarmManagerExample" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />

</RelativeLayout>

AlarmManagerExample.Java
package com.example.alarmmanagerexample;

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) {}
}
}


alarm.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/stopAlarm"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Stop Alarm" />

</LinearLayout>

RingAlaram.Java

package com.example.alarmmanagerexample;

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

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    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>

Output




No comments:

Post a Comment

Ads Inside Post