1. Why do we need broadcasters?
Broadcasting in life:
Radio: to send a certain channel of radio messages, 50 mhz;
Radio: adjust to a certain channel to receive broadcast messages;
Radio in android applications: A service built into the system that sends events (insufficient power, full power, start-up) as a broadcast message to other recipients;
Radio in android applications: A class of broadcast receivers written by oneself.
2. Broadcast Receiver Case 1-ip Dialer (Emphasis)
-
Steps for developing broadcasting receivers:
(1) Buy a radio:
public class OutCallBroadCastReceiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { }
}
(2) Insert the battery:
<receiver android:name="com.lile.ipcall.OutCallBroadCastReceiver" />
(3) To adjust to one channel:
<intent-filter > <action android:name="android.intent.action.NEW_OUTGOING_CALL" /> </intent-filter>
3. Broadcast Receiver Case 2 - Short Message Monitor (Emphasis)
Pdus: protocol data unit s protocol data unit
Characteristic:
(1) Even if the broadcast receiver is not running, when the broadcast message arrives, the system will automatically start the process of the broadcast receiver and call onReceive method to receive the message.
(2) After version 4.0, for the sake of security, the application must have an interface and must be run once by the user before the broadcasting receiver takes effect.
(3) The forcible stop of version 4.0 is equivalent to freezing an application. Once the application is forcibly stopped by the user, the broadcasting receiver will not take effect. Until the user manually opens the application.
(4) There was no such security design before version 4.0.
Steps:
(1) Buy a radio
(2) Plug in the battery
(3) Adjust to a channel
Configuration file:
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<receiver android:name="com.lile.smslistener.SMSBroadCastReceiver">
<intent-filter >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
Code:
package com.itheima.smslistener;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.gsm.SmsMessage;
public class SMSBroadCastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Object[] objs = (Object[]) intent.getExtras().get("pdus");
for(Object obj : objs){
SmsMessage sms = SmsMessage.createFromPdu((byte[]) obj);
String content = sms.getMessageBody();
String srcPhone = sms.getOriginatingAddress();
System.out.println("content========"+content);
System.out.println("srcPhone========"+srcPhone);
}
}
}
4. Broadcast Receiver Case 3_sd Card Status Monitoring (Emphasis)
The 2.3 simulator was used in the test, and the version after 4.0 did not have the functions of uninstalling, mounting and removing SD cards.
Steps:
(1) Buy a radio
(2) Plug in the battery
(3) Adjust to a channel
Configuration file:
<receiver android:name="com.lile.sdlistener.SDBroadCastReceiver">
<intent-filter >
<action android:name="android.intent.action.MEDIA_MOUNTED" />
<action android:name="android.intent.action.MEDIA_UNMOUNTED" />
<action android:name="android.intent.action.MEDIA_REMOVED" />
<!-- Must be added data This property -->
<data android:scheme="file"/>
</intent-filter>
</receiver>
Code:
package com.itheima.sdlistener;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class SDBroadCastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if("android.intent.action.MEDIA_MOUNTED".equals(action)){
Toast.makeText(context, "It's already plugged in. SD card.................", 0).show();
}
if("android.intent.action.MEDIA_UNMOUNTED".equals(action)){
Toast.makeText(context, "Pull out SD card.................", 0).show();
}
if("android.intent.action.MEDIA_REMOVED".equals(action)){
Toast.makeText(context, " Removal SD card.................", 0).show();
}
}
}
5. Broadcast Receiver Case Start Up (Emphasis)
Steps:
(1) Buy a radio
(2) Plug in the battery
(3) Adjust to a channel
What to do: Let the software turn on and off, disable the return key and minimization key (small house key);
Configuration file:
<receiver android:name="com.lile.lesuo.BootCompletedBroadCastReceiver">
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Code:
package com.itheima.lesuo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class BootCompletedBroadCastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Open main activity
Intent i = new Intent(context, MainActivity.class);
// Tell activity to maintain the task stack itself, and if the task stack does not have the current task, it will create a new task and put it in the task stack.
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
System.out.println("***********88888888888 Start-up Completion*********************************");
}
}
6. Broadcast Receiver Case Unloading and Installation (Emphasis)
Configuration file:
<receiver android:name="com.lile.azxz.AZXZBroadCastReceiver">
<intent-filter >
<action android:name="android.intent.action.PACKAGE_INSTALL" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<action android:name="android.intent.action.PACKAGE_ADDED" />
<!-- This attribute must be added -->
<data android:scheme="package"/>
</intent-filter>
</receiver>
Code:
package com.itheima.azxz;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class AZXZBroadCastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if("android.intent.action.PACKAGE_INSTALL".equals(action)){
Toast.makeText(context, "Installed a new software...................", 0).show();
}
if("android.intent.action.PACKAGE_REMOVED".equals(action)){
Toast.makeText(context, "Unloading a software...................", 0).show();
}
if("android.intent.action.PACKAGE_REPLACED".equals(action)){
Toast.makeText(context, "Re-install a software...................", 0).show();
}
}
}
7. Send custom broadcasts
Steps to create a radio station:
// Create an intent object to deliver a message
Intent intent = new Intent();
// Set the event type to broadcast
intent.setAction("com.lile.broadcast.HMSSDT");
// Setting up broadcast message data
intent.putExtra("news", "Ale Radio, which starts at 12:30 p.m..........");
// Send a broadcast message
sendBroadcast(intent);
8. Orderly and disorderly broadcasting (emphasis)
-
Disordered broadcasting:
Broadcast receivers can receive broadcasts as long as they register to receive the corresponding event types.
// Send a broadcast message (out-of-order broadcast)
sendBroadcast(intent);
-
Orderly broadcasting:
When the broadcast sends out the message, the message will be sent underground from high to low according to the priority of the receiver.
Messages can be intercepted or modified.
Send orderly broadcasting:
Intent intent = new Intent();
intent.setAction("com.lile.orderedbroadcast.ZYFFNTBT");
// Send an orderly broadcast
// intent intention
// permission specifies that the recipient needs to add permissions
// Result Receiver specifies which broadcaster receives the last message
// scheduler message processor
// Initial Code specifies the initial code for the message
// Initial Data specifies the data for the message
// initialExtras specifies additional parameters
sendOrderedBroadcast(intent, null, null, null, 1, "The State Council began to issue farmland subsidies in 2017:900 element", null);
Broadcast Receiver Profile:
<receiver android:name="com.lile.zf.ProvinceBroadCastReceiver">
<intent-filter android:priority="1000" >
<action android:name="com.lile.orderedbroadcast.ZYFFNTBT"/>
</intent-filter>
</receiver>
Broadcast Receiver Code:
String info = getResultData();
System.out.println("I am the provincial people's government,The central message has been received.:"+info);
// Toast.makeText(context, "I am the provincial people's government,The central message has been received.:"+info, 0).show();
setResultData("The State Council began to issue farmland subsidies in 2014:400 element");
9. Intermediate animation
/**
* Animation of Transparency Change
* @param view
*/
public void alpha(View view) {
AlphaAnimation aa = new AlphaAnimation(0, 1.0f);
// Time of animation playing
aa.setDuration(2000);
// Repetition times
aa.setRepeatCount(2);
//Setting duplicate patterns
aa.setRepeatMode(Animation.REVERSE);
iv.startAnimation(aa);
}
/**
* Rotating animation
* @param view
*/
public void rotate(View view) {
RotateAnimation ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
// Time of animation playing
ra.setDuration(2000);
// Repetition times
ra.setRepeatCount(2);
ra.setRepeatMode(Animation.REVERSE);
iv.startAnimation(ra);
}
/**
* Animation of displacement change
* @param view
*/
public void trans(View view) {
TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, -0.5f, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, -0.5f, Animation.RELATIVE_TO_PARENT, 0.5f);
ta.setDuration(2000);
// Repetition times
ta.setRepeatCount(2);
ta.setRepeatMode(Animation.REVERSE);
iv.startAnimation(ta);
}
/**
* Zoom in and out animations
* @param view
*/
public void scale(View view) {
ScaleAnimation sa = new ScaleAnimation(0.1f, 2.0f, 0.1f, 2.0f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f);
sa.setDuration(2000);
// Repetition times
sa.setRepeatCount(2);
sa.setRepeatMode(Animation.REVERSE);
sa.setFillAfter(true); // Set the effect of filling after
iv.startAnimation(sa);
}
/**
* Animation Set
* @param view
*/
public void set(View view){
// Animation Insertor
AnimationSet set = new AnimationSet(false);
RotateAnimation ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
// Time of animation playing
ra.setDuration(2000);
// Repetition times
ra.setRepeatCount(2);
ra.setRepeatMode(Animation.REVERSE);
TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, -0.3f, Animation.RELATIVE_TO_PARENT, 0.3f, Animation.RELATIVE_TO_PARENT, -0.3f, Animation.RELATIVE_TO_PARENT, 0.3f);
ta.setDuration(2000);
// Repetition times
ta.setRepeatCount(2);
ta.setRepeatMode(Animation.REVERSE);
ScaleAnimation sa = new ScaleAnimation(0.1f, 2.0f, 0.1f, 2.0f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f);
sa.setDuration(2000);
// Repetition times
sa.setRepeatCount(2);
sa.setRepeatMode(Animation.REVERSE);
set.addAnimation(ra);
set.addAnimation(sa);
set.addAnimation(ta);
iv.startAnimation(set);
}