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