main-xml
创建L1ocalService.java继承Service 并重写内部方法
public class L1ocalService extends Service {
public L1ocalService() {
Log.i("godv", "本地服务1被创建");
}
@Override
public void onCreate() {
super.onCreate();
Log.i("godv", "本地服务1启动");
}
//启动服务再次启动调用
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("godv", "onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i("godv", "本地服务1被销毁");
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
@Override
public void onLowMemory() {
super.onLowMemory();
}
@Override
public void onTrimMemory(int level) {
super.onTrimMemory(level);
}
@Override
public boolean onUnbind(Intent intent) {
Log.i("godv", "onUnbind");
return super.onUnbind(intent);
}
@Override
public void onRebind(Intent intent) {
super.onRebind(intent);
}
@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
}
@Override
protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
super.dump(fd, writer, args);
}
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(newBase);
}
//这里绑定的时候需要返回一个Binder对象 即IBinder的实现类
@Override
public IBinder onBind(Intent intent) {
Log.i("godv", "onBind");
return new Binder();
}
}
activity启动与销毁代码
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void startLocalService(View v) {
Intent intent = new Intent(this, L1ocalService.class);
startService(intent);
Toast.makeText(this, "start service", Toast.LENGTH_SHORT).show();
}
public void stopLocalService(View v) {
Intent intent = new Intent(this, L1ocalService.class);
stopService(intent);
Toast.makeText(this, "stop service", Toast.LENGTH_SHORT).show();
}
ServiceConnection serviceConnection;
public void bindLocalService(View v) {
Intent intent = new Intent(this, L1ocalService.class);
if (serviceConnection == null) {
serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
Log.i("godv", "onServiceConnected");
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
Log.i("godv", "onServiceDisconnected");
}
};
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
Toast.makeText(this, "bind service", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "already bind service", Toast.LENGTH_SHORT).show();
}
}
public void unbindLocalService(View v) {
if (serviceConnection != null) {
unbindService(serviceConnection);
serviceConnection = null;
Toast.makeText(this, "unbind service", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "already unbind service", Toast.LENGTH_SHORT).show();
}
}
//在activity消亡的时候需要解绑服务
//只有当所有与服务连接的对象解绑 才会销毁Service
@Override
protected void onDestroy() {
super.onDestroy();
if (serviceConnection != null) {
unbindService(serviceConnection);
serviceConnection = null;
}
}
}
AndroidManifest.xml中注册
具体位置
总结一下生命周期:
1. startService(intent)
第一次调用 : -->构造方法()-->onCreate()-->onStartCommand()
(重要)后面再调用 : -->onStartCommand()
stopService() : -->onDestory()
2. bindService(intent, serviceConnection)
调用 : -->构造方法()-->onCreate()-->onBind()-->onServiceConnected()
unbindService(): (中有当前Activity与Service连接)-->onUnbind()-->onDestroy()