Broadcastreceiver (broadcast) static registration and dynamic registration--android development

Source: Internet
Author: User

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

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.