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