Saturday 25 April 2015

Android Alarm Manager to set a Remainder with Notificationand Sound


Android Alarm Manager to set a Remainder with Notificationand Sound

Screen Shots:

 Coding:

AlarmActivity.java

package com.example.alarm_manager;
import android.app.Activity;
import android.os.Bundle;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

public class AlarmActivity extends Activity {
    /** Called when the activity is first created. */
    /* Author J M */
    TimePicker TimePicker;
    DatePicker DatePicker;
    Button Setalarm;

    TimePickerDialog timePickerDialog;

    final static int RQS_1 = 1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
  
        DatePicker =(DatePicker)findViewById(R.id.datePicker1);
        TimePicker=(TimePicker)findViewById(R.id.timePicker1);
        Calendar now = Calendar.getInstance();

        DatePicker.init(
          now.get(Calendar.YEAR),
          now.get(Calendar.MONTH),
          now.get(Calendar.DAY_OF_MONTH),
          null);
       
        TimePicker.setCurrentHour(now.get(Calendar.HOUR_OF_DAY));
        TimePicker.setCurrentMinute(now.get(Calendar.MINUTE));
       
        Setalarm = (Button) findViewById(R.id.Setalarm);
        Setalarm.setOnClickListener(new OnClickListener() {

            public void onClick(View arg0) {
                    Calendar current = Calendar.getInstance();
                   
                    Calendar cal = Calendar.getInstance();
                    cal.set(DatePicker.getYear(),
                            DatePicker.getMonth(),
                            DatePicker.getDayOfMonth(),
                      TimePicker.getCurrentHour(),
                      TimePicker.getCurrentMinute(),
                      00);
                   
                    if(cal.compareTo(current) <= 0){
                     //The set Date/Time already passed
                        Toast.makeText(getApplicationContext(),
                          "Invalid Date/Time",
                          Toast.LENGTH_LONG).show();
                    }else{
                     setAlarm(cal);
                    }
                   
                   }});
           
    }

   
    private void setAlarm(Calendar targetCal) {

        //
        Toast.makeText(AlarmActivity.this, "Alarm is set at " + targetCal.getTime(),
                Toast.LENGTH_LONG).show();
        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);

    }

}

 

AlarmReceiver.java

package com.example.alarm_manager;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Vibrator;
import android.sax.StartElementListener;
import android.telephony.SmsManager;
import android.widget.Toast;

public class AlarmReceiver extends BroadcastReceiver {
   
     @Override
    public void onReceive(Context context, Intent intent) {
       
         Toast.makeText(context, "Your Time is up!!!!!", Toast.LENGTH_LONG).show();
       Vibrator vib=(Vibrator)context.getSystemService(context.VIBRATOR_SERVICE);    //for Vibration
       vib.vibrate(2000);
      
       Intent i=new Intent(context,song.class);  //song class contain media song
         i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         context.startActivity(i);
       }
}

 

song.java

package com.example.alarm_manager;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.media.MediaPlayer;
import android.os.Bundle;

public class song extends Activity{
    MediaPlayer m;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.song);
        m=MediaPlayer.create(getApplicationContext(), R.raw.gaji);
        //create a folder raw inside res folder. and put 1 .mp3 song
        m.start();
      
        AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(song.this);                    
         dlgAlert.setTitle("Remainder !");
         dlgAlert.setMessage("Your time is up !");
         dlgAlert.setPositiveButton("OK",new DialogInterface.OnClickListener() {
         public void onClick(DialogInterface dialog, int whichButton) {

             m.stop();
             dialog.cancel();
         }
         });
         dlgAlert.show();
     }
}
 

 
Layout:

main.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"
    android:padding="20dp"    >

    <TextView
        android:id="@+id/alarmprompt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <TimePicker
        android:id="@+id/timePicker1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <DatePicker
        android:id="@+id/datePicker1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    

     <Button
        android:id="@+id/Setalarm"
        android:layout_width="117dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Set Alarm" />

</LinearLayout>


song.xml

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Welcome"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>


AndroidManifest.xml 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.alarm_manager"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.VIBRATE"/>
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".AlarmActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name="AlarmReceiver"></receiver>
        <activity android:name="song"></activity>
    </application>

</manifest>

 

Sunday 12 April 2015

Dil Ki Deal Dil Se

“I am participating in the #DilKiDealOnSnapdealactivity at BlogAdda in association with SnapDeal.”


Now a day’s buying a smart phone is the very difficult job. We have lot of choices, when we look at its features we feel want to buy all of them. But it is not at all possible. Finally we have to listen our heart what it tells. Trust me if you doing anything what your heart says, you will be happy with that.

Now from here my story starts
I planned to give a special gift to my brother. And most of the boys like new featured mobile phones. So I planned to gift a smart phone. I know my brother likes smart phone but I had lot of confusions which one I have to give. For that reason daily I am searching in the internet.  Comparing the phones and its features and asking with friends. At last short listed few branded phones.

Out of the few branded phones I have to select the one which is best. At that time I heard the voice of my heart and I selected Moto E smart phone. I ordered online and within 3 working days I got the product. Now I have to give the gift to my brother.

When I went near to my brother with the gift he was looking like shocked. He never thought I will give it to him. With the shocking expression he asked are you giving this to me. I said yes. Immediately he asked what is this. And he opened it. Ya he was fully surprised its Moto E.

At that movement he was totally happy. I realized that the selection which I did is perfect. At that point I came to know what ever we doing from heart it’s really worth and it increases the happiness of others along with our happiness. Because of my gift my brother was happy.

When the phone is switched on he took my photo. And that photo is still there in his phone. Whenever he calls me I remember the phone which I gifted him. And he too remembers me whenever he looks the phone. I gifted phone because, now a day’s mobile phones are very close. No one is going anywhere without mobile phone.

If anyone wants to gift a smart phone then go for Moto E. Its good and you will get in a reasonable price with the new high end features. I am mentioning this here because, I experienced lot of happiness.

So just listen from your heart and buy the product. No matters whether it is small or big. Money comes and goes but we get happiness only when we give others. So let’s forward one step and give gifts to your beloved ones. And feel the happiness and spread it. This is called #DilKiDealOnSnapdeal .

Share It

Followers