我正在使用Fragment的应用程序,其中有一个带有时间选择器的提醒但我的ReminderPage类没有进入BroadcastReceiver类,我无法找出为什么会发生这种情况。
以下是RemiderPage.java
的代码。
public class ReminderPage extends android.support.v4.app.Fragment {
View view;
Button Setalarm;
TimePicker timePicker;
final static int RQS_1=1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
view= inflater.inflate(R.layout.fragment_reminder, container, false);
timePicker=(TimePicker)view.findViewById(R.id.timePicker);
Setalarm=(Button)view.findViewById(R.id.button8);
Calendar calendar=Calendar.getInstance();
timePicker.setCurrentHour(calendar.get(Calendar.HOUR_OF_DAY));
timePicker.setCurrentMinute(calendar.get(Calendar.MINUTE));
Setalarm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar cal = Calendar.getInstance();
cal.set(timePicker.getCurrentHour(), timePicker.getCurrentMinute(), 00);
setAlarm(cal);
}
private void setAlarm(Calendar targetcal) {
Toast.makeText(getContext(), "Alarm is set", Toast.LENGTH_LONG).show();
Intent intent = new Intent(getContext().getApplicationContext(),AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getContext().getApplicationContext(), RQS_1, intent, 0);
AlarmManager alarmmanager = (AlarmManager)getActivity().getSystemService(Context.ALARM_SERVICE);
alarmmanager.set(AlarmManager.RTC_WAKEUP, targetcal.getTimeInMillis(), pendingIntent);
}
});
return view;
}
}
这是AlarmReceiver.java
的代码。
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"Alarm received",Toast.LENGTH_LONG).show();
}
}
答案 0 :(得分:0)
请在manifest.xml文件的应用程序节点内注册您的接收器。
<receiver android:name=".AlarmReceiver" android:enabled="true">
</receiver>
请查看以下链接,了解更多信息。 http://developer.android.com/guide/topics/manifest/receiver-element.html
您也可以通过编程方式注册。请检查以下墨水。