Android creates a separate service running in the background (no interface)

Reprinted from: https://blog.csdn.net/a704225995/article/details/56481934

 

One of the requirements of today's project is to start a service that runs alone in the background, and there is no interface yet. After searching around Du Niang, I couldn't find a perfect solution. Then I tried to solve it myself. The starting idea was to put The interface is killed, that is, the activity, and then the operation of opening the Service is placed in the Application. As a result, the program is run and an error is reported in the console.

 

Because I have killed the configuration of the main Activity in AndroidManifest.xml, and the program cannot find the entry of the application, so the application cannot be opened. This method does not work.

 

Then I thought, keep the Activity, but I don't give it setContentView(...); that is, I don't set the layout file for it,

 

 

[java]  view plain copy  
 
  1. public class MainActivity extends Activity {  
  2.   
  3.     @Override  
  4.     protected void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate (savedInstanceState);  
  6.           
  7.         System.out.println("MainActivity  OnCreate()....");  
  8.           
  9.         System.out.println( "Ready to start the service");  
  10.         Intent intent = new Intent(MainActivity.this,TestService.class);    
  11.         startService(intent);    
  12.     }  
  13. }  

 


Run the program, the program is opened, and the service is running, but there is a problem, the interface also comes out, why?

 

 

The reason is that this line of code android:theme="@style/AppTheme" in the Application node in AndroidManifest.xml, since it is a theme problem that leads to the appearance of the interface, then I wonder if android provides a theme that does not display the interface? After searching, the problem is finally solved, the solution: in the manifest file, add this line of code to the configuration of the main activity

 

android:theme="@android:style/Theme.NoDisplay"

Code:

 

[java]  view plain copy  
 
  1. <application  
  2.        android:allowBackup="true"  
  3.        android:icon="@drawable/ic_launcher"  
  4.        android:label="@string/app_name"  
  5.        android:theme="@style/AppTheme" >  
  6.        <activity  
  7.            android:name=".MainActivity"  
  8.            android:label="@string/app_name"  
  9.            android:theme="@android:style/Theme.NoDisplay"  
  10.            >  
  11.            <intent-filter>  
  12.                <action android:name="android.intent.action.MAIN" />  
  13.   
  14.                <category android:name="android.intent.category.LAUNCHER" />  
  15.            </intent-filter>  
  16.        </activity>  
  17.   
  18.        <service android:name="com.example.backgroundservice.TestService" >  
  19.        </service>  
  20.    </application>  


We can also Ctrl+left click to see how this theme is written:

 

[java]  view plain copy  
 
  1. <!-- Default theme for activities that don't actually display a UI; that  
  2.         is, they finish themselves before being resumed.  -->  
  3.    <style name="Theme.NoDisplay">  
  4.        <item name="android:windowBackground">@null</item>  
  5.        <item name="android:windowContentOverlay">@null</item>  
  6.        <item name="android:windowIsTranslucent">true</item>  
  7.        <item name="android:windowAnimationStyle">@null</item>  
  8.        <item name="android:windowDisablePreview">true</item>  
  9.        <item name="android:windowNoDisplay">true</item>  
  10.    </style>  


Run the program, the service is turned on, and the interface is not displayed, which perfectly solves the process of starting the service in the background.

 

Guess you like

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