I'm working on a small android radio application. The app is fetching radio stream from internet and plays it over Media Player
. That all works fine, but my next problem is adding alarm in that same app. The alarm will start the radio on specific date (day,hour,minute).
The alarm looks the same as the Android official alarm clock.
The layout is:
alarm.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ff4379df">
<TimePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/timePicker"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<LinearLayout
android:weightSum="7"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/timePicker"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<ToggleButton
android:textOn="P"
android:textOff="P"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/chk_monday" />
<ToggleButton
android:textOn="U"
android:textOff="U"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/chk_tuesday" />
<ToggleButton
android:textOn="S"
android:textOff="S"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/chk_wednesday" />
<ToggleButton
android:textOn="C"
android:textOff="C"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/chk_thursday" />
<ToggleButton
android:textOn="P"
android:textOff="P"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/chk_friday" />
<ToggleButton
android:textOn="S"
android:textOff="S"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/chk_sat" />
<ToggleButton
android:textOn="N"
android:textOff="N"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/chk_sunday" />
</LinearLayout>
</RelativeLayout>
And alarm.java who is a Fragment (tab) Updated 2#
public class alarm extends Fragment {
TimePicker timePicker;
ToggleButton chk_monday;
ToggleButton chk_tuesday;
ToggleButton chk_wednesday;
ToggleButton chk_thursday;
ToggleButton chk_friday;
ToggleButton chk_sat;
ToggleButton chk_sunday;
TextView textView;
final static int RQS_1 = 1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.alarm, container, false);
final ToggleButton chk_monday = (ToggleButton) rootView.findViewById(R.id.chk_monday);
ToggleButton chk_tuesday = (ToggleButton) rootView.findViewById(R.id.chk_tuesday);
ToggleButton chk_wednesday = (ToggleButton) rootView.findViewById(R.id.chk_wednesday);
ToggleButton chk_thursday = (ToggleButton) rootView.findViewById(R.id.chk_thursday);
ToggleButton chk_friday = (ToggleButton) rootView.findViewById(R.id.chk_friday);
ToggleButton chk_sat = (ToggleButton) rootView.findViewById(R.id.chk_sat);
ToggleButton chk_sunday = (ToggleButton) rootView.findViewById(R.id.chk_sunday);
TimePicker timePicker = (TimePicker) rootView.findViewById(R.id.timePicker);
timePicker.setIs24HourView(true);
/**/
Calendar calSet = Calendar.getInstance();
int hour = calSet.get(Calendar.HOUR_OF_DAY);
int min = calSet.get(Calendar.MINUTE);
timePicker.setCurrentHour(hour);
timePicker.setCurrentMinute(min);
/**/
if (chk_monday.isChecked()) {
forday(2);
} if (chk_tuesday.isChecked()) {
forday(3);
} if (chk_wednesday.isChecked()) {
forday(4);
} if (chk_thursday.isChecked()) {
forday(5);
}if (chk_friday.isChecked()) {
forday(6);
} if (chk_sat.isChecked()) {
forday(7);
}if (chk_sunday.isChecked()) {
forday(1);
}
return rootView;
}
public void forday (int week) {
Calendar calSet = Calendar.getInstance();
calSet.set(Calendar.DAY_OF_WEEK, week);
calSet.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());
calSet.set(Calendar.MINUTE, timePicker.getCurrentMinute());
calSet.set(Calendar.SECOND, 0);
calSet.set(Calendar.MILLISECOND, 0);
Intent intent = new Intent(getActivity().getBaseContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity().getApplicationContext(), RQS_1, intent, 0);
AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calSet.getTimeInMillis(), 1 * 60 * 60 * 1000, pendingIntent);
}
}
Now my goal is to make it repeat every day,hour,minute when user declares it. Plus it should stay in an TextView
the day and the time when the user set it.
+ I was using this link to guide myself a little bit.
Update 2# Created AlarmReceiver.java
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.e("onReceive", "Recived!");
Toast.makeText(context, "OnRecive!", Toast.LENGTH_SHORT).show();
}
}
Added receiver to AndroidManifest.xml
<receiver android:name=".AlarmReceiver"></receiver>