Android实战Service BroadcastReceiver与AlarmManager
2010-08-22 04:48:00 来源:WEB开发网public IBinder onBind(Intent intent) {
return null;
}
}
完成了Service,之后就是写一个Activity来启动这个Service,其中为了保证在应用退出后Servcie继续运行,所以要使用startService()来启动Service。一般关于Service的资料上都是有的。
package com.shinestudio.sleepMusic;
import java.util.Iterator;
import java.util.List;
//......
import com.shinestudio.sleepMusic.service.ISleepMusicService;
import com.shinestudio.sleepMusic.service.SleepMusicService;
import com.shinestudio.sleepMusic.unit.TimerPickerUIStruct;
public class StartActivity extends Activity {
private static String TAG = "sleepMusic - StartActivity";
private ListView timerlist;
private TimerPicker timerPicker;
private TimerPickerUIStruct tpui;
private Button startButton;
private Button cancelButton;
private Button restartButton;
//当点击开始按钮
private OnClickListener startButtonClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
Intent isleepMusicService = new Intent(StartActivity.this,
SleepMusicService.class);
/*
在Service启动之前可以使用Intent来传递参数给Service ,方法如下
目前的代码只是演示,与功能无关
*/
Bundle setting = new Bundle();
setting.putString(“TIME_SETTING”, "5s");
// 在Service中使用“TIME_SETTING”这个标签就可以从Intent取出5s 这个字符串了
isleepMusicService.putExtras(setting);
startService(isleepMusicService);
}
};
//点击取消按钮
private OnClickListener cancelButtonClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
Intent sleepMusicService = new Intent(StartActivity.this,
更多精彩
赞助商链接