How to pass the Alarm value to Alarm Receiver

血红的双手。 提交于 2019-12-24 12:30:14

问题


I tried to develop a sample Alarm Application. I was searched Google and SC, most of the examples confusing me. I have done with my code, but why it failed to pass the alarm value to the Alarm receiver.

Please help me. Thank you for your concern.

Here is my code.

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);
    }
}

回答1:


Pass the values by

intent .putExtra("test", "ValueReceived");

and then in onReceive() get the value by

intent.getStringExtra("test")




回答2:


Try with

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);
    }
};



回答3:


You need to convert the selected time in long value (milliseconds)

use the below code to do this.

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();

and then pass this value timeInMillis in

alarmManager.set(AlarmManager.RTC_WAKEUP, timeInMillis, pendingIntent);


来源:https://stackoverflow.com/questions/23150104/how-to-pass-the-alarm-value-to-alarm-receiver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!