在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
{
}
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
{
}
解决方案 »
- ServerSocket做的端口监听时间长老是被回收的问题
- Android 应该程序开机启动如何用复选框来控制。
- 安卓中StartService = (Button) findViewById(R.id.StartService);为什么错的
- 关于view的点击问题
- 谁解决了给100!!!!!!!!!!!!!!!!!!
- 鉴于opengl es学的太。。。。痛苦,现招募同道中人去买杨哥哥的视频
- 想找一个公司开发一款手机游戏,只有一个构思,请问需要准备什么?
- 新手,Android打开SQLite问题
- BitmapFactory.decode问题
- Android Studio3.2.1在Build菜单没有Generate Signed Apk选项怎么回事
- gallery能否实现拖拉到头儿时的弹性效果
- android 调用lua脚本
现在遇到的情况是 activity onstop以后service还是继续执行的