Android to start-up (a)--to enable the reception of the broadcast

Source: Internet
Author: User

API19 is the 4.4 version, using the simulator AVD.

Here's how it works:

1. Install the APK and start it manually once.

2. Start the AVD from the window option and do not start the run as application.

The code is very simple, also posted to the novice to see.

In three steps:

1.new a class, overriding the OnReceive method

2. Register receiver in the manifest.

3. Grant permission to receive system broadcasts (3.1 must be granted permission to receive system broadcasts at the beginning)

The code is as follows:

1  PackageCom.example.testreceiver;2 3 ImportAndroid.content.BroadcastReceiver;4 ImportAndroid.content.Context;5 Importandroid.content.Intent;6 ImportAndroid.util.Log;7 8  Public classMyreceiverextendsbroadcastreceiver{9String tag = "Tag";Ten @Override One      Public voidOnReceive (Context context, Intent Intent) { A         //TODO auto-generated Method Stub -LOG.W (Tag, "You have received system MSG"); -LOG.I (Tag, "You have received 123system msg"); the          -     } -  -}
View Code

The configuration in manifest is as follows

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Manifestxmlns:android= "Http://schemas.android.com/apk/res/android"3  Package= "Com.example.testreceiver"4 Android:versioncode= "1"5 Android:versionname= "1.0" >6 7     <USES-SDK8         android:minsdkversion= "+"9 android:targetsdkversion= "+" />Ten  One     <uses-permissionAndroid:name= "Android.permission.RECEIVE_BOOT_COMPLETED" /> A  -     <Application -         Android:allowbackup= "true" the Android:icon= "@drawable/ic_launcher" - Android:label= "@string/app_name" - Android:theme= "@style/apptheme" > -         <Activity +             Android:name=". Mainactivity " - Android:label= "@string/app_name" > +             <Intent-filter> A                 <ActionAndroid:name= "Android.intent.action.MAIN" /> at  -                 <categoryAndroid:name= "Android.intent.category.LAUNCHER" /> -             </Intent-filter> -         </Activity> -  -         <receiverAndroid:name= "Com.example.testreceiver.MyReceiver" > in             <Intent-filter> -                 <ActionAndroid:name= "Android.intent.action.BOOT_COMPLETED" > to                 </Action> +             </Intent-filter> -         </receiver> the     </Application> *  $ </Manifest>
View Code

You only need to register a receiver in your own file and write a permission statement.

<receiver android:name= "Com.example.testreceiver.MyReceiver" >
<intent-filter>
<action android:name= "Android.intent.action.BOOT_COMPLETED" >
</action>
</intent-filter>
</receiver>

<uses-permission android:name="Android.permission.RECEIVE_BOOT_COMPLETED"></ Uses-permission>

Android to start-up (a)--to enable the reception of the broadcast

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.