每周重复闹铃

时间:2015-07-23 13:26:59

标签: android alarmmanager timepicker

我正在开发一个小型的Android广播应用程序。该应用程序从互联网获取广播流并通过Media Player播放。一切正常,但我的下一个问题是在同一个应用程序中添加警报。闹钟将在特定日期(白天,小时,分钟)启动收音机。 警报看起来与Android官方闹钟相同。 布局是: 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>

并且是片段(标签)的alarm.java 已更新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);

    }
}

现在我的目标是在用户声明时每天,每小时,每分钟重复一次。此外,它应该保持在TextView当天和用户设置它的时间。 +我正在使用this link来指导自己。

更新2# 创建了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();
    }
}

将接收器添加到AndroidManifest.xml

<receiver android:name=".AlarmReceiver"></receiver>

2 个答案:

答案 0 :(得分:0)

我编写了类似的功能。 我通过搜索当前启动警报的下一个警报时间并在AlarmManager上开始新的意图来解决这个问题。

因此,每个警报都会在onStartCommand重启。 我的警报模型有一个类似getNextAlarmTime()的方法来获取下一个时间戳来执行。

也许这会给你一些新鲜的想法。

答案 1 :(得分:-1)

您是否可以将上一个闹钟日期存储在Apps共享偏好设置中,然后在触发闹钟时将值增加1天并重新保存值?