Auto-Start Failure of Android program startup

Source: Internet
Author: User

According to the company's requirements, I checked the code implementation of Android boot self-startup, using the blog Park-kongshan unlimited code.

Then I found that the startup failed. After checking for a long time, I couldn't find the cause. The methods I mentioned on the Internet were uniform, and then I found that

Segment code is different from others

<Span style ="COLOR: #ff00ff"><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>  No one else has <span> </span>, and then I modify the previous Code as follows: <receiver android:name=".BootBroadcastReceiver"> 

        <intent-filter> 
        <action android:name="android.intent.action.BOOT_COMPLETED"
/> 
        <category android:name="android.intent.category.HOME"
/>
        </intent-filter> 
    </receiver> By the way, the permission is also removed and started successfully. The complete method of empty mountain is provided:

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: #ff00ff"><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: #ff00ff"><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.

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.