@Override public void onCreate() { super.onCreate(); }
@Override public int onStartCommand(Intent intent, int flags, int startId) { new Thread(new Runnable() { @Override public void run() { // 开始执行后台任务 } }).start(); return super.onStartCommand(intent, flags, startId); }
@Override public void onDestroy() { super.onDestroy(); }
@Override public IBinder onBind(Intent intent) { return null; } //与activity 绑定 class MyBinder extends Binder {
public void startDownload() { // 执行具体的下载任务 } }
public class MainActivity extends Activity implements OnClickListener {
private Button startService;
private Button stopService;
private Button bindService;
private Button unbindService;
private MyService.MyBinder myBinder;
private ServiceConnection connection = new ServiceConnection() {
@Override public void onServiceDisconnected(ComponentName name) { }