我写了一个我设置闹钟的活动,并且它会在设定的时间醒来。但是当时间报警时,它没有开火。这是我的代码;
MainActivity.java
buttonEkle.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Yapilacaklar yapilacaklar = new Yapilacaklar(null, null, null,
null);
if (!checkBoxIsAlarm.isChecked()) {
yapilacaklar.setIsAlarm(Boolean.FALSE);
} else {
yapilacaklar.setIsAlarm(Boolean.TRUE);
Intent intentsOpen = new Intent(
YapilacakEklemeActivity.this, AlarmReceiver.class);
intentsOpen.setAction("com.mesutemre.alarm.ACTION");
pendingIntent = PendingIntent.getBroadcast(
YapilacakEklemeActivity.this, 111, intentsOpen, 0);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 10000, pendingIntent);
}
}
});
这是我的AlarmReceiver;
AlarmReceiver.java
public class AlarmReceiver extends BroadcastReceiver {
private final String SOMEACTION = "com.mesutemre.alarm.ACTION";
@Override
public void onReceive(Context context, Intent intent) {
generateNotification(context,"Hi how are you?");
String action = intent.getAction();
if (SOMEACTION.equals(action)) {
//do what you want here
generateNotification(context,"Hi how are you?");
}
}
@SuppressWarnings("deprecation")
private void generateNotification(Context context, String message) {
System.out.println(message+"++++++++++2");
int icon = R.drawable.javandroid;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
String subTitle = context.getString(R.string.app_name);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
}
我已经在manifest.xml上保存了接收器;
<receiver android:name=".AlarmReceiver">
<intent-filter>
<action android:name="com.mesutemre.alarm.ACTION" />
</intent-filter>
</receiver>
为什么警报没有在设定时间触发?我尝试过相同的代码,另一个应用程序工作正常。可能是什么原因?