2

AlarmService sample error building can't find AlarmService_Service.class. What am I missing?

Code:

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();
    }
  };
}
dda
  • 6,030
  • 2
  • 25
  • 34
Java Review
  • 427
  • 2
  • 9
  • 20

1 Answers1

1

You have refered the AlarmService_Service.class in the intent. Doing this android is expecting the AlarmService_Service class in your source code. I think that class is not in your code.

You have 2 options, either to define Activity AlarmService_Service (also define the ref. in manifest) or to refer to this link http://android-er.blogspot.com/2010/10/simple-example-of-alarm-service-using.html

This should work

Rohit Mandiwal
  • 10,258
  • 5
  • 70
  • 83