Broadcast (master)
- The concept of broadcasting
- Reality: Radio stations send messages, buy a radio, and listen to
- Android: The system sends a broadcast when an event is generated, and the application uses the broadcast receiver to receive the broadcast, knowing what happened to the system. During the operation of the Android system, there are many events, such as power-on, battery change, SMS, call, screen unlock
Broadcast Recipient (master)
- When a broadcast is sent, the system iterates through all the manifest files and finds the broadcast receiver that can receive the broadcast by matching the intent filter.
IP dialer (Master)
Principle: Receive radio calls, modify phone numbers carried in the broadcast * Define broadcast receivers to receive call broadcasts
public class CallReceiver extends BroadcastReceiver { //当广播接收者接收到广播时,此方法会调用 @Override public void onReceive(Context context, Intent intent) { //拿到用户拨打的号码 String number = getResultData(); //修改广播内的号码 setResultData("17951" + number); }}
Define the broadcast type received by the broadcast recipient in the manifest file
<receiver android:name="com.itheima.ipdialer.CallReceiver"> <intent-filter > <action android:name="android.intent.action.NEW_OUTGOING_CALL"/> </intent-filter></receiver>
Permission to receive calls to broadcast
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
- Even if the broadcast recipient's process does not start, the system will automatically start the process where the receiver is sent when the broadcast can be received by the receiver.
SMS blocker (familiar)
When the system receives a text message, it generates a broadcast that contains the number and content of the SMS.
Define broadcast recipients to receive SMS broadcasts
public void onReceive(Context context, Intent intent) {//拿到广播里携带的短信内容Bundle bundle = intent.getExtras();Object[] objects = (Object[]) bundle.get("pdus");for(Object ob : objects ){ //通过object对象创建一个短信对象 SmsMessage sms = SmsMessage.createFromPdu((byte[])ob); System.out.println(sms.getMessageBody()); System.out.println(sms.getOriginatingAddress());}
}
- When the system creates a broadcast, it stores the text message in an array, then pdus the data to the bundle, and then deposits the bundle into the intent
The manifest file configures the broadcast type to be received by the broadcast receiver, and note that to set the priority attribute, the priority level is higher than the SMS application to enable interception
<receiver android:name="com.itheima.smslistener.SmsReceiver"> <intent-filter android:priority="1000"> <action android:name="android.provider.Telephony.SMS_RECEIVED"/> </intent-filter></receiver>
Add permissions
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
After 4.0, the broadcast recipient's app must be started once before it takes effect
- After 4.0, if the broadcast recipient's app is manually closed by the user, it will never start again until the user starts the app manually again
Monitor SD card status (master)
The manifest file defines the type received by the broadcast receiver, listening to the three common status of the SD card, so the broadcast recipient needs to receive three broadcasts
<receiver android:name="com.itheima.sdcradlistener.SDCardReceiver"> <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"/> <data android:scheme="file"/> </intent-filter></receiver>
Definition of broadcast receiver
public class SDCardReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // 区分接收到的是哪个广播 String action = intent.getAction(); if(action.equals("android.intent.action.MEDIA_MOUNTED")){ System.out.println("sd卡就绪"); } else if(action.equals("android.intent.action.MEDIA_UNMOUNTED")){ System.out.println("sd卡被移除"); } else if(action.equals("android.intent.action.MEDIA_REMOVED")){ System.out.println("sd卡被拔出"); } }}
Ransomware (Master)
- Receive a start-up broadcast, initiating ransomware activity in the broadcast recipient
Configure the receive on-boot broadcast in the manifest file
<receiver android:name="com.itheima.lesuo.BootReceiver"> <intent-filter > <action android:name="android.intent.action.BOOT_COMPLETED"/> </intent-filter></receiver>
Permissions
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
Define broadcast receivers
@Overridepublic void onReceive(Context context, Intent intent) { //开机的时候就启动勒索软件 Intent it = new Intent(context, MainActivity.class); context.startActivity(it);}
- The above code does not start mainactivity because the broadcast receiver does not create a task stack, so no task stack can start the activity
Manually set flag to create a new task stack
it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Monitor the installation, uninstallation, and update of the app (familiar)
Principle: When an application installs an uninstall update, the system sends a broadcast, and the package name * manifest file that carries the app in the broadcast defines the type that the broadcast receiver receives, because it listens to three actions of the app, so it needs to receive three broadcasts
<receiver android:name= "Com.itheima.app.AppReceiver" > <intent-filter > <ac tion android:name= "Android.intent.action.PACKAGE_ADDED"/> <action android:name= "Android.intent.action.PAC kage_replaced "/> <action android:name=" Android.intent.action.PACKAGE_REMOVED "/> <data an Droid:scheme= "Package"/> </intent-filter> </receiver>
Definition of broadcast receiver
public void onReceive(Context context, Intent intent) { //区分接收到的是哪种广播 String action = intent.getAction(); //获取广播中包含的应用包名 Uri uri = intent.getData(); if(action.equals("android.intent.action.PACKAGE_ADDED")){ System.out.println(uri + "被安装了"); } else if(action.equals("android.intent.action.PACKAGE_REPLACED")){ System.out.println(uri + "被更新了"); } else if(action.equals("android.intent.action.PACKAGE_REMOVED")){ System.out.println(uri + "被卸载了"); }}
Two types of broadcasts (master)
- Out-of-order broadcasts: All broadcast receivers that match the broadcast's intent can receive the broadcast and are not sequenced (received at the same time)
- Ordered broadcasts: All broadcast receivers that match the broadcast's intent can receive the broadcast, but the order of reception is determined according to the priority of the broadcast recipient
- Definition of priority: -1000~1000
- Result recipient: All broadcast receivers receive the broadcast after it is received, and must receive
- Abortbroadcast: Prevents other receivers from receiving this broadcast, like interception, only ordered broadcasts can be intercepted
Service (Master)
- Is the component that runs silently in the background, can be understood as the activity without foreground, suitable for running code that does not need the foreground interface
- The service can be manually shut down and not restarted, but if it is shut down automatically, the memory will restart
- StartService the life cycle of the start service
- Oncreate-onstartcommand-ondestroy
- Repeated calls to StartService cause Onstartcommand to be called repeatedly
Process priority (Master)
- Foreground process: a process that has an activity that is interacting with the user (the Onresume method is called)
- Visible process: a process that has a non-foreground, but activity that is visible to the user (the OnPause method is called)
- Service process: A process that has a service initiated through the StartService method
- Background process: a process that has a background activity (the OnStop method is called)
- Empty process: No processes that have any active application components, that is, no service and activity is running
Telephone Listener (familiar)
- Phone Status: Idle, ringing, answering
Get phone manager, set listening
TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);tm.listen(new MyPhoneStateListener(), PhoneStateListener.LISTEN_CALL_STATE);
Implementation of the Listening object
Class Myphonestatelistener extends phonestatelistener{//when the phone status changes, this method calls @Override public void oncallstatechanged ( int state, String incomingnumber) {//TODO auto-generated method stubs super.oncallstatechanged (state, incom Ingnumber); Switch (state) {Case telephonymanager.call_state_idle://idle if (recorder! = null) {Recorde R.stop (); Recorder.release (); } break; Case telephonymanager.call_state_offhook://Off-hook if (recorder! = null) {Recorder.start (); } break; Case telephonymanager.call_state_ringing://Bell recorder = new Mediarecorder (); Set the sound source Recorder.setaudiosource (MediaRecorder.AudioSource.MIC); Set the audio file format Recorder.setoutputformat (MediaRecorder.OutputFormat.THREE_GPP); Recorder.setoutputfile ("sdcard/haha.3gp"); Set Audio file Encoding Recorder.setaudioencodER (MediaRecorder.AudioEncoder.AMR_NB); try {recorder.prepare (); } catch (IllegalStateException e) {//TODO auto-generated catch block E.printstacktrace (); } catch (IOException e) {//TODO auto-generated catch block E.printstacktrace (); } break; } }}
Android Basics Summary (vii)