Java 设置报警的时间选择器不工作,是否有其他修复方法?
目标是在从计时器中拾取时间并按下用于确认的ImageButton时设置报警。但是,当按下按钮设置报警时,什么也没有发生 该活动的Java代码为:Java 设置报警的时间选择器不工作,是否有其他修复方法?,java,android,Java,Android,目标是在从计时器中拾取时间并按下用于确认的ImageButton时设置报警。但是,当按下按钮设置报警时,什么也没有发生 该活动的Java代码为: public class Time_Date extends FragmentActivity { private PendingIntent pendingIntent; private TimePicker alarmPicker; AlarmManager alarmManager; @Override protected void onC
public class Time_Date extends FragmentActivity {
private PendingIntent pendingIntent;
private TimePicker alarmPicker;
AlarmManager alarmManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_time__date);
alarmPicker = (TimePicker)findViewById(R.id.time);
alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
}
public void fireAlarm (View view)
{
ImageButton alarmPower = (ImageButton)findViewById(R.id.setAlarm);
alarmPower.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,
alarmPicker.getCurrentHour());
calendar.set(Calendar.MINUTE,
alarmPicker.getCurrentMinute());
Intent myIntent = new Intent(Time_Date.this,
AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(Time_Date.this,
0, myIntent, 0);
alarmManager.set(AlarmManager.RTC,
calendar.getTimeInMillis(), pendingIntent);
}
});
}
XML布局代码是:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#FFFFFF"
tools:context="zyia.alarm.zyia.zyia.Time_Date">
<ImageView
android:background="@drawable/top_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TimePicker
android:id="@+id/time"
android:layout_gravity="center"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageButton
android:layout_marginTop="15dp"
android:id="@+id/setAlarm"
android:onClick="fireAlarm"
android:layout_gravity="center"
android:layout_width="200dp"
android:layout_height="150dp" />
</LinearLayout>
所用服务的代码为:
public class AlarmService extends IntentService {
private NotificationManager alarmNotificationManager;
public AlarmService() {
super("AlarmService");
}
@Override
public void onHandleIntent(Intent intent) {
sendNotification("Wake Up! Wake Up!");
}
private void sendNotification(String msg) {
Log.d("AlarmService", "Preparing to send notification...: " + msg);
alarmNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, Time_Date.class), 0);
NotificationCompat.Builder alamNotificationBuilder = new
NotificationCompat.Builder(
this).setContentTitle("Alarm").setSmallIcon(R.drawable.ic_launcher)
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg);
alamNotificationBuilder.setContentIntent(contentIntent);
alarmNotificationManager.notify(1, alamNotificationBuilder.build());
Log.d("AlarmService", "Notification sent.");
}
}
我已经试了差不多一天了,找到了这个bug!任何帮助都将不胜感激 有两件事会成为潜在的问题:
AlarmManager.RTC_WAKEUP
,否则设备将无法从睡眠状态唤醒以触发闹钟Calendar.getInstance()
时,您将得到一个Calendar
对象,该对象被设置为设备的区域设置和时区的当前TOD。如果您在选择器中选择了当前TOD之前的时间,则不会触发警报,因为它将已经超过今天还有一件事你需要注意:从KitKat开始,默认情况下警报是不精确的。因此,如果您需要在指定的确切时间发出警报,那么对于KK或更高版本的设备,您需要使用方法
AlarmManager.setExact()
。第一件事是可以的,但是如何适当调整日历对象?您必须查看选择器的时间是否在当前TOD之前。如果是,您需要在设置闹钟之前在日历
对象中添加一天(使闹钟在明天选定的时间响起)
public class AlarmService extends IntentService {
private NotificationManager alarmNotificationManager;
public AlarmService() {
super("AlarmService");
}
@Override
public void onHandleIntent(Intent intent) {
sendNotification("Wake Up! Wake Up!");
}
private void sendNotification(String msg) {
Log.d("AlarmService", "Preparing to send notification...: " + msg);
alarmNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, Time_Date.class), 0);
NotificationCompat.Builder alamNotificationBuilder = new
NotificationCompat.Builder(
this).setContentTitle("Alarm").setSmallIcon(R.drawable.ic_launcher)
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg);
alamNotificationBuilder.setContentIntent(contentIntent);
alarmNotificationManager.notify(1, alamNotificationBuilder.build());
Log.d("AlarmService", "Notification sent.");
}
}