gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 14:56:26 25 4
gpt4 key购买 nike

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

请帮助我。谢谢您的关心。

这是我的代码。

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

最佳答案

传递值

intent .putExtra("测试", "ValueReceived");

然后在onReceive()中通过

获取值

intent.getStringExtra("测试")

关于java - 如何将警报值传递给警报接收器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23150104/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com