During this period of time, we were engaged in basic Android development. Now we need to start the system and follow some blog posts on the Internet to find that it has never been successful, when the server is started, it always prompts that the started application is accidentally terminated. Therefore, after referring to the Android SDK doc and finally solving the problem, I will share my experience with you.
Android boot Activity or Service method
Principle]
After the Android system completes the BOOT phase, it will send a broadcast named ACTION_BOOT_COMPLETED. We can capture this broadcast in a BroadcastReceiver and then start our Activity or Service, of course, we must note that our application must have the permission to capture the broadcast. See the specific steps below:
Step 1: First, you must have an Activity or Service for startup. Here we will explain the simplest Activity created by the system.
- package com.billhoo.study;
-
- import android.app.Activity;
- import android.os.Bundle;
-
- public class BootTestActivity extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- }
- }
Step 2: Write A BroadcastReceiver to capture the broadcast ACTION_BOOT_COMPLETED and start the Activity to be started after the capture.
Note: you must addIntent. FLAG_ACTIVITY_NEW_TASKMark, Which is why I always failed to start. As for why, I am still studying the SDK doc. After understanding it, I will make it up.
- Package com. billhoo. study;
-
- Import android. content. BroadcastReceiver;
- Import android. content. Context;
- Import android. content. Intent;
-
- Public class BootCompletedReceiver extends BroadcastReceiver {
- @ Override
- Public void onReceive (Context context, Intent intent ){
- If (intent. getAction (). equals (Intent. ACTION_BOOT_COMPLETED ))
- {
- Intent newIntent = new Intent (context, BootTestActivity. class );
- NewIntent. addFlags (Intent. FLAG_ACTIVITY_NEW_TASK); // note that this flag must be added; otherwise, the startup will fail.
- Context. startActivity (newIntent );
- }
- }
- }
Step 3: register our BroadcastReceiver in the AndroidManifest. xml configuration file.
- <receiver android:name=".BootCompletedReceiver">
- <intent-filter>
- <action android:name="android.intent.action.BOOT_COMPLETED" />
- </intent-filter>
- </receiver>
Step 4: add the permission to allow us to capture the broadcast in the AndroidManifest. xml configuration file.
- <!-- permissions -->
- <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Restart the virtual machine.
The AndroidManifest. xml is attached below for your understanding and reference.
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.billhoo.study" android:versionCode="1"
- android:versionName="1.0">
- <uses-sdk android:minSdkVersion="4" />
-
- <!-- permissions -->
- <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
-
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <!-- activities -->
- <activity android:name=".BootTestActivity" android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
-
- <!-- receivers -->
- <receiver android:name=".BootCompletedReceiver">
- <intent-filter>
- <action android:name="android.intent.action.BOOT_COMPLETED" />
- </intent-filter>
- </receiver>
-
- </application>
- </manifest>
This article is from the "Bill_Hoo" blog, please be sure to keep this source http://billhoo.blog.51cto.com/2337751/761230