Android --- WebView (the system contact number is called in the text box on the webpage)

Source: Internet
Author: User

Today, I discussed with my colleagues about the WebView text control to obtain system contacts. I thought it was okay to just click the text box to bring up the system contact Activity. The idea immediately emerged is to implement the jump function in the java method, and then call the java method through the Js event. Only later will you know that you have to obtain the contact number and assign it to the text box after selecting a contact. At that time, I simply thought it would not be possible, but my colleague reminded me that if I passed the number as a Js event parameter on the client page and assigned a value to the text box. This is indeed feasible, but where is the java call client Js method entry (it must be called when the Activty containing WebView is reproduced after the system contact is closed ), some people may think of a method: onActivityResult (); yes, this method will be called back after the system contact Activity is disabled. The premise is that startActivityForResult () is used to call the system contact ();
The idea is to paste some specific code below:
Java code: (1) Call the Java method when you click the text box to bring up the system contact (how Js calls the Java method in the following Js Code)
 
// AddJavascriptInterface () method for interacting with a webpage
Browser. addJavascriptInterface (new Object (){
@ SuppressWarnings ("unused ")
Public void getContacts (){
Intent i1 = new Intent (Intent. ACTION_PICK,
Android. provider. Contacts. People. CONTENT_URI );
StartActivityForResult (i1, CODE_PHONEBOOK );

}
}, "Xxxxxxxx ");
 

(2) call the client Js [setText (var value)] method after obtaining the contact to assign a value to the text box.
 
@ Override
Protected void onActivityResult (int requestCode, int resultCode, Intent data ){
// TODO Auto-generated method stub
Cursor c = null;
Super. onActivityResult (requestCode, resultCode, data );
If (requestCode = CODE_PHONEBOOK) {// phone book
Try {
If (data! = Null ){
C = getContentResolver (). query (data. getData (), null, null,
Null, null );
}
If (c! = Null ){
C. moveToFirst ();
For (int I = 0; I <c. getColumnCount (); I ++ ){
String name = c. getColumnName (I );
String phoneNum = c. getString (I );
If (name. inclusignorecase ("number ")){
CurrentWebView. loadUrl ("javascript: setText ('"
+ PhoneNum + "')");
Break;
}
}
C. close ();
}
} Catch (Exception e ){
E. getMessage ();
}
}
}
 
Client Js Code:
 
<Html>
<Head>
<Script language = "javascript" type = "text/javascript">
Function setText (xx ){
Document. getElementById ('test'). value = xx;
}
</Script>
</Head>
<Body>
<Input id = "test" type = "text" onclick = "javascript: window. xxxxxxxx. getContacts ()"/>
</Body>
</Html>
 

 


From the wangjinyu501 Column

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.