我想更多地了解 Android 的行为,我想知道 Android 将如何创建服务。
我知道如何创建服务...从服务类扩展等等...
但有趣的是如何创建服务(在代码中)或如何将活动绑定到服务(方法:bindService(..))?
有谁知道程序吗?通过android的源代码战斗没有成功:/
此代码正在我的应用程序中运行
import java.io.IOException;
import android.app.Service;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class MusicService extends Service {
private static final String TAG = "MyService";
public static MediaPlayer mediaplayer;
String musicfile="http://radio.radioamerica.org:8000/ra.mp3";
@Override
public IBinder onBind(Intent intent) {
Log.i(" ibinder ", "");
return null;
}
@Override
public void onCreate() {
mediaplayer = new MediaPlayer();
mediaplayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
}
@Override
public void onDestroy() {
mediaplayer.stop();
}
@Override
public void onStart(Intent intent, int startid) {
Log.d(TAG, "onStart");
if (intent.getExtras().getString("paths") == null) {
Log.i("Intent Null", "Null");
} else {
Log.i("Intent Not Null", "Not Null");
musicfile = intent.getExtras().getString("paths");
Log.i("musicfile", "" + musicfile);
try {
mediaplayer.reset();
mediaplayer.setDataSource(musicfile);
mediaplayer.prepare();
mediaplayer.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
还有吗?