Can an Android app only have one service or broadcast reciver without activity?

Source: Internet
Author: User
Service is the most similar component to activity among the four Android components. It can represent executable programs.
The difference between service and activity is:(1) The Service has been running in the background and there is no user interface. (2) once the service is started, it is the same as the activity. Has its own lifecycle. Therefore, no activity is allowed. Two steps are required for service development:(1) define a subclass that inherits the Service (2). Configure the service in androidmanifest. xml. The process is the same as configuring the activity. There are two ways to run the service:[1]. Use the startservice () method of context to start a service. There is no association between the visitor and the service. Even if the visitor exits, the service still runs.
[2]. Use the bingserive () method of context to enable the Service. The visitor and service are associated, that is, they are bound to the first node, the access exits, and the service exits. Broadcast reciver is essentially a global listener, which can be used for communication between components. It is used to receive the broadcast from the program.
Intent, which is the same as the application to start the activity. The service is: the program to start broadcast reciver also requires two steps [1]. Create the intent of broadcast reciver [2]. Call sendbroadcase () of context () or the sendorderbroadcase () method is used to start the broadcastreciver. In the code demonstrated by the author below, the service and broadcast reciver are combined, and no activity is required. When the program receives a broadcast
When reciver is enabled, the service can also be started through activity ). In this example, start the service by yourself. When it is started, the broadcast will be carried out. We will receive the broadcast and then enable the service! Broadcast Program code:
Package com. keenhi. tianpei;
Import Android. content. broadcastreceiver;
Import Android. content. context;
Import Android. content. intent;
Public class test_chargereceive extends broadcastreceiver {
@ Override
Public void onreceive (context, intent ){
// Todo auto-generated method stub
Intent intent1 = new intent (context, test_chargeservice.class );

Context. startservice (intent1 );
}

}
Service program code: Package com. keenhi. tianpei;
Import Android. App. Service;
Import Android. content. intent;
Import Android. OS. ibinder;
Public class test_chargeservice extends Service {
@ Override
Public ibinder onbind (intent ){
// Todo auto-generated method stub
Return NULL;
} @ Override
Public void oncreate ()

{System. Out. println ("service create ");}
}

Manifest. xml program code: <? XML version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"
Package = "com. keenhi. tianpei"
Android: versioncode = "1"
Android: versionname = "1.0" type = "codeph" text = "/codeph">
<Uses-SDK Android: minsdkversion = "15"/>
<Uses-Permission Android: Name = "android. Permission. receive_boot_completed"/>
<Application
Android: icon = "@ drawable/ic_launcher"
Android: Label = "@ string/app_name">
<Activity
Android: Name = ". test_chageactivity"
Android: Label = "@ string/app_name">
<Intent-filter>
<Action Android: Name = "android. Intent. Action. Main"/>
<Category Android: Name = "android. Intent. Category. launcher"/>
</Intent-filter>
</Activity>
<Service android: Name = "test_chargeservice"> </service>
<Explorer Android: Name = "test_chargereceive">
<Intent-filter>
<Action Android: Name = "android. Intent. Action. boot_completed"/>
</Intent-filter>
</Cycler>
</Application>
</Manifest>
If you directly push xxx.apk into system/APP with ADB, the program will start automatically. However, if I did a test in eclipse, I found that if I didn't start an activity, broadcastreceiver
The reason for not receiving the self-starting action is unclear. It is estimated that android never prompts the user to prevent the trojan from running in the background. Useless activity code:
Package com. keenhi. tianpei;
Import Android. App. activity;
Import Android. OS. Bundle;
Public class test_chageactivity extends activity {
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
}
}


As you can see, this activity is absolutely useless. It will only be opened once when eclipse loads the program into the machine. After countless shutdown times, it will not be called, however, the service will use broadcastreceiver. However, there must be activity during testing. Remember.
After testing the xxx.apk file, we can remove the activity and directly push it into the system/APP program to run in good background. Because there is a service, we can see the processes and services running in the background through the process controller (Setup/application. But in fact, we can avoid service. Only one broadcastreceiver can be used. However, please note that there is no service or process at this time. We cannot find it in the process controller, but it does run. It loads itself as part of the system program. During the test, you still need to create an activity. To push the activity to system/app, you only need a broadcastreceiver. A broadcastreceiver program is the same as above. You can simply remove the service.
In addition, such a program will usually implement the same daemon process as the system, so that it cannot be killed. Add the following content to the manifest. xml file:
<Manifest
Xmlns: Android = "http://schemas.android.com/apk/res/android"

Android: shareduserid = "android. uid. System">

<Application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name"

Android: allowclearuserdata = "false" Android: Process = "system"

Android: killafterrestore = "false">

Related Article

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.