First of all, our sample project list is as follows:
1. First we still buy a radio , define a outcallreceiver inherited from the Broadcastreceiver,onreceive () method defines the actions to be performed by the supervisor to hear the broadcast :
public class Outcallreceiver extends broadcastreceiver {
@Override
Public void OnReceive (context context, Intent Intent) {
sharedpreferences sp = context.getsharedpreferences ("config", 0);
//Modify the phone number dialed by the user and precede the number with 17951
String number = Getresultdata ();
System.out.println ("------have outgoing calls out. "+ number");
if (Number.startswith ("0")) {
Setresultdata (sp.getstring ("Ipnumber", "") + number);
}
setresultdata (null);
}
}
2. install the battery and Adjust the channel, configure in the Androidmanifest.xml file:
<receiver android:name= "Com.itheima.ipdailer.OutCallReceiver" >
<intent-filter >
<action android:name= "Android.intent.action.NEW_OUTGOING_CALL"/>
</intent-filter>
</receiver>
Note here to add an external phone dial-in permission :
<uses-permission android:name= "Android.permission. process_outgoing_calls"/>
3. Engineering Code Overview Map:
(1) Activity_main.xml:
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Tools:context=". Mainactivity " > <EditTextAndroid:id= "@+id/et_ipnumber"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:hint= "Please enter the IP number prefix to be set" /> <ButtonAndroid:onclick= "Save"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "Save" /></LinearLayout>
(2) Mainactivity.java:
PackageCom.itheima.ipdailer;Importandroid.app.Activity;Importandroid.content.SharedPreferences;ImportAndroid.content.SharedPreferences.Editor;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.EditText;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {PrivateEditText Et_ipnumber; PrivateSharedpreferences sp; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Et_ipnumber=(EditText) Findviewbyid (R.id.et_ipnumber); SP= This. getsharedpreferences ("config", 0); Et_ipnumber.settext (Sp.getstring ("Ipnumber", "" ")); } Public voidSave (View view) {String Ipnumber=Et_ipnumber.gettext (). toString (). Trim (); Editor Editor=Sp.edit (); Editor.putstring ("Ipnumber", Ipnumber); Editor.commit (); Toast.maketext ( This, "set success", 0). Show (); }}
(3) Outcallreceiver.java:
PackageCom.itheima.ipdailer;ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.Context;Importandroid.content.Intent;Importandroid.content.SharedPreferences; //Buy a radio Public classOutcallreceiverextendsBroadcastreceiver {@Override Public voidOnReceive (Context context, Intent Intent) {sharedpreferences SP= context.getsharedpreferences ("config", 0); //Modify the phone number dialed by the user, and precede the number with 17951String number =Getresultdata (); System.out.println ("------have outgoing calls out. " +Number ); if(Number.startswith ("0") {setresultdata (sp.getstring ("Ipnumber", "") +Number ); } setresultdata (NULL); }}
(4) Androidmanifest.xml:
<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.itheima.ipdailer"Android:versioncode= "1"Android:versionname= "1.0" > <USES-SDKandroid:minsdkversion= "8"android:targetsdkversion= "+" />
//Add an external phone dial-in permission <uses-permissionAndroid:name= "Android.permission.PROCESS_OUTGOING_CALLS"/> <ApplicationAndroid:allowbackup= "true"Android:icon= "@drawable/ic_launcher"Android:label= "@string/app_name"Android:theme= "@style/apptheme" > <ActivityAndroid:name= "Com.itheima.ipdailer.MainActivity"Android:label= "@string/app_name" > <Intent-filter> <ActionAndroid:name= "Android.intent.action.MAIN" /> <categoryAndroid:name= "Android.intent.category.LAUNCHER" /> </Intent-filter> </Activity>
//battery pack, channel adjustment <receiverAndroid:name= "Com.itheima.ipdailer.OutCallReceiver"> <Intent-filter> <ActionAndroid:name= "Android.intent.action.NEW_OUTGOING_CALL"/> </Intent-filter> </receiver>
</ Application > </ Manifest >
Android (Java) Learning Note 175: Outbound calls to broadcast receivers