在Android上没有调用警报

时间:2012-12-30 02:44:39

标签: android alarmmanager

因此,我尝试将此网站中的重复功能 - http://www.java2s.com/Code/Android/Core-Class/Exampleofschedulingoneshotandrepeatingalarms.htm - 实施到我的Android应用中,但由于某种原因,它不会调用该类,而是代码摘录。

public class MainActivity extends Activity implements SensorEventListener {

// Schedule repeating alarm

          Intent intent = new Intent(MainActivity.this, RepeatingAlarm.class);
          PendingIntent sender = PendingIntent.getBroadcast(MainActivity.this, 0,
              intent, 0);

          // We want the alarm to go off 30 seconds from now.
          long firstTime = SystemClock.elapsedRealtime();
          firstTime += 15 * 1000;

          // Schedule the alarm!
          AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
          am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime,
              15 * 1000, sender);

          // Tell the user about what we did.
            Toast.makeText(
                    getApplicationContext(),
                    "Scheduled", Toast.LENGTH_LONG)
                    .show();

}

//Schedule alarm for data upload
class RepeatingAlarm extends BroadcastReceiver {
      @Override
      public void onReceive(Context context, Intent intent) {
            Toast.makeText(
                    context,
                    "ALARMED!", Toast.LENGTH_LONG)
                    .show();
      }
}

如果它有助于完整代码在此处:https://gist.github.com/4410665

1 个答案:

答案 0 :(得分:0)

解决方案是采取:

//Schedule alarm for data upload
class RepeatingAlarm extends BroadcastReceiver {
      @Override
      public void onReceive(Context context, Intent intent) {
            Toast.makeText(
                    context,
                    "ALARMED!", Toast.LENGTH_LONG)
                    .show();
      }
}

并将其保存为RepeatingAlarm.java,基本上它是自己的类。然后进入mainfest文件并添加一些效果

<receiver android:name="RepeatingAlarm"></receiver>