Android connectivity-Session Initiation Protocol (SIP) (2)

Source: Internet
Author: User

Create a list

If you are developing an application using the sip api, remember that this function is only supported in Versions later than Android (API level 9. In addition, not all devices running Versions later than Android (API level 9) provide SIP support.

Add the following permissions to your application list to use the SIP

1. Android. Permission. use_sip

2. Android. Permission. Internet

3. to ensure that your application can only be installed on devices that provide the SIP capability, you must add <uses-SDK Android: minsdkversion = "9"> in your application list. This statement indicates that your application requires a version later than Android. For more information, see the documentation related to the API level and <uses-SDK> element.

4. to control whether your application can be filtered out by devices that do not support SIP (for example, on Google Play), add the following statements to your application list:

<Uses-feature Android: Name = "android. Hardware. Sip. VoIP"/>. This declares the sip api used by your application. This statement should contain an Android: Required data that indicates whether you want your application to be filtered out by devices that do not provide SIP support. Other <uses-feature> declarations may also be required, depending on your implementation. For more information, see the document of the <uses-feature> element.

5. If your application is designed to receive calls, you must also define a receiver (subclass of the broadcastreceiver class) in the Application List ).

<Cycler Android: Name = ". incomingcallcycler" Android: Label = "Call Cycler"/>

Below isSipdemoSummary in the sample list:

<? XML version = "1.0" encoding = "UTF-8"?>

<Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"


Package = "com. example. Android. Sip">

 
...


<Cycler Android: Name = ". incomingcallcycler" Android: Label = "Call Cycler"/>

 
...

 
<Uses-SDK Android: minsdkversion = "9"/>

 
<Uses-Permission Android: Name = "android. Permission. use_sip"/>

 
<Uses-Permission Android: Name = "android. Permission. Internet"/>

 
...

 
<Uses-feature Android: Name = "android. Hardware. Sip. VoIP" Android: required = "true"/>

 
<Uses-feature Android: Name = "android. Hardware. WiFi" Android: required = "true"/>

 
<Uses-feature Android: Name = "android. Hardware. Microphone" Android: required = "true"/>

</Manifest>

Create sipmanager

To use the sip api, your application must create a sipmanager object. In your application, this object is responsible for the following tasks:

1.
Start a sip session;

2.
Start and accept the call;

3.
Register and deregister the SIP service provider;

4.
Verify session connectivity.

Instantiate a new sipmanager object in the following ways:

Public sipmanager msipmanager = NULL;

...

If (msipmanager = NULL ){

Msipmanager = sipmanager. newinstance (this );

}

Register the SIP service

A typical Android sip application involves one or more users. Each user must have a sip account. In an android sip application, each sip account is represented by a sipprofile object.

A sipprofile object defines a sip configuration, including a sip account, domain name, and server information. The configuration associated with the SIP account on the device that runs the application is called local profile ). The configuration used for session connection is called Peer profile ). When your sip application uses a local sipprofile object to log on to the SIP server, this actually uses the SIP address used by the registration device to send the SIP call.

This section describes how to create a sipprofile object, register a SIP server, and register an event with the client.

The following code creates a sipprofile object:

Public sipprofile msipprofile = NULL;

...

Sipprofile. Builder = new sipprofile. Builder (username, domain );

Builder. setpassword (password );

Msipprofile = builder. Build ();

The following code determines that the local configuration will be enabled for dialing and/or receiving common sip calls. The caller can make subsequent calls through the msipmanager. makeaudiocall () method. This Code also sets the Android. sipdemo. incoming_call operation. This operation will be used by the intent filter when the device receives a call. The following are the registration steps:

Intent intent = new intent ();

Intent. setaction ("android. sipdemo. incoming_call ");

Pendingintent = pendingintent. getbroadcast (this, 0, intent, intent. fill_in_data );

Msipmanager. Open (msipprofile, pendingintent, null );

Finally, this Code sets the sipregistrationlistener listener to the sipmanager object. This will track whether the sipprofile object is successfully registered with your sip service provider:

Msipmanager. setregistrationlistener (msipprofile. geturistring (), new sipregistrationlistener (){

Public void onregistering (string localprofileuri ){


Updatestatus ("registering with SIP server ...");

}

Public void onregistrationdone (string localprofileuri, long expirytime ){


Updatestatus ("ready ");

}

Public void onregistrationfailed (string localprofileuri, int errorcode,


String errormessage ){


Updatestatus ("registration failed. Please check settings .");

}

When your application completes the use of a configuration, it should be closed to release the memory associated with these objects, and disconnect the device from the server, such:

Public void closelocalprofile (){


If (msipmanager = NULL ){


Return;


}


Try {


If (msipprofile! = NULL ){


Msipmanager. Close (msipprofile. geturistring ());


}


} Catch (exception ee ){


Log. D ("walkietalkieactivity/ondestroy", "failed to close local profile.", ee );


}

}

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.