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 :(得分:0)
我要尝试的一件事是改变你启动服务的方式:
startService(new Intent("com.commonware.android.AnalogClock.START_AUDIO_SERVICE"));
到
startService(new Intent(this, Music.class));
我遇到了自己的麻烦(here)所以:)