4、MainActivity 代码
package kim.hsl.two_progress_alive; import android.content.Intent; import android.os.Build; import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 通过前台 Service 提升应用权限 // 启动普通 Service , 但是在该 Service 的 onCreate 方法中执行了 startForeground // 变成了前台 Service 服务 startService(new Intent(this, LocalForegroundService.class)); startService(new Intent(this, RemoteForegroundService.class)); // JobScheduler 拉活 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { KeepAliveJobService.startJob(this); } } }
5、运行效果
运行后 , 两个进程成功运行 ;
即使将启动双进程的代码注释掉 , 也可以成功拉起双进程 ;
三、 源码资源
源码资源 :
GitHub 地址 : https://github.com/han1202012/Two_Process_Alive
CSDN 源码快照 : https://download.csdn.net/download/han1202012/16623594