时间选择器设置闹钟不工作,任何替代修复



目标是在从TimePicker中选择时间并按下ImageButton进行确认时设置Alarm。然而,当按下按钮设置警报时,什么也没发生!

活动的Java代码是:

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 AlarmReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {

    //this will sound the alarm tone
    //this will sound the alarm once, if you wish to
    //raise alarm in loop continuously then use MediaPlayer and 
setLooping(true)
    Uri alarmUri = 
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
    if (alarmUri == null) {
        alarmUri = 
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    }
    Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);
    ringtone.play();
    //this will send a notification message
    ComponentName comp = new ComponentName(context.getPackageName(),
            AlarmService.class.getName());
    startWakefulService(context, (intent.setComponent(comp)));
    setResultCode(Activity.RESULT_OK);
}
}

,所用服务的代码为:

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!任何帮助都会很感激。

有两件事会成为潜在的问题:

  1. 设置闹钟时使用AlarmManager.RTC_WAKEUP,否则设备不会从睡眠状态唤醒来触发闹钟
  2. 如果选择的时间当前时间之前,请确保适当调整Calendar对象。当您调用Calendar.getInstance()时,您将获得一个Calendar对象,该对象被设置为设备的区域设置和时区的当前TOD。如果你在选择器中选择了在当前TOD之前的时间,那么闹钟将不会触发,因为它已经超过了今天。

还有一件事你需要注意:从KitKat开始,闹钟默认是不精确的。因此,如果您需要闹钟在您指定的精确的时间响起,则需要为KK或更高版本的设备使用AlarmManager.setExact()方法。

相关内容

最新更新