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:

<script language="javascript" type="text/javascript">
function setText(xx){
document.getElementById('test').value=xx;
}
</script>
<body>
<input id="test" type="text" onclick="javascript:window.xxxxxxxx.getContacts()" />
</body>

Related Article

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.