如何将警报值传递给警报接收器

时间:2014-04-18 08:12:33

标签: java android android-alarms

我尝试开发一个示例警报应用程序。我在Google和SC上搜索过,大多数例子让我感到困惑。我已经完成了我的代码,但为什么它无法将警报值传递给Alarm receiver

请帮帮我。谢谢你的关注。

这是我的代码。

public class ReminderFragment extends Fragment {

    Button buttonstartSetDialog;
    TextView txt_time;

    Context ctx;

    final static int RQS_1 = 1;

    public ReminderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.fragment_reminder, container, false);

        txt_time = (TextView) v.findViewById(R.id.txt_time);

        buttonstartSetDialog = (Button) v.findViewById(R.id.startSetDialog);
        buttonstartSetDialog.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // textAlarmPrompt.setText("");
                showTimePicker();
            }
        });

        return v;
    }

    private void showTimePicker() {
        // DatePickerFragment date = new DatePickerFragment();
        TimePickerFragment time = new TimePickerFragment();

        Calendar calendar = Calendar.getInstance();
        Calendar calSet = (Calendar) calendar.clone();

        Bundle args = new Bundle();

        args.putInt("hour", calendar.HOUR_OF_DAY);
        args.putInt("month", calendar.get(Calendar.MONTH));
        args.putInt("minute", calendar.get(Calendar.MINUTE));
        time.setArguments(args);

        time.setCallBack(ontime);
        time.show(getFragmentManager(), "Time Picker");

    }

    OnTimeSetListener ontime = new OnTimeSetListener() {

        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            // TODO Auto-generated method stub
            txt_time.setText(String.valueOf(hourOfDay) + ":" + String.valueOf(minute));

            Intent intent = new Intent(getActivity(), AlarmReceiver.class);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), RQS_1, intent, 0);
            AlarmManager alarmManager = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
            alarmManager.set(AlarmManager.RTC_WAKEUP, txt_time.getTimeInMillis(), pendingIntent);

        }
    };

}

AlarmReceiver.java

public class AlarmReceiver extends BroadcastReceiver {

    private MediaPlayer mMediaPlayer;

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Time is up!!!!.", Toast.LENGTH_LONG).show();
        // Vibrate the mobile phone
        Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
        vibrator.vibrate(2000);

    }
}

TimePickerFragment.java

public class TimePickerFragment extends DialogFragment {

    OnTimeSetListener onTimeSet;

    public TimePickerFragment() {
    }

    public void setCallBack(OnTimeSetListener ontime) {
        onTimeSet = ontime;
    }

    @SuppressLint("NewApi")
    private int hour, minute;

    public void setArguments(Bundle args) {
        super.setArguments(args);
        hour = args.getInt("hour");
        minute = args.getInt("minute");

    }

    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new TimePickerDialog(getActivity(), onTimeSet, hour, minute, false);
    }
}

3 个答案:

答案 0 :(得分:0)

通过

传递值

intent .putExtra(“test”,“ValueReceived”);

然后在onReceive()中通过

获取值

intent.getStringExtra( “测试”)

答案 1 :(得分:0)

尝试

OnTimeSetListener ontime = new OnTimeSetListener() 
{

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        // TODO Auto-generated method stub
        txt_time.setText(String.valueOf(hourOfDay) + ":" + String.valueOf(minute));

        Intent intent = new Intent(getActivity(), AlarmReceiver.class);

        intent.putExtra("time_value",String.valueOf(hourOfDay) + " : " + String.valueOf(minute));

        PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), RQS_1, intent, 0);
        AlarmManager alarmManager = (AlarmManager)ctx.getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, txt_time.getTimeInMillis(), pendingIntent);
    }
};

答案 2 :(得分:0)

您需要以长值(毫秒)转换所选时间

使用以下代码执行此操作。

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.AM_PM, Calendar.PM); // set AM or PM
long timeInMillis = calendar.getTimeInMillis();

然后在

中传递此值timeInMillis
alarmManager.set(AlarmManager.RTC_WAKEUP, timeInMillis, pendingIntent);