咸鱼干_ 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

    评论

报告相同问题?

悬赏问题

  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致