2010-05-21 77 views
0

AlertDialog可以正常显示,但为什么音乐服务无法启动?为什么服务不起作用?

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> 

回答

0

有一两件事我想尝试的是改变你开始的服务方式:

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

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

我有我自己的(here)的烦恼等等:)