Background data upload in android sleep state

  

Let's talk about passing data in the case of black screen:

To realize that after the program exits, data can still be passed and the network can be requested, the service must be used, and the service can be kept running in the background. Unless the system resources are extremely scarce, generally speaking, the service will not be killed by the system.

  

 

 start a server  

 

Then set it in the onStart method of the server

 

@Override  
     public  void onStart(Intent intent, int startId) {  
         // TODO Auto-generated method stub   
        super .onStart(intent, startId);  
         // Create PowerManager object   
        pm = (PowerManager) getSystemService(Context.POWER_SERVICE);  
         // Keep The cpu keeps running, no matter whether the screen is black or not   
        wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "CPUKeepRunning" );  
        wakeLock.acquire();  
    }  
@Override
public void onDestroy() {
wakeLock.release();
super.onDestroy();
}
 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325229175&siteId=291194637