views:

42

answers:

0

The AlertDialog can show normally, but why the music service does not start?

package com.commonware.android.AnalogClock;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.media.*;

public class AlarmAlert extends Activity
{
  public MediaPlayer myPlayer = new MediaPlayer();
      @Override
      protected void onCreate(Bundle savedInstanceState) 
      {
         super.onCreate(savedInstanceState);
         startService(new Intent("com.commonware.android.AnalogClock.START_AUDIO_SERVICE"));
         new AlertDialog.Builder(AlarmAlert.this)
        .setIcon(R.drawable.clock)
       .setTitle("Alarm Clock!!")
       .setMessage("Get up!!!")
       .setPositiveButton("Close it!",
       new DialogInterface.OnClickListener()
       {
          public void onClick(DialogInterface dialog, int whichButton)
          {

        stopService(new Intent("com.commonware.android.AnalogClock.START_AUDIO_SERVICE"));
            AlarmAlert.this.finish();
          }
       })
       .show();
   }
}


///////////////////////////////////////////////////////////////////////////////////////

package com.commonware.android.AnalogClock;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;


public class Music extends Service {

private MediaPlayer player;
@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}
public void onStart(Intent intent, int startId) {       
    super.onStart(intent, startId);
    player = MediaPlayer.create(this, R.raw.gequ);
    player.start();
}

public void onDestroy() {
    super.onDestroy();
    player.stop();
}

}
//////////////////////////////////////////////////////////////////////////////////



// AndroidManifest.xml


      <Service android:name=".music">
      <intent-filter>
        <action      android:name="com.commonware.android.AnalogClock.START_AUDIO_SERVICE" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
    </Service>