One months ago or summer, now it is winter, Xi ' an really is no autumn and spring. OK, nonsense not much to say, today is to say is andriod internal call broadcast and mention Asynctask.
Before we read this blog, let's take a look at my article <<Windows Mobile 5 programming experience 3>>. In that article I mentioned a website that can get cell phone number attribution, weather forecast and so on some webservice. I was in the Windows Mobile Simulator implementation of the effect, speaking of this mobile, I would like to learn Windows Phone development, who would like to pay to buy account, too troublesome, I gave up, it is better to learn Android, write a program casually put up. No, look.
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/4E/08/wKioL1RgHZ2iPhDVAADKg_xynbo131.jpg "title=" 004343951.jpg "alt=" Wkiol1rghz2iphdvaadkg_xynbo131.jpg "/>
is not so, I said at that time, this number who casually input, if there is similar, purely coincidental.
OK, let's take a look at this website WebService website, go inside, we click Domestic Mobile number attribution to query Web services
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/4E/09/wKioL1RgHnuRgIKMAAGjKBXLSlw138.jpg "title=" Qq20141110100802.jpg "alt=" Wkiol1rghnurgikmaagjkbxlslw138.jpg "/>
After entering, we look at the following methods Getmobilecodeinfo
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/4E/09/wKioL1RgHsniPaqyAADn_d7jaE4704.jpg "title=" Qq20141110100925.jpg "alt=" Wkiol1rghsnipaqyaadn_d7jae4704.jpg "/>
OK, we see the WebService request parameter and response return the result.
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/4E/0B/wKiom1RgHv_BYB_dAALSfO4XROA152.jpg "title=" Qq20141110101141.jpg "alt=" Wkiom1rghv_byb_daalsfo4xroa152.jpg "/>
That's OK, knowing all this, we're going to call it, and then we'll see how our Android client calls it.
First of all, our design is when the activity starts, we get the number of the machine. When the user broadcast the phone, the first to get the local number attribution, and then get the broadcast number of the attribution, two number of attribution to the comparison, if the attribution is inconsistent, then add 17951 or 17911.
First of all, we have to take a look at the local number, we now define a public variable in the activity
Public String Nativephonenumber;
In the OnCreate method, we get the number of the machine.
Private String Getnativephonenumber () {Telephonymanager Telephonymanager = (Telephonymanager) this. Getsystemservice ( Context.telephony_service); return Telephonymanager.getline1number ();}
OK, after we get the number, we'll take a look at the handling of the call. We know that Android has a lot of internal broadcasts, such as low battery, phone call, SMS, phone restart and so on. These broadcasts we can all receive, so that we can achieve some functions, such as IP dialing, low battery power automatically adjust the screen brightness, cut off the network and other mobile phone management software similar to some of the 360 features.
Here we receive the code for the outgoing call broadcast as follows
Public class ipdialbroadcastreceiver extends broadcastreceiver {final string IPChinaMobilePrefix = "17951";final string ipchinaunionprefix = "17911"; Final string chinamobile= "mobile"; final string chinaunion= "Unicom"; @Overridepublic void OnReceive (context context, intent intent) {String callNumber = Getresultdata ();//progressdialog pg=punchinalarm.owner.progressdialog;//new mobileadresstask (PG, Callnumber). Execute (callnumber); string nativephonenumber = punchinalarm.owner.nativephonenumber; String nativeaddress= punchinalarm. Getmobileaddress (Nativephonenumber). toString (); String calladdress = punchinalarm. Getmobileaddress (Callnumber). toString (); String newipphonenumber= ""; if (!nativeaddress.equalsignorecase (calladdress)) {if (Nativeaddress.contains ( Chinamobile)) {Newipphonenumber = ipchinamobileprefix.concat (calLnumber);} Else{newipphonenumber = ipchinaunionprefix.concat (Callnumber);} Setresultdata (Newipphonenumber);}}}
The
Here we distinguish between Unicom and mobile. When we receive the broadcast of the call, we first get the local number of the place of attribution and the place of the call to compare, if not consistent, then add IP. Here is mainly to look at the punchinalarm. Getmobileaddress this method.
Public static soapobject getmobileaddress (String mobilenumber) {SoapObject Request = new soapobject (Namespace, method_name); Propertyinfo pi = new propertyinfo ();p i.setname ("Mobilecode");p I.settype (String.class); Pi.setvalue (Mobilenumber), Request.addproperty (PI);p i=new propertyinfo ();p i.setname ("UserID");p I.settype ( String.class);p I.setvalue (""); Request.addproperty (PI); Soapserializationenvelope soapenvelope = new soapserializationenvelope (SOAPENVELOPE.VER11) ;soapenvelope.dotnet = true; Httptransportse httpts = new httptransportse (URL); soapenvelope.bodyout = request ; Soapenvelope.setoutputsoapobject (Request);// set request parameter Try {httpts.call (soap_action, soapenvelope) ;} catch (ioexception e) {// TODO Auto-generated catch Blocke.printstacktrace ();} catch (xmlpullparserexception e) {// todo auto-generated catch blocke.printstacktrace ();} soapobject result = (Soapobject) soapenvelope.bodyin;return result;}
What we need to pay attention to is namespace,method_name,url,soapaction and so on.
Final static string NAMESPACE = "http://WebXml.com.cn/"; final static string method_name = "Getmobilecodeinfo"; final Stati C String soap_action = "Http://WebXml.com.cn/getMobileCodeInfo"; final static String URL = "http:// WEBSERVICE.WEBXML.COM.CN/WEBSERVICES/MOBILECODEWS.ASMX?WSDL ";
If you don't know how to take these variables, look at the following
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/4E/18/wKioL1RgLu2Qr3tvAALX9eau0kw533.jpg "title=" Qq20141110111827.jpg "alt=" Wkiol1rglu2qr3tvaalx9eau0kw533.jpg "/>
namespace know.
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/4E/18/wKioL1RgL0bxcAIzAAFdY62CA5A256.jpg "title=" Qq20141110111954.jpg "alt=" Wkiol1rgl0bxcaizaafdy62ca5a256.jpg "/>
SOAPAction know, URL also know, MethodName also know.
OK, we give WebService passed two parameters, UserID does not pass, the specific parameters how to see the description in WebService.
After we get these two attributions, we reset the dialing numbers.
Setresultdata (Newipphonenumber);
is equivalent to blocking the current non-IP dialing, and then IP dialing. Let's look at the effect in the simulator first. After commissioning, we found that the number is 15555215554, the attribution is Anhui, the operator is unicom (if there is a similar, purely coincidental)
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/4E/1B/wKiom1RgMbKjp_g2AACvU0uzirE115.jpg "title=" Qq20141110113204.jpg "alt=" wkiom1rgmbkjp_g2aacvu0uzire115.jpg "/>650" this.width=650; "src=" http://s3.51cto.com /wyfs02/m00/4e/1a/wkiol1rgmnzht5sxaac7jmi4up4905.jpg "title=" qq20141110113341.jpg "alt=" Wkiol1rgmnzht5sxaac7jmi4up4905.jpg "/>
Dialed number is 13555556666, belongs to Heilongjiang, the operator is mobile (if there is similar, purely coincidental)
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/4E/1A/wKioL1RgMkCzmzlAAACulHljQuI126.jpg "title=" Qq20141110113241.jpg "alt=" wkiol1rgmkczmzlaaaculhljqui126.jpg "/>650" this.width=650; "src=" http://s3.51cto.com /wyfs02/m02/4e/1c/wkiom1rgmnqytg4haacxfmxqbem374.jpg "title=" qq20141110113506.jpg "alt=" Wkiom1rgmnqytg4haacxfmxqbem374.jpg "/>
So two belong to the same place, and this machine is unicom, so add 17911.
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/4E/1B/wKioL1RgM5qA67hRAAJVm3shFzY797.jpg "title=" Qq20141110113737.jpg "alt=" Wkiol1rgm5qa67hraajvm3shfzy797.jpg "/>
OK, let's look at the situation in the real machine. Result error, WiFi connection, why error? Looking for a long reason, the original is my app does not open network permissions.
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/4E/1D/wKioL1RgNbmT1qlAAAI0XHLtwo8112.jpg "title=" QQ picture 20141110114613.jpg "alt=" Wkiol1rgnbmt1qlaaai0xhltwo8112.jpg "/>
This is the personal finance app, let's look at the effect
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/4E/1E/wKiom1RgNdXQUARzAAHV-DF2tLs174.jpg "title=" QQ picture 20141110114242.jpg "alt=" Wkiom1rgndxquarzaahv-df2tls174.jpg "/>
See, Auto-dial 17951. Finally, let's look at the asynchronous implementation.
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/4E/25/wKiom1RgPOjwWvrmAAIL8DCjB3k462.jpg "title=" Qq20141110115632.jpg "alt=" Wkiom1rgpojwwvrmaail8dcjb3k462.jpg "/>
The code below, after we get the call broadcast, we open an asynchronous task to detect the place of attribution
ProgressDialog pg=punchinalarm.owner.progressdialog;new Mobileadresstask (pg,callnumber). Execute (callNumber);
Public class mobileadresstask extends asynctask<string, integer, string> {final String IPPrefix = "17951"; string callnumber; Progressdialog progressdialog;public mobileadresstask (progressdialog progressdialog, String callnumber) {this.progressDialog = progressDialog;this.callNumber = Callnumber;} Protected void onpreexecute () {super.onpreexecute ();p rogressdialog.show ();} Protected string doinbackground (String... params) {publishprogress (5); string s = params[0]; Soapobject soapobjvalue = punchinalarm. Getmobileaddress (Params[0]);p ublishprogress (+);return soapobjvalue == null ? "" : soapobjvalue.tostring ();} Protected void onprogressupdate (integer... values) {super.onprogressupdate (values); Progressdialog.setprogress (Values[0]);} Protected void onposteXecute (String result) {super.onpostexecute (Result);if (!result.contains ("Xian") && !callnumber.startswith (Ipprefix)) {string newnumber = ipprefix.concat (CallNumber); Ntent dialintent = new intent (Intent.action_call, uri.parse ("Tel:" + newnumber)); Punchinalarm.owner.startActivity (dialintent);} Progressdialog.dismiss ();}}
In Doinbackgroud we get the place of attribution and simulate the progress bar. After obtaining the place of attribution, we determine that if it is not the number of Xian and does not add 17951 we will add 17951. A number will be hold at this time.
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/4E/29/wKiom1RgQKXSk2PEAAI_odynZj4914.jpg "title=" Qq20141110123534.jpg "alt=" Wkiom1rgqkxsk2peaai_odynzj4914.jpg "/>
OK, last look at the real machine effect
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/4E/44/wKiom1RgYdbSD37eAAJ6xh4VrcY413.jpg "title=" Wkiom1rgqajbavsraajifu_lq7q423.jpg "alt=" Wkiom1rgydbsd37eaaj6xh4vrcy413.jpg "/>
Finally, do not forget the three configuration, the first two are reading the phone information and processing dial-up permissions, the last is a static registered broadcast receiver. This recipient is the ipdialbroadcastreceiver mentioned above, and the broadcast recipient only receives the broadcast Android.intent.action.NEW_OUTGOING_CALL the action. It is important to note that this receiver is registered in the application node.
<uses-permission android:name= "Android.permission.READ_PHONE_STATE"/><uses-permission android:name= " Android.permission.PROCESS_OUTGOING_CALLS "/><receiver android:name=" Bruce.broadcastor.IpDialBroadCastReceiver "> <intent-filter android:priority=" 1 "> <action Android Oid:name= "Android.intent.action.NEW_OUTGOING_CALL"/> </intent-filter> </receiver>
Dude blog Real, Xiaomi 3 test machine, need source of the students to download the source code download
This article is from the "Technology Creation value" blog, please be sure to keep this source http://leelei.blog.51cto.com/856755/1574894
Android closest Combat (ix)