在bindService的api中提到For example, if this Context is an Activity that is stopped, the service will not be required to continue running until the Activity is resumed. 请问怎么做到上面提到的context状态与service绑定 然后flags各个值不是太了解 没感觉出多大差异 我自己写了个demo 
main activity :public class BindServiceTestActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Intent intent=new Intent("net.rgb123.service");
        //startService(intent);        
        
        bindService(intent,con,Service.BIND_AUTO_CREATE);
    }
    
    @Override
    protected void onDestroy() {
     // TODO Auto-generated method stub
     super.onDestroy();
    
     unbindService(con);
    };
    
    @Override
    protected void onStop() {
     super.onStop();
     Log.i("activity","onStop");
    };
    
    @Override
    protected void onRestart() {
     // TODO Auto-generated method stub
     super.onRestart();
     Log.i("activity","onRestart");
    }
    
    @Override
    protected void onResume() {    
     super.onResume();
     Log.i("activity","onResume");
    
    };
    
    ServiceConnection con=new ServiceConnection() {

public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
Log.i("onServiceDisconnected",name.toString());
}

public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
Log.i("onServiceConnected",name+" | "+service.toString());
}
};service:
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();

Log.i("service","onCreate");


}

@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();

Log.i("service","onDestroy");
}

@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId); Log.i("service","onStart: "+intent.toString()+" | "+startId);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub

Log.i("service","onStartCommand: "+intent.toString()+" | "+flags+" | "+startId);
return super.onStartCommand(intent, flags, startId);

}

@Override
public void onLowMemory() {
// TODO Auto-generated method stub
super.onLowMemory();

Log.i("service","onLowMemory");
}

@Override
public void onRebind(Intent intent) {
// TODO Auto-generated method stub
super.onRebind(intent);

Log.i("service","onRebind: "+intent.toString());
}

@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub

Log.i("service","onBind: "+arg0.toString());
return myBinder;
}

@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
Log.i("service","onUnbind: "+intent.toString());
return super.onUnbind(intent);
}

private MyBinder myBinder=new MyBinder();

public class MyBinder extends Binder
{

}

解决方案 »

  1.   

    不太懂上面的问题。startService(intent);  当你的activity摧毁的时候,service并不会摧毁。bindservice的话,伴随activity的摧毁而摧毁
      

  2.   

    For example, if this Context is an Activity that is stopped, the service will not be required to continue running until the Activity is resumed.Activity 在进入 onStop() 状态后,service 不会再继续执行, 直到Activity重新进入onResume
      

  3.   


    现在遇到的情况是 activity onstop以后service还是继续执行的