How to enable Android boot

Source: Internet
Author: User

Background: when Android is started, it sends out a system broadcast with the content action_boot_completed.
The constant of the string is Android. Intent. Action. boot_completed. Just "capture" the message in the program and start it again
You can. Remember, the android framework says: Don't call me, I'll call you back. What we need to do is to prepare for receiving this message, and
The implementation method is to implement a broadcastreceiver.

1. Interface Activity, bootstartdemo. Java File

1234567891011121314151617181920212223242526 public
class
BootStartDemo extends
Activity {     /** Called when the activity is first created. */    @Override    public
void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // No title
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // Full screen
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.main);
        new
Thread() {             public
void run() {                 try
                    /* Close the page after 8 seconds */                    sleep(10000);
                }
catch (Exception e) {
                    e.printStackTrace();
                }
finally {                     finish();
// Close the page                }
            }
        }.start();
      }
}

This code is very simple. When the activity is started, textview is displayed, and the words you want to display are displayed, and the page disappears after 10 seconds.

2. Receive broadcast messages: bootbroadcastreceiver. Java

1234567891011121314 public
class
BootBroadcastReceiver extends
BroadcastReceiver {     static
final String action_boot="android.intent.action.BOOT_COMPLETED"
      @Override    public
void onReceive(Context context, Intent intent) {
        if
(intent.getAction().equals(action_boot)){              Intent ootStartIntent=new
Intent(context,BootStartDemo.class); 
            ootStartIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
            context.startActivity(ootStartIntent); 
        }
      }
  }

This class continues from broadcastreceiver. In the overwriting method onreceive, it detects whether the received intent meets
Boot_completed. If yes, start the activity bootstartdemo.

3. Configuration File

(1) androidmanifest. xml:

1234567891011121314151617181920212223 <?xml
version="1.0"
encoding="utf-8"?>
<! -- This is a self-starting program --><manifest
xmlns:android="http://schemas.android.com/apk/res/android"      package="com.ajie.bootstartdemo"      android:versionCode="1"      android:versionName="1.0">
    <application
android:icon="@drawable/icon"
android:label="@string/app_name">
        <activity
android:name=".BootStartDemo"                  android:label="@string/app_name">
            <intent-filter>
                <action
android:name="android.intent.action.MAIN"
/>                 <category
android:name="android.intent.category.LAUNCHER"
/>             </intent-filter>
        </activity>
    <span
style="color: rgb(255, 0, 255);"><receiver
android:name=".BootBroadcastReceiver"
        <intent-filter
        <action
android:name="android.intent.action.BOOT_COMPLETED"
/>          <category
android:name="android.intent.category.HOME"
/>         </intent-filter
    </receiver
</span>    </application>
<span
style="color: rgb(255, 0, 255);"><strong><uses-permission
android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission></strong
</span></manifest>

Note that the node registers a receiver with the system, and the sub-node intent-filter indicates receiving.
Android. Intent. Action. boot_completed message. Configure the permission for Android. Permission. receive_boot_completed.

(2) Layout file, Main. xml

1234567891011121314151617 <?xml
version="1.0"
encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"          >
<TextView    android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:text="@string/boottext"    android:textColor="#5F2DD2"    android:background="#FFFFFF"    android:textSize="60px"     android:gravity="center_horizontal"    />
</LinearLayout>

Compile the APK package and install it to the simulator or mobile phone. Shut down and reboot. The page showing the activity bootstartdemo is displayed.

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.