开发者

Why the service does not work?

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开发者_JS百科.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>


One thing I would try is change the way you're starting the Service:

startService(new Intent("com.commonware.android.AnalogClock.START_AUDIO_SERVICE"));

to

startService(new Intent(this, Music.class));

I'm having troubles of my own (here) so :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜