2

AlarmService 示例错误构建找不到 AlarmService_Service.class。我错过了什么?

代码:

public class AlarmService extends Activity {
  private PendingIntent mAlarmSender;

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Create an IntentSender that will launch our service, to be scheduled
    // with the alarm manager.
    mAlarmSender = PendingIntent.getService(AlarmService.this, 0,
        new Intent(AlarmService.this, AlarmService_Service.class), 0);
    setContentView(R.layout.main);
    // Watch for button clicks.
    Button button = (Button) findViewById(R.id.Button01);
    button.setOnClickListener(mStartAlarmListener);
    button = (Button) findViewById(R.id.Button02);
    button.setOnClickListener(mStopAlarmListener);
  }

  private OnClickListener mStartAlarmListener = new OnClickListener() {

    public void onClick(View v) {
      // We want the alarm to go off 30 seconds from now.
      long firstTime = SystemClock.elapsedRealtime();
      // Schedule the alarm!
      AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
      am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime,
          3 * 1000, mAlarmSender);
      // Tell the user about what we did.
      Toast.makeText(AlarmService.this, "Repeating Scheduled",
          Toast.LENGTH_LONG).show();
    }
  };
  private OnClickListener mStopAlarmListener = new OnClickListener() {

    public void onClick(View v) {
      // And cancel the alarm.
      AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
      am.cancel(mAlarmSender);
      // Tell the user about what we did.
      Toast.makeText(AlarmService.this, "Repeating Unscheduled",
          Toast.LENGTH_LONG).show();
    }
  };
}
4

1 回答 1

1

您已在意图中引用了 AlarmService_Service.class。执行此 android 需要在您的源代码中使用 AlarmService_Service 类。我认为该类不在您的代码中。

您有 2 个选项,可以定义 Activity AlarmService_Service (也在清单中定义参考)或参考此链接http://android-er.blogspot.com/2010/10/simple-example-of-alarm-service -using.html

这应该工作

于 2011-02-19T16:51:38.327 回答