Android boot example Program

Source: Internet
Author: User

Android boot example Program

Android boot sample program. The broadcast method is used for acceptance, and the Android built-in storage SharedPreferences is used to store the auto-Start Settings of startup.

Source code: Click

1. add permissions first

 

    
 

 

2. register the expected broadcast receiver (if you want to start the service, register it first)

 

 


 

3. Broadcast receiver AutoStartBroadcastReceiver

 

 

Package com. example. autostart; import android. content. broadcastReceiver; import android. content. context; import android. content. contextWrapper; import android. content. intent; import android. content. sharedPreferences; // auto-start broadcast on startup receives public class AutoStartBroadcastReceiver extends BroadcastReceiver {private static final String ACTION = android. intent. action. BOOT_COMPLETED; private SharedPreferences mPreferences = null; @ Overridepublic void onReceive (Context context, Intent intent) {mPreferences = context. getSharedPreferences (AutoStart, ContextWrapper. MODE_PRIVATE); if (intent. getAction (). equals (ACTION) {if (mPreferences. getBoolean (AddToAuto, false) {// XXX. class is the service Intent service to be started = new Intent (context, AutoStartService. class); context. startService (service); // start the application. The parameter is the package name of the application to be automatically started. It is only the package name Intent newIntent = context of the activity that starts the app. getPackageManager (). getLaunchIntentForPackage (com. example. autostart); context. startActivity (newIntent );}}}}

 

4. If you need to start some services and then write (optional)

 

If the program needs to start some necessary services and then write this code, generally, you only need to start the main activity of the app at startup. Here we will demonstrate the write service.

 

Package com. example. autostart; import android. app. service; import android. content. broadcastReceiver; import android. content. context; import android. content. contextWrapper; import android. content. intent; import android. content. sharedPreferences; import android. OS. IBinder; import android. util. log; // start the broadcast automatically when the server is started. Accept public class AutoStartService extends Service {@ Overridepublic IBinder onBind (Intent intent) {// TODO Auto-generated method stubreturn null ;} @ Override public void onCreate () {super. onCreate (); Log. d (TAG2, test service );}}

 

5. How to use these settings to configure MainActivity

 

 

Package com. example. autostart; import android. OS. bundle; import android. app. activity; import android. content. contextWrapper; import android. content. sharedPreferences; import android. content. sharedPreferences. editor; import android. view. menu; import android. view. view; import android. view. view. onClickListener; import android. widget. textView; public class MainActivity extends Activity {private SharedPreferences mPreferences = null; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); mPreferences = getSharedPreferences (AutoStart, ContextWrapper. MODE_PRIVATE); boolean bStart = mPreferences. getBoolean (AddToAuto, false); final TextView textView1 = (TextView) findViewById (R. id. textView1); if (bStart) {textView1.setText (started upon startup);} else {textView1.setText (started upon shutdown);} // enable findViewById (R. id. button1 ). setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {Editor editor = mPreferences. edit (); editor. putBoolean (AddToAuto, true); editor. commit (); textView1.setText (enabled and automatically started) ;}}); // close findViewById (R. id. button2 ). setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {Editor editor = mPreferences. edit (); editor. putBoolean (AddToAuto, false); editor. commit (); textView1.setText (auto start upon shutdown) ;}}@overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. activity_main, menu); return true ;}}

 

6. simple interface implementation Diagram

 

 

 

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.