如何通过报警值,报警接收机(How to pass the Alarm value to Alarm

2019-10-19 22:19发布

我试图开发一个示例应用程序报警。 我搜索谷歌和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);
    }
}

Answer 1:

由值传递

意图.putExtra( “测试”, “ValueReceived”);

然后在的onReceive()获取的价值

intent.getStringExtra( “测试”)



Answer 2:

与尝试

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


Answer 3:

你需要选择的时间转换成long值(毫秒)

使用下面的代码来做到这一点。

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


文章来源: How to pass the Alarm value to Alarm Receiver