0

我有一个关于alarmmanager的问题,我之前制作过简单的警报应用程序并且它成功了,但我尝试用更多功能再次制作它。但是我的警报管理器的未决意图永远不会被调用,所以我的警报永远不会响起。这是我在选中时使用工具按钮调用该函数时的代码

public void onClick(View v) {
    if(toggleButton.isChecked())
    {
        sts = 1;
        alarmDb = new TorasanDatabaseAdapter(context);
        alarmDb.open();
        alarmDb.updateAlarm(alarm._id,sts);
        alarmDb.close();
        Alarm.talarm.setText("aaaaaaaa");
        Intent alarmIntent = new Intent(context, MyAlarmService.class);
        alarmIntent.putExtra("id", alarm._id);
        alarmIntent.putExtra("sound", alarm.sound);
        PendingIntent pendingAlarmIntent = PendingIntent.getService(context, alarm._id, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT );
        AlarmManager alarmManager = (AlarmManager)context.getSystemService(context.ALARM_SERVICE);
        Calendar AlarmCal = Calendar.getInstance();
        int currentstatus = sts;
        alarm.status = currentstatus;
        Log.i("ID", " alarm hour: " + alarm.hour + " minute: " + alarm.minute+" status:"+alarm.status);
        AlarmCal.setTimeInMillis(System.currentTimeMillis());
        AlarmCal.set(Calendar.HOUR_OF_DAY, alarm.hour);  // set user selection
        AlarmCal.set(Calendar.MINUTE, alarm.minute);        // set user selection
        AlarmCal.set(Calendar.SECOND, 0);
        alarmManager.set(AlarmManager.RTC_WAKEUP, AlarmCal.getTimeInMillis(),pendingAlarmIntent);
    }

MyAlarmService.java

public class MyAlarmService extends Service {

   public void onCreate() {
      Toast.makeText(this, "MyAlarmService.onCreate()", Toast.LENGTH_SHORT).show();
   }

   public IBinder onBind(Intent intent) {
      return null;
   }

   public void onDestroy() {
      super.onDestroy();
      Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_SHORT).show();
   }

   public void onStart(Intent intent, int startId) {
      super.onStart(intent, startId);
      Bundle extras = intent.getExtras();
      String sound = extras.getString("sound");
      int hour = extras.getInt("hour");
      int minute = extras.getInt("minute");
      int id = extras.getInt("id");
      int status = extras.getInt("status");
      int statusdone = extras.getInt("statusdone");
      int statusdelete = extras.getInt("statusdelete");  
      Toast.makeText(this, "asadsadasdad", Toast.LENGTH_SHORT).show();
      Log.i("ID"," H="+hour+" M="+minute);
      Intent alertIntent = new Intent();
      alertIntent.setClass(this,DisplayNotifications.class);
      alertIntent.putExtra("sound", sound);
      alertIntent.putExtra("id", id);
      alertIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      startActivity( alertIntent );       
   }
}
4

1 回答 1

0

您传递给 AlarmManager 的 PendingIntent 必须供 BroadcastReceiver 使用。您已经传递了一个 PendingIntent 供服务使用。

AlarmManager 不会为您启动服务。它所要做的就是发送一个广播意图。

编辑:添加代码示例:

代替

PendingIntent.getService()

PendingIntent.getBroadcast()
于 2012-06-28T09:57:43.263 回答