在第一个service里启动第二个service并绑定第二个service,当第二个service被杀死后在断开连接回调里再唤醒,第二个service做一样的操作
public class FirstService extends Service { private MyCon myCon; @Override public IBinder onBind(Intent intent) { //第4步,将自定义的Binder,在这里 return return null; } @Override public void onCreate() { super.onCreate(); Log.e("TAG", "本地服务启动"); //第6步,new 一个自定义的连接对象 if (myCon == null) { myCon = new MyCon(); } } @Override public int onStartCommand(Intent intent, int flags, int startId) { //第7步,当这个服务启动的时候,和远程服务绑定 bindService Intent intent1 = new Intent(FirstService.this, SecondService.class); FirstService.this.bindService(intent1, myCon, Context.BIND_IMPORTANT); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { super.onDestroy(); Log.e("TAG", "本地服务注销"); } //第5步,实现ServiceConnection 接口,实现它两个方法 public class MyCon implements ServiceConnection { @Override public void onServiceConnected(ComponentName name, IBinder service) { Log.e("TAG", "连接远程服务成功"); } @Override public void onServiceDisconnected(ComponentName name) { Log.e("TAG", "连接远程服务失败"); //这里是重点,当远程服务被杀死的时候,我们会在这里收到消息 然后重新启动服务,并且重新建立连接 Intent intent = new Intent(FirstService.this, SecondService.class); //重新启动服务 FirstService.this.startService(intent); //重新建立连接 FirstService.this.bindService(intent, myCon, Context.BIND_IMPORTANT); } } }
public class SecondService extends Service { private MyCon myCon; @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); Log.e("TAG", "远程服务启动"); if (myCon == null) { myCon = new MyCon(); } } @Override public int onStartCommand(Intent intent, int flags, int startId) { Intent intent1 = new Intent(SecondService.this, FirstService.class); SecondService.this.bindService(intent1, myCon, Context.BIND_IMPORTANT); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { super.onDestroy(); Log.e("TAG", "远程服务注销"); } public class MyCon implements ServiceConnection { @Override public void onServiceConnected(ComponentName name, IBinder service) { Log.e("TAG", "连接本地服务成功"); } @Override public void onServiceDisconnected(ComponentName name) { Log.e("TAG", "连接本地服务失败"); Intent intent = new Intent(SecondService.this, FirstService.class); //重新启动服务 SecondService.this.startService(intent); //重新建立连接 SecondService.this.bindService(intent, myCon, Context.BIND_IMPORTANT); } } }
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate( savedInstanceState ); setContentView( R.layout.activity_main ); findViewById(R.id.btn_start).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, FirstService.class); Intent intent1 = new Intent(MainActivity.this, SecondService.class); startService(intent); startService(intent1); } }); findViewById(R.id.btn_kill_server).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, SecondService.class); stopService(intent); } }); findViewById(R.id.btn_kill_local).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, FirstService.class); stopService(intent); } }); findViewById(R.id.btn_kill_double).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, SecondService.class); stopService(intent); Intent intent1 = new Intent(MainActivity.this, FirstService.class); stopService(intent1); } }); } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <Button android:id="@+id/btn_start" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="启动两个服务" /> <Button android:id="@+id/btn_kill_local" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="杀死本地服务" /> <Button android:id="@+id/btn_kill_server" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="杀死远程服务" /> <Button android:id="@+id/btn_kill_double" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="杀死两个服务" /> </LinearLayout>
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <service android:name=".SecondService" ></service> <service android:name=".FirstService" /> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>