Android---Get your apk program to run automatically (GO)

Source: Internet
Author: User

Transferred from: http://blog.sina.com.cn/s/blog_72f6e45701014l6t.html

Sometimes apps need to run automatically when they're powered on, such as a back-office service that automatically updates content from the Web. How to realize the application of automatic running on the boot? At the time of writing, Lenovo to Mr. Gao to "Don t call me, I'll call you back!" Summarizing the Android framework, it's really a point. Understanding the meaning of this sentence, many of the Android platform to achieve a certain function of the problem, can be solved.


Usage Scenarios: After the phone is powered on, it automatically runs the program and displays "Hello" on the screen. I started! " Words. Background Knowledge:When Android starts, it emits a system broadcast with the contents of action_boot_completed, whose string constants are represented as Android.intent.action.BOOT_COMPLETED. Just "snap" to this message in the program and start it again. Remember, the Android framework says "Don ' tcall me, I'll call Youback." What we have to do is prepare to receive the message, and the means to achieve it is to achieve a broadcastreceiver.   Code parsing:  1, interface Activity:sayhello.java package Com.ghstudio.bootstartdemo;   import Android.app.activity; import Android.os.bundle; import android.widget.textview;    public class SayHello Extendsactivity {      @Override     public void onCreate (Bundle savedinstancestate) {       super.oncreate (savedinstancestate)         TEXTVIEWTV = new TextView (this);        tv.settext ("Hello. I started! ");        setcontentview (TV);    }  }     This code is simple, when the activity starts, create a textview, and use it to display "Hello." I started! " Words. &NBSP;2, receiving broadcast message: Bootbroadcastreceiver.java package Com.ghstudio.bootstartdemo;   importandroid.content.broadcastreceiver;  import Android.content.context; import Android.content.intent;   public Class Bootbroadcastreceiverextends broadcastreceiver {    static finalString ACTION = " Android.intent.action.BOOT_COMPLETED ";     @Override   public voidonreceive (Context Context, Intent Intent) {      if (Intent.getaction (). Equals (ACTION)) {    Intent sayhellointent=newintent (Context,sayhello.class);    sayhellointent.addflags (Intent.flag_activity_new_task);    Context.startactivity (sayhellointent);  }  } }    the class derives from Broadcastreceiver, The Load method OnReceive, detects whether the received intent is in accordance with boot_completed, and if so, initiates sayhello that activity.  3, configuration file: Androidmanifest.xml <?xml version= "1.0" encoding= "Utf-8"?>  <manifestxmlns: Android= "Http://schemas.android.com/apk/res/android"      package= "Com.ghstudio.BootStartDemo"       android:versioncode= "1"       android:versionname= "1.0" >     <applicationandroid:icon= "@drawable/icon" Android:label= "@string/app_name" >       <activity android:name= ". SayHello "               android:label=" @string/app_name ">           <intent-filter>              <actionandroid:name= "Android.intent.action.MAIN"/>              <categoryandroid:name= "Android.intent.category.LAUNCHER"/>           </intent-filter>       </activity>   < Receiverandroid:name= ". Bootbroadcastreceiver ">  <intent-filter>  <actionandroid:name= "Android.intent.action.BOOT_COMPLETED"/></intent-filter> </receiver> </application> <uses-sdkandroid:minsdkversion= "3"/> < Uses-permissionandroid:name= "Android.permission.RECEIVE_BOOT_COMPLETED" ></uses-permission> </ Manifest> Note that part of the bold word, the node registers a receiver with the system, and the child node Intent-filter represents the receiving Android.intent.action.BOOT_COMPLETED message. Don't forget to configure Android.permission.RECEIVE_BOOT_COMPLETED permissions. code Download Http://pan.baidu.com/s/1hqgQhik

Android---Get your apk program to run automatically (GO)

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.