0

因此,我正在尝试在我的 Android 应用程序中实现此站点的重复功能 - http://www.java2s.com/Code/Android/Core-Class/Exampleofschedulingoneshotandrepeatingalarms.htm - 但由于某种原因它没有调用该类是代码摘录。

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

4

1 回答 1

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>
于 2012-12-30T05:04:29.023 回答