我正在尝试使用警报管理器在不同时间触发 3 个警报。这是我的代码(请注意,alarm1、alarm2、alarm3 是我代码中前面设置的三个日历对象):
AlarmNum=1;
new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
Intent myIntent = new Intent(MainActivity.this,
MyAlarmService.class);
pendingIntent = PendingIntent.getService(MainActivity.this, 0,
myIntent, 0);
if (AlarmNum == 1)
alarmManager.set(AlarmManager.RTC_WAKEUP,
alarm1.getTimeInMillis(), pendingIntent);
else if (AlarmNum == 2)
alarmManager.set(AlarmManager.RTC_WAKEUP,
alarm2.getTimeInMillis(), pendingIntent);
else
alarmManager.set(AlarmManager.RTC_WAKEUP,
alarm3.getTimeInMillis(), pendingIntent);
Toast.makeText(MainActivity.this, "Start Alarm",
Toast.LENGTH_LONG).show();
}
};
在上面的代码中,我启动了一个意图,它引发了下面给出的 MyAlarmService 类:
public class MyAlarmService extends Service {
MainActivity instance;
MediaPlayer mp;
@Override
public void onCreate() {
// TODO Auto-generated method stub
Toast.makeText(this, "MyAlarmService.onCreate()", Toast.LENGTH_LONG)
.show();
mp = MediaPlayer.create(this, R.raw.alarmtone);
instance = new MainActivity();
}
@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, "MyAlarmService.onStart()", Toast.LENGTH_LONG)
.show();
mp.start();
instance.setAlarmNum(instance.getAlarmNum() + 1);
}
@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
mp.release();
mp.reset();
Toast.makeText(this, "MyAlarmService.onUnbind()", Toast.LENGTH_LONG)
.show();
return super.onUnbind(intent);
}
我认为这里有问题,因为祝酒词永远不会出现,警报也不会出现。