title: Android AlarmManager Start a service with ringtone
body: <p>I am working on Alarm Manager. I used a service to start and will ring a tone. But the service is not starting. Can anyone suggest about service, Pending Intent and identify the error in my code. The code is copied from net.</p>
  
  <p>This is my AndroidScheduledActivity.java</p>
  
  <pre><code>public class AndroidScheduledActivity extends Activity {
  
      private PendingIntent pendingIntent;
  
      /** Called when the activity is first created. */
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
  
          super.onCreate(savedInstanceState);
  
          setContentView(R.layout.activity_main);
  
          Button buttonStart = (Button) findViewById(R.id.startalarm);
  
          Button buttonCancel = (Button) findViewById(R.id.cancelalarm);
  
          buttonStart.setOnClickListener(new Button.OnClickListener() {
  
              @Override
              public void onClick(View arg0) {
  
                  // TODO Auto-generated method stub
  
                  Intent myIntent = new Intent(AndroidScheduledActivity.this,
                          MyAlarmService.class);
  
                  pendingIntent = PendingIntent.getService(AndroidScheduledActivity.this,
                          0, myIntent, 0);
  
                  AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
  
                  Calendar calendar = Calendar.getInstance();
  
                  calendar.setTimeInMillis(System.currentTimeMillis());
  
                  calendar.add(Calendar.SECOND, 10);
  
                  alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                          pendingIntent);
  
                  Toast.makeText(AndroidScheduledActivity.this, "Start Alarm",
                          Toast.LENGTH_LONG).show();
                  Log.d("Alarm", "Alarm Started");
  
              }
          });
  
          buttonCancel.setOnClickListener(new Button.OnClickListener() {
  
              @Override
              public void onClick(View arg0) {
  
                  // TODO Auto-generated method stub
  
                  AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
  
                  alarmManager.cancel(pendingIntent);
  
                  // Tell the user about what we did.
  
                  Toast.makeText(AndroidScheduledActivity.this, "Cancel!",
                          Toast.LENGTH_LONG).show();
  
              }
          });
  
      }
  }
  </code></pre>
  
  <p>And this is my MyAlarmService.java</p>
  
  <pre><code>public class MyAlarmService extends Service {
  
      @Override
      public void onCreate() {
  
          // TODO Auto-generated method stub
  
          Toast.makeText(this, "MyAlarmService.onCreate()", Toast.LENGTH_LONG).show();
  
      }
  
      @Override
      public IBinder onBind(Intent intent) {
  
          // TODO Auto-generated method stub
  
          Toast.makeText(this, "MyAlarmService.onBind()", Toast.LENGTH_LONG).show();
  
          return null;
  
      }
  
      @Override
      public void onDestroy() {
  
          // TODO Auto-generated method stub
  
          super.onDestroy();
  
          Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG).show();
  
      }
  
      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
  
          Log.d("AlarmService", "Alarm Starting to ring");
  
          Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
          MediaPlayer mp = MediaPlayer.create(getBaseContext(), alert);
          mp.setVolume(100, 100);
          mp.start();
          mp.setOnCompletionListener(new OnCompletionListener() {
  
              @Override
              public void onCompletion(MediaPlayer mp) {
                  mp.release();
              }
          });
  
          Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
          vibrator.vibrate(400);
  
          Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG).show();
  
          return Service.START_STICKY;
  
      }
  
      @Override
      public boolean onUnbind(Intent intent) {
  
          // TODO Auto-generated method stub
  
          Toast.makeText(this, "MyAlarmService.onUnbind()", Toast.LENGTH_LONG).show();
  
          return super.onUnbind(intent);
  
      }
  
  }
  </code></pre>
  
  <p>And my menifest.xml file</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.alarm_basic"
      android:versionCode="1"
      android:versionName="1.0" &gt;
  
      &lt;uses-sdk
          android:minSdkVersion="8"
          android:targetSdkVersion="17" /&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" &gt;
          &lt;activity
              android:name="com.example.alarm_basic.AndroidScheduledActivity"
              android:label="@string/app_name" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
          &lt;service android:name=".MyAlarmService" /&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>