我需要你的帮助。我遇到了这个问题。警报正常工作。但是,当设备重新启动,并且存储在db中的指定时间是过去(下午3点)且当前时间是下午4点时,我该如何检查以防止立即触发警报?
Docs说:
如果声明的触发时间是过去的,则会立即触发警报。 这是我到目前为止所尝试的内容:
<!doctype html>
<html lang="en">
<head>
....
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
..
</head>
<body>
<div id="myslider"></div>
<script>
$( "#myslider" ).slider();
</script>
<script>
var circle;
function init() {
var mapCenter = new google.maps.LatLng( 30.356625899999994, 78.08492950000004);
var map = new google.maps.Map(document.getElementById('map'), {
'zoom':12 ,
'center': mapCenter,
'mapTypeId': google.maps.MapTypeId.ROADMAP
});
// Create a draggable marker which will later on be binded to a
// Circle overlay.
var marker = new google.maps.Marker({
map: map,
position: new google.maps.LatLng(30.356625899999994, 78.08492950000004),
draggable: true,
title: 'Drag me!'
});
// Add a Circle overlay to the map.
circle = new google.maps.Circle({
map: map,
radius: 5000 // 5 km
});
// Since Circle and Marker both extend MVCObject, you can bind them
// together using MVCObject's bindTo() method. Here, we're binding
// the Circle's center to the Marker's position.
// http://code.google.com/apis/maps/documentation/v3/reference.html#MVCObject
circle.bindTo('center', marker, 'position');
}
$("#myslide").slider({
orientation: "vertical",
range: "min",
max: 3000,
min: 100,
value: 500,
slide: function(event, ui) {
updateRadius(circle, ui.value);
}
});
function updateRadius(circle, rad) {
circle.setRadius(rad);
}
// Register an event listener to fire when the page finishes loading.
google.maps.event.addDomListener(window, 'load', init);
</script>
</body>
</html>
AndroidManifest:
class DeviceBootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Check if successful reboot
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
SharedPreferences shared = context.getSharedPreferences(Constants.PREF_NAME, Context.MODE_PRIVATE);
// Check if daily notification is enabled
// If yes, call NotificationPublisher to communicate with NotificationAlarmService
if(shared.getBoolean(Constants.KEY_IS_DAILY_NOTIFIED, false)) {
DBHelper dbHelper = new DBHelper(context);
DBConnector.dbConnect(dbHelper);
int DAILY_NOTIFICATION_ID = Constants.DAILY_VERSE_NOTIFICATION_1_ID;
ArrayList<Notification> notificationList = dbHelper.getNotifications();
Log.e("", "notificationList: " + notificationList.size());
for(Notification obj : notificationList) {
Calendar datetime = Calendar.getInstance();
datetime.set(Calendar.HOUR_OF_DAY, obj.getHourOfDay());
datetime.set(Calendar.MINUTE, obj.getMinute());
datetime.set(Calendar.SECOND, 0);
Calendar now = Calendar.getInstance();
if (now.after(datetime)) {
datetime.add(Calendar.DATE, now.get(Calendar.DATE) + 1);
}
Log.e("BOOT RECEIVER", "" + obj.getHourOfDay() + ":" + obj.getMinute());
Intent myIntent = new Intent(context, NotificationPublisher.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
DAILY_NOTIFICATION_ID++,
myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
datetime.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
pendingIntent);
}
}
}
}
我的代码似乎有什么问题?我很感激任何帮助。谢谢!
答案 0 :(得分:1)
我设置&amp;像这样检查闹钟时间,它现在对我有用:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getCurrentHour());
calendar.set(Calendar.MINUTE, alarmTimePicker.getCurrentMinute());
Calendar now = Calendar.getInstance();
now.setTimeInMillis(System.currentTimeMillis());
if (calendar.before(now))
calendar.add(Calendar.DAY_OF_MONTH, 1);
答案 1 :(得分:0)
在间隔日设置重复警报的示例代码。
如果你想通过扩展Application类并编写代码来设置在onCreate里面的报警,你想要设置报警,如果你想设置报警。
public class MainActivity extends AppCompatActivity {
private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 15);
calendar.set(Calendar.MINUTE, 30);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, alarmIntent);
}
}
SampleBootReciver - 设备重启后设置闹钟
public class SampleBootReceiver extends BroadcastReceiver {
private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent1 = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent1, 0);
Calendar now = Calendar.getInstance();
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 15);
calendar.set(Calendar.MINUTE, 30);
if (calendar.before(now))
calendar.add(Calendar.DAY_OF_MONTH, 1);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, alarmIntent);
}
}
}
AlarmReceiver - 触发警报后执行操作。
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "I'm running", Toast.LENGTH_SHORT).show();
}
}
我只是在举杯祝酒。
最后是清单文件 -
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
包=&#34; com.bits.kevz.samplealarm&#34;&GT;
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".AlarmReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<receiver android:name=".SampleBootReceiver"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
我希望您运行上面的代码并检查onBootCompleted输出。