Broadcastreceiver is one of the four main components of Android, and this example demonstrates static and dynamic registration in the form of code. 1, static registration static registration only need to be configured in Androidmanifest.xml: Androidmanifest.xml:
<?xml version= "1.0" encoding= "Utf-8"?>
<manifest xmlns:android= "Http://schemas.android.com/apk/res/android"
package= "Thonlon.example.cn.ipdaildemo" >
<uses-permission android:name= "Android.permission.PROCESS_OUTGOING_CALLS"/>
<application
android:allowbackup= "true"
android:icon= "@mipmap/ic_launcher"
android:label= "@string/app_name"
android:roundicon= "@mipmap/ic_launcher_round"
android:supportsrtl= "true"
android:theme= "@style/apptheme" >
<activity android:name= ". Mainactivity ">
<intent-filter>
<action android:name= "Android.intent.action.MAIN"/>
<category android:name= "Android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<receiver android:name= ". Mybroadcastreceiver ">
<intent-filter>
<action android:name= "Android.intent.action.NEW_OUTGOING_CALL"/>
</intent-filter>
</receiver>
</application>
</manifest>
Here is the relevant code: Mybroadcastreceiver.java:
Package Thonlon.example.cn.ipdaildemo;
import Android.content.BroadcastReceiver;
import Android.content.Context;
import android.content.Intent;
/**
* Created by Niuxinlong on 2018/6/21.
*/
Public class Mybroadcastreceiver extends broadcastreceiver{
@Override
Public void onreceive(context context, Intent Intent) {
String number = getresultdata ();
setresultdata(number+ "123456");
}
}
Mainactivity.java:
Package Thonlon.example.cn.ipdaildemo;
import Android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import Android.os.Bundle;
Public class Mainactivity extends Appcompatactivity {
private Mybroadcastreceiver mybroadcastreceiver;
@Override
protected void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_main);
mybroadcastreceiver mybroadcastreceiver = new Mybroadcastreceiver (); //Call a broadcast in the main activity to make the broadcast effective
}
2. Dynamic registration needs to be set in Java code as follows: Mainactivity.java:
Package Thonlon.example.cn.ipdaildemo;
import Android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import Android.os.Bundle;
Public class Mainactivity extends Appcompatactivity {
private Mybroadcastreceiver mybroadcastreceiver;
@Override
protected void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
intentfilter filter = new Intentfilter ();
filter.addaction("Android.intent.action.NEW_OUTGOING_CALL");
mybroadcastreceiver = new Mybroadcastreceiver ();
Registerreceiver (mybroadcastreceiver, filter);
}
@Override
protected void OnDestroy () {
Super.ondestroy ();
unregisterreceiver (mybroadcastreceiver);// destroy broadcast
}
}
In addition, dynamic mutual side although do not need to configure <receiver> in Androidmanifest.xml , but do not forget to add the broadcast type permission in the configuration file:
<uses-permission android:name= "Android.permission.PROCESS_OUTGOING_CALLS"/>
Broadcastreceiver (broadcast) static registration and dynamic registration--android development