How to create callback functionality into your Android application

In this example you will learn how to create a Java application targeted to Android device, that makes a call through your Ozeki Phone System PBX.

Download project

After you created your project, you need to allow the Internet accessiblity in the AndroidManifest.xml file with the following line. Note this line must be a direct child of the <manifest> root element.

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

Code example 1 - Allowing Internet connection, and getting phone number

In the Layout resources, locate /res/layout/activity_main.xml, and add a Button to it with an event handler.

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="18dp"
android:layout_marginTop="21dp"
android:text="@string/request_callback"
android:onClick="requestCallback"/>

Code example 2 - Setting onClick event handler

After this, you need to create the handler function in your Activity (ex.: MainActivity.java). This function has to be a public method, that needs only one parameter, the View. The View passed into the method is a reference to the widget that was clicked.

private String getPhoneNumber()
{
	TelephonyManager telephony_manager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
    return telephony_manager.getLine1Number();
}

public void requestCallback(View view)
{
	String ip_address = "ozekipbx.ip";
	short http_api_port = 7780;
	String agent_phone_number = "2000";
	String company_name = "...";
	
	HttpClient httpclient = new DefaultHttpClient(); 
	HttpPost httppost = new HttpPost(String.format("http://%s:%s/", ip_address, http_api_port));

    try
    {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("Command", "Call"));
        nameValuePairs.add(new BasicNameValuePair("CallerId", agent_phone_number));
        nameValuePairs.add(new BasicNameValuePair("CallerDisplayName", company_name));
        nameValuePairs.add(new BasicNameValuePair("Dialed", getPhoneNumber()));
        nameValuePairs.add(new BasicNameValuePair("ResponseOzML",
        		String.format("<Response><Delay>1</Delay><Speak>Welcome this is the %s company. Please wait for a moment until we can connect you to our assistant.</Speak><BlindTransfer>%s</BlindTransfer><Speak>Sorry, no agent is available, please try again later.</Speak></Response>",
        				company_name, agent_phone_number)));

        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        httpclient.execute(httppost);
    }
    catch (ClientProtocolException e)
    {
        System.err.println(e.getMessage());
    }
    catch (IOException e)
    {
    	System.err.println(e.getMessage());
    }
}

Code example 3 - Sending HTTP request to PBX

The getPhoneNumber function is getting the phone number of the android device. The requestCallback function is responsible to make a HTTP request to ozekipbx.ip and call the phone number of the android device. The caller will be the 2000 and the company_name will be set as the caller's displayed name. You can also provide APIExtension as a parameter to the request. In this example the System extension will be used, because no APIExtension parameter has been passed.

In the ResponseOzML parameter, you need to provide a valid OzML file content. These commands will be executed when a call is created.

You could use many OzML commands. For more information about usable commands, please check the OzML reference book.

In this example, this basic OzML script has been used. In order to make a transfer to an employee, you could do a BlindTransfer to the employee's phone number.

<Response>
	<Delay>1</Delay>
	<Speak>Welcome this is the ... company. Please wait for a moment until we can connect you to our assistant.</Speak>
	<BlindTransfer>2000</BlindTransfer>
	<Speak>Sorry, no agent is available, please try again later.</Speak>
</Response>

Code example 4 - Receiving the call of a customer and transfering it to an employee

More information