2

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>
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
sharp
  • 1,191
  • 14
  • 39
  • It seems you're on the right track, I'm not sure what your problem really is. You can store the date set by the user in your `SharedPreferences` for instance and when you're retrieving your `Activity`/`Fragment`, restore the value and set it in your `TextView` – Edson Menegatti Jul 23 '15 at 13:30
  • I hope it's on the right track, I have been trying this for days, still nothing. My problem is the button who will save it all. How to make him? – sharp Jul 23 '15 at 13:34
  • Why do you get the day info , if you are just going to trigger it everyday? Just use 'HH:mm:SS.ms' – Alpaslan Jul 23 '15 at 13:39
  • That's my mistake, old code, I'll delete it. – sharp Jul 23 '15 at 13:40
  • First try to understand how the `SharedPreferences` work and add some mock content. Afterwards just create a regular button which will get a reference to the `SharedPreferences` and apply some changes to it. – Edson Menegatti Jul 23 '15 at 13:40
  • Instead of `1 * 60 * 60 * 1000` use `AlarmManager.INTERVAL_DAY` -- there are other params too, you can play around. Also one more word of caution, you must set the alarm again after boot. As restarting the device will clear of all alarms. Check a complete [working](http://stackoverflow.com/questions/21461191/alarmmanager-fires-alarms-at-wrong-time/21461246#21461246) example – Skynet Jul 23 '15 at 14:09

2 Answers2

0

I programmed a similar function. I solved this by searching the next alarm-time of current-started-alarm and start new Intent over AlarmManager.

So each alarm restart himself in onStartCommand. My alarm-model has a method like getNextAlarmTime() to get next timestamp to execute.

Maybe this gives you some fresh ideas.

JoGe
  • 872
  • 10
  • 26
-1

Could you not just store the previous alarm date in the Apps shared preferences, then when the alarm is triggered just increment the value by 1 day and re save the value?