Practical:-10: Aim: Write A Mobile Application That Create Alarm Clock. Code
Practical:-10: Aim: Write A Mobile Application That Create Alarm Clock. Code
Practical:-10: Aim: Write A Mobile Application That Create Alarm Clock. Code
Code
activity_main.xml
<TimePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/timePicker"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/alarm_state"
android:layout_marginTop="5dp"
android:textSize="18dp"
android:text="Start"
android:id="@+id/alarm_on"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/alarm_state"
android:layout_marginTop="5dp"
android:textSize="18dp"
android:text="Stop"
android:id="@+id/alarm_off"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:textSize="25dp"
android:text="Did you set the alarm?"
android:id="@+id/alarm_state"
android:layout_below="@+id/timePicker"
android:layout_centerHorizontal="true" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18dp"
android:id="@+id/spinner"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
MainActivity.java
package comjoshsibayan.github.alarm;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.icu.util.Calendar;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
AlarmManager alarm_manager;
TimePicker alarm_timepicker;
TextView alarm_state;
Context context;
PendingIntent pending_intent;
int sound_select;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final AlarmManager alarm_manager = (AlarmManager)
getSystemService(ALARM_SERVICE);
final TimePicker alarm_timepicker = (TimePicker) findViewById(R.id.timePicker);
final TextView alarm_state = (TextView) findViewById(R.id.alarm_state);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
Button alarm_on = (Button) findViewById(R.id.alarm_on);
alarm_on.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
calendar.set(Calendar.HOUR_OF_DAY, alarm_timepicker.getHour());
calendar.set(Calendar.MINUTE, alarm_timepicker.getMinute());
my_intent.putExtra("sound_choice", sound_select);
pending_intent = PendingIntent.getBroadcast
(MainActivity.this, 0, my_intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarm_manager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
pending_intent);
}
});
alarm_manager.cancel(pending_intent);
my_intent.putExtra("sound_choice", sound_select);
sendBroadcast(my_intent);
}
});
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}
AlarmReciver.java
package comjoshsibayan.github.alarm;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
context.startService(service_intent);
}
}
RingtonePlayingService.java
package comjoshsibayan.github.alarm;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
import java.util.Random;
/**
* Created by Josh on 8/31/2016.
*/
public class RingtonePlayingService extends Service {
MediaPlayer media_song;
int startId;
boolean isRunning;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
NotificationManager notify_manager =
(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Intent intent_main_activity = new Intent(this.getApplicationContext(), MainActivity.class);
PendingIntent pending_intent_main_activity =
PendingIntent.getActivity(this, 0, intent_main_activity, 0);
Notification notify_popup =
new Notification.Builder(this).setContentTitle("An alarm is going off!")
.setContentText("Click me!")
.setContentIntent(pending_intent_main_activity)
.setAutoCancel(true).build();
assert state != null;
switch (state) {
case "alarm on":
startId = 1;
break;
case "alarm off":
startId = 0;
Log.e("Start ID is ", state);
break;
default:
startId = 0;
break;
}
this.isRunning = true;
this.startId = 0;
notify_manager.notify(0, notify_popup);
if(sound_id == 0) {
if(sound_number == 1) {
media_song = MediaPlayer.create(this, R.raw.catalina_wine_mixer);
media_song.start();
} else if(sound_id == 2) {
media_song = MediaPlayer.create(this, R.raw.inclement_weather);
media_song.start();
} else if(sound_id == 3) {
media_song = MediaPlayer.create(this, R.raw.johnny_hopkins);
media_song.start();
} else if(sound_id == 4) {
media_song = MediaPlayer.create(this, R.raw.lightning_bolt);
media_song.start();
} else if(sound_id == 5) {
media_song = MediaPlayer.create(this, R.raw.robert_better_not_get_in_my_face);
media_song.start();
} else {
media_song = MediaPlayer.create(this, R.raw.butt_buddy);
media_song.start();
}
} else if(sound_id == 1) {
media_song = MediaPlayer.create(this, R.raw.catalina_wine_mixer);
media_song.start();
} else if(sound_id == 2) {
media_song = MediaPlayer.create(this, R.raw.inclement_weather);
media_song.start();
} else if(sound_id == 3) {
media_song = MediaPlayer.create(this, R.raw.johnny_hopkins);
media_song.start();
} else if(sound_id == 4) {
media_song = MediaPlayer.create(this, R.raw.lightning_bolt);
media_song.start();
} else if(sound_id == 5) {
media_song = MediaPlayer.create(this, R.raw.robert_better_not_get_in_my_face);
media_song.start();
} else {
media_song = MediaPlayer.create(this, R.raw.butt_buddy);
media_song.start();
}
media_song.stop();
media_song.reset();
this.isRunning = false;
this.startId = 0;
this.isRunning = false;
startId = 0;
this.isRunning = true;
this.startId = 1;
} else {
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
this.isRunning = false;
}
Output