咸鱼干_ 2017-02-23 03:54 采纳率: 0%
浏览 955

关于服务执行的顺序问题

这是服务里的代码
public class Myservice extends Service {

private DownloadBinder mBinder= new DownloadBinder();

@Override
public IBinder onBind(Intent intent){
    return mBinder;
}
class DownloadBinder extends Binder{

    public void startDownload(){
        Log.d ( "MyService","startDownload excuted");

    }

    public int getProgress(){
        Log.d("MyService","getprogress");
        return 0;
    }
}

@Override
public void onCreate(){
    super.onCreate();
    Log.d ( "MyService","OnCreate excuted");
}

@Override

public int onStartCommand(Intent intent,int flags,int startId){
    Log.d ( "MyService","start excuted");
    return super.onStartCommand(intent, flags, startId);

}

@Override
public void onDestroy(){
    super.onDestroy();
    Log.d ( "MyService","stop excuted");
}

}
这是主活动的代码
public class MainActivity extends ActionBarActivity implements OnClickListener {

private Button startService;
private Button stopService;
private Button bindService;
private Button unbindService;
private Myservice.DownloadBinder downloadBinder;

private ServiceConnection connection = new ServiceConnection() {

    @Override
    public void onServiceDisconnected(ComponentName name) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        downloadBinder = (Myservice.DownloadBinder) service;
        downloadBinder.startDownload();
        downloadBinder.getProgress();


    }
};


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    startService = (Button) findViewById(R.id.start_service);
    stopService = (Button) findViewById(R.id.stop_service);
    bindService = (Button) findViewById(R.id.bind_service);
    unbindService = (Button) findViewById(R.id.unbind_service);

    startService.setOnClickListener(this);
    stopService.setOnClickListener(this);
    bindService.setOnClickListener(this);
    unbindService.setOnClickListener(this);
}


@Override

public void onClick(View v){
    switch(v.getId()){
    case R.id.start_service:
        Intent startIntent = new Intent(this,Myservice.class);
        startService(startIntent);break;

    case R.id.stop_service:
        Intent stopIntent = new Intent(this,Myservice.class);
        stopService(stopIntent);break;
    case R.id.bind_service:
        Intent bindIntent = new Intent (this,Myservice.class);
        bindService(bindIntent,connection,BIND_AUTO_CREATE);
        break;
    case R.id.unbind_service:
        unbindService(connection);
        break;
    default:
        break;

    }
}

}

为什么DownloadBinder放在onStartCommand()后面然后点击bing_service会执行oncreate() 方法,放在oncreate则不会执行

  • 写回答

1条回答 默认 最新

  • 白萝卜。 2017-02-23 06:00
    关注

    onCreate只执行1次,onStartCommand可以执行多次,
    http://www.cnblogs.com/larrylawrence/p/3420896.html

    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料