Android determines whether or not to start the instance at startup based on user settings.

Source: Internet
Author: User

Today we will discuss how to determine whether to start the application at startup based on the user's settings on the Android phone. If you do not know how to use this sentence, let's take a closer look at the problem to be solved.

We all know that to enable the application to start at startup, first add permissions to manifest, register the handler, add actions to the handler, and then enable a service or activity in the onreceive method of the handler.

If an application wants the user to decide whether to start at startup, the user selects yes, and the application receives the boot completed broadcast. If the user chooses no, the program does not receive the boot completed broadcast. However, since the receiver is registered in the manifest file, how can we make it not respond to boot completed broadcast when the mobile phone starts.

Some people say that checking sharedpreferences in the onreceive method is incorrect. Even if it is finished after onreceive is executed, it is started after all.

The solution is to use packagemanager and call its setcomponentenabledsetting method. This method can invalidate or take effect for a component. If the consumer fails, the consumer cannot receive the broadcast, of course, if it takes effect, it will be able to receive the broadcast.

 


 

Post code snippets first

First, permission

<Uses-Permission Android: Name = "android. Permission. receive_boot_completed"/>

Register the aggreger

<Cycler Android: Name = ". mycycler">
<Intent-filter>
<Action Android: Name = "android. Intent. Action. boot_completed"> </Action>
</Intent-filter>
</Cycler>

Then the onreceiver method. I chose to start an activity test.

Public class myreceiver extends broadcastreceiver {

@ Override
Public void onreceive (context, intent ){
If (intent. getaction (). Equals (intent. action_boot_completed )){
Toast. maketext (context, "receive boot completed", Toast. length_long). Show ();
Intent start = new intent (context, disablereceiverincodeactivity. Class );
Start. setflags (intent. flag_activity_new_task );
Context. startactivity (start );
}
}

The main interface is disablereceiverincodeactivity. There is a checkbox in the layout file. When the checkbox is selected, the program will be started at the startup; otherwise, the program will not be started.

<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: Orientation = "vertical">

<Linearlayout
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: Orientation = "horizontal">
<Textview
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "start on boot complete">
</Textview>
<Checkbox Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: Id = "@ + ID/boot_complete_checkbox"/>
</Linearlayout>

</Linearlayout>

Next is the checkbox event and Processing

Public class disablereceiverincodeactivity extends activity {
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );

Final checkbox bootcheck = (checkbox) findviewbyid (R. Id. boot_complete_checkbox );
Final componentname CM = new componentname ("com. rsft. Test. demo1", "com. rsft. Test. demo1.myreceiver ");
Final packagemanager PM = getpackagemanager ();
Int state = PM. getcomponentenabledsetting (CM );
If (State! = Packagemanager. component_enabled_state_disabled
& State! = Packagemanager. component_enabled_state_disabled_user ){
Bootcheck. setchecked (true );
}
Bootcheck
. Setoncheckedchangelistener (New compoundbutton. oncheckedchangelistener (){

@ Override
Public void oncheckedchanged (compoundbutton buttonview,
Boolean ischecked ){
Int newstate = bootcheck. ischecked ()? Packagemanager. component_enabled_state_enabled
: Packagemanager. component_enabled_state_disabled;
PM. setcomponentenabledsetting (CM, newstate,
Packagemanager. dont_kill_app );
}
});
}
}

Then it's over. Let's just briefly talk about componentname CM = new componentname ("com. rsft. test. demo1 "," com. rsft. test. demo1.myreceiver ") in front of this item is the package name of the program, followed by the class name of the component, and mine is a aggreger. Note that the class name must be the full name, is the package name + class name.

Let's talk about PM. setcomponentenabledsetting (CM, newstate,
Packagemanager. dont_kill_app); this is the method to invalidate or take effect of a component. The first parameter is the component, and the second parameter is the status you want to set. You can check the document to find out the status, the third parameter can be set with only two values. For more information, see the document.

I will write the project files in the comments. download them.

Thank you for reading

If Apsara

Code downloads

Http://download.csdn.net/detail/huihuangui/4323364

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.