서비스: 백그라운드에 있는 작업 구성 요소를 처리하기 위한 전용 백그라운드 서비스입니다.
인스턴트 메시징: 상주 배경이 되는 서비스
1. 핵심서비스는 최대한 가볍게!
많은 사람들이 모든 백그라운드 작업을 하나의 서비스로 중앙 집중화하기를 원합니다.
핵심 서비스를 위한 전용 프로세스를 만들고 다른 모든 백그라운드 작업에서 분리합니다.
큰 나무는 바람을 끌어당기고 핵심 서비스는 가벼워야 합니다.
프로세스 중요도 우선순위:
-
포그라운드 프로세스: 포그라운드 프로세스
1. 사용자가 상호 작용하는 활동(onResume())
2. 서비스가 상호 작용하는 활동에 바인딩된 경우
3. 포그라운드 서비스로 적극적으로 호출됩니다(startForeground())
4. 구성 요소가 수명 주기 ((onCreate ) () /onStart()/onDestory())
5.BroadcastReceiver가 onReceive()를 실행 중입니다.
-
보이는 과정: 보이는 과정
1. 활동이 onPause()에 있습니다(onStop()을 입력하지 않음)
. 2. 전경 활동의 서비스에 바인딩 -
서비스 프로세스: 서비스 프로세스
간단한 startservice() 시작
-
백그라운드 프로세스: 백그라운드 프로세스
사용자에게 직접적인 영향을 미치지 않는 프로세스 ---활동이 onStop()에 있을 때
-
빈 프로세스: 빈 프로세스
활성 구성 요소를 포함하지 않습니다. (Android는 두 번째로 더 빨리 시작하기 위해 절충안으로 설계되었습니다)
프로세스가 뒤로 갈수록 시스템에 의해 더 쉽게 죽습니다.
2. 프로세스의 우선순위를 높이는 방법 (시스템에 쉽게 죽지 않도록 노력)
1. QQ를 모방하여 화면이 잠겨 있을 때 1픽셀 활동을 시작합니다.
背景:当手机锁屏的时候什么都干死了,为了省电。
监听锁屏广播,锁了---启动这个1像素Activity。
监听锁屏的, 开启---结束掉这个1像素Activity。
要监听锁屏的广播---动态注册。
复制代码
키 코드:
public class KeepLiveActivityManager {
private static KeepLiveActivityManager instance;
private Context context;
private WeakReference<Activity> activityInstance;
public static KeepLiveActivityManager getInstance(Context context) {
if(instance==null){
instance = new KeepLiveActivityManager(context.getApplicationContext());
}
return instance;
}
private KeepLiveActivityManager(Context context) {
this.context = context;
}
public void setKeepLiveActivity(Activity activity){
activityInstance = new WeakReference<Activity>(activity);
}
public void startKeepLiveActivity() {
Intent intent = new Intent(context, KeepLiveActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
public void finishKeepLiveActivity() {
if(activityInstance!=null&&activityInstance.get()!=null){
Activity activity = activityInstance.get();
activity.finish();
}
}
}
复制代码
public class KeepLiveActivity extends Activity {
private static final String TAG ="KeepLive" ;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
moveTaskToBack(true); //按home键不退出程序
Log.i(TAG,"KeepLiveActivity----onCreate");
Window window = getWindow();
window.setGravity(Gravity.LEFT|Gravity.TOP);
WindowManager.LayoutParams params =window.getAttributes();
params.height = 1;
params.width = 1;
params.x = 0;
params.y = 0;
window.setAttributes(params);
KeepLiveActivityManager.getInstance(this).setKeepLiveActivity(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i(TAG, "KeepLiveActivity----onDestroy!!!");
}
}
复制代码
public class MyService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
ScreenListener listener = new ScreenListener(this);
listener.begin(new ScreenListener.ScreenStateListener() {
@Override
public void onScreenOn() {
//开屏---finish这个一像素的Activity
KeepLiveActivityManager.getInstance(MyService.this).finishKeepLiveActivity();
}
@Override
public void onScreenOff() {
//锁屏---启动一像素的Activity
KeepLiveActivityManager.getInstance(MyService.this).startKeepLiveActivity();
}
@Override
public void onUserPresent() {
}
});
}
}
复制代码
소스 주소:
KeepLiveProcess
2. 대규모 앱 운영자와 휴대폰 제조업체는 협력 관계를 가질 수 있습니다 --- 허용 목록
3. 이중 프로세스 데몬
一个进程被杀死,另外一个进程又被他启动,相互监听启动。
复制代码
A<---->B
杀进程是一个一个杀的,本质是和杀进程时间赛跑。
关键代码:
public class LocalService extends Service {
public static final String ACTION_LOCAL_SERVICE = "com.haocai.app.keepliveprocess.LocalService";
private static final String TAG = "LocalService";
private MyServiceConnection conn;
private MyBinder binder;
private Intent testIntent;
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
if(binder ==null){
binder = new MyBinder();
}
conn = new MyServiceConnection();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return binder;
}
@Override
public void onDestroy() {
super.onDestroy();
if(testIntent!=null){
stopService(testIntent);
}
//unbindService(conn);
}
//启动前台进程 增加重要性优先级
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
LocalService.this.bindService(new Intent(LocalService.this, RemoteService.class), conn, Context.BIND_IMPORTANT);
PendingIntent contentIntent = PendingIntent.getService(this, 0, intent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setTicker("360")
.setContentIntent(contentIntent)
.setContentTitle("我是360,我怕谁!")
.setAutoCancel(true)
.setContentText("hehehe")
.setWhen( System.currentTimeMillis());
//把service设置为前台运行,避免手机系统自动杀掉改服务。
startForeground(startId, builder.build());
return START_STICKY;
}
class MyBinder extends RemoteConnection.Stub{
@Override
public String getProcessName() throws RemoteException {
// TODO Auto-generated method stub
return "LocalService";
}
}
class MyServiceConnection implements ServiceConnection {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i(TAG, "建立连接成功!");
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i(TAG, "本地服务被干掉了~~~~~断开连接!");
Toast.makeText(LocalService.this, "断开连接", Toast.LENGTH_SHORT).show();
//启动被干掉的
testIntent = new Intent();
//自定义的Service的action
testIntent.setAction(RemoteService.ACTION_REMOTE_SERVICE);
//自定义Service的包名
testIntent.setPackage(getPackageName());
Log.i("999", getPackageName() + "");
startService(testIntent);
LocalService.this.bindService(new Intent(LocalService.this, RemoteService.class), conn, Context.BIND_IMPORTANT);
}
}
}
复制代码
public class RemoteService extends Service {
private static final String TAG = "RemoteService";
private MyBinder binder;
private MyServiceConnection conn;
public static final String ACTION_REMOTE_SERVICE = "com.haocai.app.keepliveprocess.RemoteService";
private Intent testIntent;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return binder;
}
@Override
public void onCreate() {
super.onCreate();
if(binder == null){
binder = new MyBinder();
}
conn = new MyServiceConnection();
}
@Override
public void onDestroy() {
super.onDestroy();
// if(testIntent!=null){
// stopService(testIntent);
// }
//unbindService(conn);
}
//启动前台进程 增加重要性优先级
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
RemoteService.this.bindService(new Intent(RemoteService.this, LocalService.class), conn, Context.BIND_IMPORTANT);
PendingIntent contentIntent = PendingIntent.getService(this, 0, intent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setTicker("360")
.setContentIntent(contentIntent)
.setContentTitle("我是360,我怕谁!")
.setAutoCancel(true)
.setContentText("hehehe")
.setWhen( System.currentTimeMillis());
//把service设置为前台运行,避免手机系统自动杀掉改服务。
startForeground(startId, builder.build());
return START_STICKY;
}
class MyBinder extends RemoteConnection.Stub{
@Override
public String getProcessName() throws RemoteException {
return "RemoteService";
}
}
class MyServiceConnection implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i(TAG,"RemoteService 建立连接成功!");
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i(TAG,"远程服务被干掉了~~~~~断开连接!");
Toast.makeText(RemoteService.this,"断开连接",Toast.LENGTH_SHORT).show();
//启动被干掉的
testIntent = new Intent();
//自定义的Service的action
testIntent.setAction(LocalService.ACTION_LOCAL_SERVICE);
//自定义Service的包名
testIntent.setPackage(getPackageName());
Log.i("999",getPackageName()+"");
startService(testIntent);
RemoteService.this.bindService(new Intent(RemoteService.this, LocalService.class), conn, Context.BIND_IMPORTANT);
}
}
}
复制代码
源码地址:
KeepLiveProcess2
4.JobScheduler
把任务加到系统调度队列中,当到达任务窗口期的时候就会执行,我们可以在这个任务里面启动我们的进程。
这样可以做到将近杀不死的进程。
@SuppressLint("NewApi")
public class JobHandleService extends JobService{
public static final String ACTION_JOB_HANDLE_SERVICE = "com.haocai.app.keepliveprocess.JobHandleService";
private int kJobId = 0;
@Override
public void onCreate() {
super.onCreate();
Log.i("INFO", "jobService create");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("INFO", "jobService start");
scheduleJob(getJobInfo());
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
@Override
public boolean onStartJob(JobParameters params) {
// TODO Auto-generated method stub
Log.i("INFO", "job start");
// scheduleJob(getJobInfo());
boolean isLocalServiceWork = isServiceWork(this, LocalService.ACTION_LOCAL_SERVICE);
boolean isRemoteServiceWork = isServiceWork(this, RemoteService.ACTION_REMOTE_SERVICE);
// Log.i("INFO", "localSericeWork:"+isLocalServiceWork);
// Log.i("INFO", "remoteSericeWork:"+isRemoteServiceWork);
if(!isLocalServiceWork||
!isRemoteServiceWork){
//this.startService(new Intent(this,LocalService.class));
startLocalService();
startRemoteService();
//this.startService(new Intent(this,RemoteService.class));
Toast.makeText(this, "process start", Toast.LENGTH_SHORT).show();
}
return true;
}
private void startLocalService(){
Intent testIntent = new Intent();
//自定义的Service的action
testIntent.setAction(LocalService.ACTION_LOCAL_SERVICE);
//自定义Service的包名
testIntent.setPackage(getPackageName());
Log.i("999",getPackageName()+"");
startService(testIntent);
}
private void startRemoteService(){
Intent testIntent = new Intent();
//自定义的Service的action
testIntent.setAction(RemoteService.ACTION_REMOTE_SERVICE);
//自定义Service的包名
testIntent.setPackage(getPackageName());
Log.i("999", getPackageName() + "");
startService(testIntent);
}
@Override
public boolean onStopJob(JobParameters params) {
Log.i("INFO", "job stop");
// Toast.makeText(this, "process stop", Toast.LENGTH_SHORT).show();
scheduleJob(getJobInfo());
return true;
}
/** Send job to the JobScheduler. */
public void scheduleJob(JobInfo t) {
Log.i("INFO", "Scheduling job");
JobScheduler tm =
(JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
tm.schedule(t);
}
public JobInfo getJobInfo(){
JobInfo.Builder builder = new JobInfo.Builder(kJobId++, new ComponentName(this, JobHandleService.class));
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
builder.setPersisted(true);
builder.setRequiresCharging(false);
builder.setRequiresDeviceIdle(false);
builder.setPeriodic(10);//间隔时间--周期
return builder.build();
}
/**
* 判断某个服务是否正在运行的方法
*
* @param mContext
* @param serviceName
* 是包名+服务的类名(例如:net.loonggg.testbackstage.TestService)
* @return true代表正在运行,false代表服务没有正在运行
*/
public boolean isServiceWork(Context mContext, String serviceName) {
boolean isWork = false;
ActivityManager myAM = (ActivityManager) mContext
.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningServiceInfo> myList = myAM.getRunningServices(100);
if (myList.size() <= 0) {
return false;
}
for (int i = 0; i < myList.size(); i++) {
String mName = myList.get(i).service.getClassName().toString();
if (mName.equals(serviceName)) {
isWork = true;
break;
}
}
return isWork;
}
}
复制代码
<service
android:name=".JobHandleService"
android:permission="android.permission.BIND_JOB_SERVICE">
</service>
复制代码
5.监听QQ,微信,系统应用,友盟,小米推送等等的广播,然后把自己启动了。
6.利用账号同步机制唤醒我们的进程
AccountManager