WELCOME Abdennour : Software engineer

May 24, 2012

Call Contact From Html Page in WebView !!!

Download Source Code









1.Structure :

2.Statique Page Web  : /assets/demo.html


In this page, write the contacts you want to call, and thier phone numbers.
Follow this format :

<tr><td><!-- اسم المتصل به --></td><td><a href="javascript:demo.startPhone(<!-- Phone Number-->)"><!--PhoneNumber--></a></td></tr>


Example :

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
function show(content){
document.getElementById("countent").innerHTML=
"Js Call "+content;
}
</script>
</head>
<body>
<table align="center">
<tr><td>Name</td><td>Phone</td></tr>
<tr><td>Abdennour</td><td><a href="javascript:demo.startPhone(123)">123</a></td></tr>
<tr><td>Abdesslem Brother</td><td><a href="javascript:demo.startPhone(456)">456</a></td></tr>
</table>
<p id="countent">الحمد لله الذي أعاننا</p>
</body>
</html>

3.Layout : WebView In LinearLayout:
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<WebView
android:id="@+id/webView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Call javascript"
/>
</LinearLayout>

4.Main Activity : CallFromWebViewActivity.java
package slm.abdennour.android.webkit;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
/**
 
 
 @author عبد النور التومي Abdennour
 @since 2012 /9:03:48 PM
 */
public class CallFromWebViewActivity extends Activity {
  private WebView webView;
  private Button button;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    webView = (WebViewthis.findViewById(R.id.webView);
    button = (Buttonthis.findViewById(R.id.button);

    WebSettings setting = webView.getSettings();
    //  تفعيل الجافاسكريبت
    setting.setJavaScriptEnabled(true);
    // ربط الجافاسكريبت بالجافا
    webView.addJavascriptInterface(new Object() {
      // نداء إلى الوظيفة التي تتصل بالمعني بالأمر
      public void startPhone(String num) {
        Intent intent = new Intent();

        intent.setAction(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tel:" + num));
        startActivity(intent);
      }
    }"demo");
    // جاري تحميل صفحة الواب
    webView.loadUrl("file:///android_asset/demo.html");

    button.setOnClickListener(new OnClickListener() {

      public void onClick(View v) {
        
        webView.loadUrl("javascript:show('activityData')");
      }
    });
  }
}
5.Required Permissions & Configuration : 
Manifest File :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="slm.abdennour.android.webkit"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".CallFromWebViewActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>







No comments:

Post a Comment