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

Source: Internet
Author: User

Turn from: 56481934

Today the project has a need to open a service alone in the background, but also can not have an interface, in the degree Niang search a circle did not find a perfect solution to the method, and then try to solve their own method, the beginning of the idea is that the interface to kill, that is, activity, Then the operation of the service is placed in the application, the result of running the program, the console error.

Because I have the main activity in the Androidmanifest.xml configuration to kill, and the program can not find the application of the portal, all can not open the application, this method does not work.

Then I thought, keep the activity, but I don't give it setcontentview (...); Which is not to set the layout file for him,

[Java]View PlainCopy
  1. Public class Mainactivity extends Activity {
  2. @Override
  3. protected void OnCreate (Bundle savedinstancestate) {
  4. super.oncreate (savedinstancestate);
  5. System.out.println ("mainactivity OnCreate () ....");
  6. System.out.println ("Ready to open service");
  7. Intent Intent = new Intent (mainactivity. This,testservice.    class);
  8. StartService (Intent);
  9. }
  10. }


Run the program, the program opened, the service is also running, but there is a problem is that the interface has come out, why?

The reason is that this line of code in the application node in Androidmanifest.xml android:theme= "@style/apptheme", since the subject matter causes the interface to appear, So do you want Android to provide a theme that does not display the interface? After finding the problem finally solved, workaround: In the manifest file, the main activity of the configuration to add this line of code

Android:theme= "@android: Style/theme.nodisplay"

Code:

[Java]View PlainCopy
  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. <category android:name="Android.intent.category.LAUNCHER"/>
  14. </intent-filter>
  15. </activity>
  16. <service android:name="Com.example.backgroundservice.TestService" >
  17. </service>
  18. </application>


We can also CTRL + Left click to see how this topic is written:

[Java]View PlainCopy
  1. <!--Default theme for activities that don ' t actually display a UI;
  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, the interface is not displayed, perfect solution to the background to start the service process.

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.