任何人都可以告诉我如何在Android应用程序中创建一个从当前时间开始的警报,它会在特定时间内每10分钟重复一次?
例如,根据不同情况,警报应重复的时间是2小时,另一种情况可能只是1:30小时,依此类推。
有没有办法做到这一点?
实际上我使用了从网上拿来的代码,如下所示:
public class MyAlarmService扩展Service {
@Override
public void onCreate() {
// TODO Auto-generated method stub
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(this, "MyAlarmService.onBind()", Toast.LENGTH_LONG)
.show();
return null;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG)
.show();
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
Toast.makeText(this, "...", Toast.LENGTH_LONG)
.show();
}
@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(this, "MyAlarmService.onUnbind()", Toast.LENGTH_LONG)
.show();
return super.onUnbind(intent);
}
}
答案 0 :(得分:0)
您可以使用BroadcastReceiver和PendingIntent。首先声明一个BroadcastReceiver:
public class EventAlarmReceiver extends BroadcastReceiver {
private long alarmTime;
@Override
public void onReceive(Context context, Intent intent1) {
//declare the new time for the alarm, you can use a time stamp, for example in the next hour
alarmTime = Calendar.getInstance().getTimeInMillis()+(60*60000);
//set an intent to the Receiver
Intent intent = new Intent(context, EventAlarmReceiver.class);
//set an PendingIntent to the Intent
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getApplicationContext(), 234324243, intent, 0);
//add to the System Alarm Manager
AlarmManager alarmManager = (AlarmManager) getSystemService(Activity.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, alarmTime, pendingIntent);
}
}
并在活动中启动闹钟:
Intent intent = new Intent(context, EventAlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Activity.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, time, pendingIntent);