Broadcasting
- 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
IP Dialer
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.ibky.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
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.ibky
-
Add permissions
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
-
4.0 after the broadcast receiver installation must be started manually, otherwise it does not take effect
- 4.0 after the broadcast receiver is manually shut down, it will no longer boot.
Monitor SD card Status
-
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.ibky.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 software
- 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.ibky.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 apps
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.ibky.app.AppReceiver"> <intent-filter > <action android:name="android.intent.action.PACKAGE_ADDED"/> <action android:name="android.intent.action.PACKAGE_REPLACED"/> <action android:name="android.intent.action.PACKAGE_REMOVED"/> <data android: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
- 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
- Final recipient: After all broadcast receivers have received the broadcast, it is received and is bound to receive
- Abortbroadcast: Prevents other receivers from receiving this broadcast, like interception, only ordered broadcasts can be intercepted
Service
- 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
- Foreground process: has foreground activity (Onresume method is called)
- Visible process: Has visible activity (OnPause method is called)
- Service process: It is not necessary to be recycled, and will be restarted even if it is recycled.
- Background process: Has background activity (the activity's OnStop method is called), it is easy to be recycled
- Empty process: No activity is running and can easily be recycled
Phone Theft ting device
- 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; } }}
Accompanying notes Android four components
- Activity
- Broadcastreceiver
- Service
- ContentProvider
Broadcasting
- Reality
- Radio broadcast, post messages
- Buy a radio and you'll get the message.
- Android
- During the operation of the system, there are many events: power on, call, send and receive SMS, charge change, screen unlock
- Once this event occurs, the system sends a broadcast, in order to receive this broadcast, know what happened to the system, you need to broadcast receiver, function like radio
IP Dialer
Function: Automatically add the number prefix of the IP line when making a call
Principle: When the system calls, will send a call broadcast, IP dialer as long as the definition of the broadcast receiver to receive this broadcast, then you can know when the user calls, call the broadcast will also contain the user dialed number, IP dialer can get this number from the broadcast, and make changes, that is, add a number prefix
SMS Blocker
Function: Similar to SMS Firewall system in the receipt of text messages, will produce a text message broadcast, SMS broadcast, contains the text message of the sender number and text message content, SMS application can receive text messages, in fact, received a text message broadcast, that our SMS blocker, as long as the SMS application to the broadcast before the SMS broadcast interception, Then the SMS app will not receive the broadcast
Monitor SD card Status
When the SD card status changes, the system will send a broadcast
Application installation, update, uninstall monitoring
Principle, when the system installs the update uninstall, the system will produce the broadcast, the broadcast with the application package name
Two kinds of broadcast disordered broadcasting
- All broadcast receivers that match the Intent-filter to an out-of-order broadcast can receive this broadcast without sequencing (which can be understood as simultaneous)
Orderly broadcast
- All broadcast receivers that match Intent-filter to an ordered broadcast can receive this broadcast, but in order of precedence, as defined in Intent-filter ( -1000~1000)
Service
- Components that run silently in the background can be understood as having no foreground activity, and the service is used to run code that needs to run silently in the background
Process priority
- Foreground process: a process that has an activity (activity's Onresume method call) that can interact with the user
- Visible process: a process that has a visible but unfocused activity (the activity's OnPause method is called)
- Service process: A process that has a service initiated through the StartService method
- Background process: a process that has an invisible activity (the activity's OnStop method is called)
- Empty process:! Processes that have any active components (activity, service)
Phone theft. Listener
The user receives the call, once the answer, starts the recording immediately, the telephone hangs, the recording finishes, produces the audio file
Broadcast and service of Android Application Development Foundation (i)