Broadcast Receiver has two broadcasts: disordered broadcasting and ordered broadcasting. Let me start with disordered broadcasting and system broadcasting.
Disordered broadcasting: The information to be sent and the information to be filtered (such as Action, Category) are loaded into an Intent object, and then the Intent object is broadcasted by calling Context.sendBroadcast().
Broadcast Receiver: The receiver implements it by inheriting a class of BroadcastRecevier, overriding the onReceive() method and registering BroadcastReceiver in Java or XML.
The src/com/zking/shuanger_android_20/Activity.java code is as follows:
package com.zking.shuanger_android_20;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private EditText et_main_content;
private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_main_content = (EditText) findViewById(R.id.et_main_content);
intent = new Intent();
//Setting the name of the broadcast (setting action behavior) is unique
intent.setAction("com.zking.shuanger_android_20.SenderContent");
}
/*
* intent It is the bridge of three basic components of Android
* Activity ,Broadcast(Broadcasting, service (service)
*
* */
public void send(View view){
String content = et_main_content.getText().toString();
//Carry data
intent.putExtra("content",content);
//Transmit broadcast
// sendBroadcast(intent);
}
}
Receiver of broadcast events (a class that inherits Broadcast Receiver)
src/com/zking/shuanger_android_20_received_01/MyReceivedOne .java
package com.zking.shuanger_android_20_received_01;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Created by Pretty Girl on 2017/2/7.
*/
public class MyReceivedOne extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if ("com.zking.shuanger_android_20.SenderContent".equals(intent.getAction())){
String content = intent.getStringExtra("content");
SimpleDateFormat format = new SimpleDateFormat("yyyy year MM month dd day HH:mm:ss");
Date date = new Date(System.currentTimeMillis());//Get the current time
String datetime = format.format(date);
Log.i("test","No.1 Receiver No. 2 receives the broadcast!"+content+"\t"+datetime);
}
}
}
Registered Broadcasting:
Register Broadcast Receiver (Static Broadcasting) through XML. AndroidManifest.xml:
<! - Registered Broadcast Receiver 1. Configuring in XML (Static Broadcasting) 2. Using java code (dynamic broadcasting) --> <receiver android:name=".MyReceivedOne"> <! - Intention fi lt er - > <intent-filter> <! - Configure the broadcasting receiver name - > <action android:name="com.zking.shuanger_android_20.SenderContent"></action> </intent-filter> </receiver>
The reception of dynamic broadcasting is the same as that of static broadcasting, so let's talk about it.
Register Broadcast Receiver through Java Code
src/com/zking/shuanger_android_20_received_02/MainActivity .java
package com.zking.shuanger_android_20_received_02;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
private MyReceivedTwo myReceivedTwo;
private IntentFilter intentFilter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myReceivedTwo = new MyReceivedTwo();
intentFilter = new IntentFilter();
intentFilter.addAction("com.zking.shuanger_android_20.SenderContent");
}
@Override
protected void onResume() {
super.onResume();
//Registered broadcasting
registerReceiver(myReceivedTwo,intentFilter);
}
@Override
protected void onDestroy() {
super.onDestroy();
//Destruction of broadcasting
unregisterReceiver(myReceivedTwo);
}
}
Finish talking about custom radio!
Then let's talk about an example of system broadcasting, monitoring the phone status.
First, write a class that inherits Broadcast Receiver for testing
src/com/zking/shuanger_android_20_phonestate/MyPhoneState.java
package com.zking.shuanger_android_20_phonestate;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.util.Log;
/**
* Created by Pretty Girl on 2017/2/7.
*/
public class MyPhoneState extends BroadcastReceiver {
private TelephonyManager telephonyManager;
@Override
public void onReceive(Context context, Intent intent) {
if ("android.intent.action.PHONE_STATE".equals(intent.getAction())){
//Get the phone number
String phone = intent.getStringExtra("incoming_number");
Log.i("test","You have a phone in!\t"+phone);
//Get the phone status
//Telephone Manager
telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
int state = telephonyManager.getCallState();
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
Log.i("test","I'm on the phone.");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.i("test","In the call");
break;
case TelephonyManager.CALL_STATE_IDLE:
Log.i("test","Hang up.");
break;
}
}
}
}
Then in AndroidManifest.xml, configure the mobile phone to read the phone status and register the broadcast.
See the code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zking.shuanger_android_20_phonestate">
<!-- Read phone status permissions -->
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher"
android:label="@string/app_name" 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>
<!-- Registered broadcasting -->
<receiver android:name=".MyPhoneState">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"></action>
</intent-filter>
</receiver>
</application>
</manifest>
That's all right. You should also go to the test soon.
This sharing will end first, remember to pay attention to me, oh, there are more to share with you, thank you!