Joke Collection Website - Blessing messages - Phonegap, what plug-in can make calls?

Phonegap, what plug-in can make calls?

I recently made an APP with phonegap, and I need to make a phone call in the app. After the call, I can give feedback on the related status of the call. I checked the official document of phonegap, and it seems that there is no api related to mobile phones. I also searched online, but I can't seem to find the relevant content. In fact, html5 supports calling directly on the web page, just add the following html code:

[html] view plain text

make a telephone call

send a text message

But this method can only make phone calls and send text messages, and can't feed back the results. For example, if I want to make a phone call in an application and automatically save the phone number, call time and call duration after the call, then the default html function cannot be satisfied.

Phonegap officially doesn't provide functions, so we have to develop plug-ins ourselves to realize them.

Here is a brief introduction to the development steps of phonegap plug-in through code:

First, define the JS plug-in API.

[javascript] View Plain Text

var Phone = function() {

};

phone . prototype . call = function(success callback,failureCallback,number) {

cordova.exec(successCallback,failureCallback," Phone "," call ",[number]);

};

Windows. Phone = new Phone();

In the above code, we defined a telephone plug-in, and the plug-in has a calling API, which we passed in.

[javascript] View Plain Text

Successful callback

Failed callback

As a callback function for successful and failed telephone dialing.

introduce

[javascript] View Plain Text

figure

As a phone number

Called inside a method.

Cordova.exec () of phonegap

[javascript] View Plain Text

cordova.exec(successCallback,failureCallback," Phone "," call ",[number]);

In this method, a function call to the local side will be executed.

Finally, we create a phone object and attach it to the window object for later call.

In this way, the JS code of the plug-in is completed.

Second, the native code (taking the android platform as an example)

Code directly first.

[java] View Plain Text

Bao: com. juhuibao. Phonegapulugin;

Import java.util.date;

Import org.apache.cordova.api.callbackcontext;

Import org.apache.cordova.api.cordova plugin;

Import org.apache.cordova.api.pluginresult;

Import org.json.jsonarray;

Import org.json.jsonexception;

Import android.app.activity;

Import android.content.intent;

Import Android. Database. Cursor;

Import android.net.uri;

Import android.provider.calllog.calls;

Import android.telephony.phonenumberutils;

CordovaPlugin {

private static final int PHONE _ CALL = 0; //Call

private static final int PHONE _ ABORT = 1; //Hang up the phone

Private date start time;

Private callbackcontext callbackcontext;

Private string phone number;

@ Overlay

Public Boolean execution (string operation, JSONArray parameter, CallbackContext callbackContext) throws JSONException {

Try {

this . callback context = callback context;

If ("call". Equal to (action)) {

this . phone number = args . getstring(0);

this.call(args.getString(0),callback context);

Return true

}

If ("abort". Equal to (action)) {

this . abort(callback context);

Return true

}

Returns false

}

Catch (exception e)

callback context . error(e . getmessage());

}

Returns false

}

//Call

Private void call (string phonenumber, callbackcontext callbackcontext) (

If (phone number! = null & amp& ampphone number . length()& gt; 0) {

if(phonenumberutils . isglobalphonenumber(phone number)){

Intent I = new Intent();

I.setAction (intention. ACTION _ CALL);

I . setdata(uri . parse(" tel:"+phone number));

start _ time = new Date();

this . Cordova . startactivityforresult(this,I,PHONE _ CALL);

}

Otherwise {

CallbackContext。 Error (phone number+"is not a valid phone number. );

}

} Otherwise {

CallbackContext.error ("Phone number cannot be empty." );

}

}

//Interrupt the phone

Private void abort (callback context callback context)

}

public void on activity result(int request code,int resultCode,Intent intent) {

Date end _ time = new Date();

If (result code = = activity. RESULT_OK) {

if (requestCode == PHONE_CALL) {

This.callbackContext.error ("unknown state");

}

}

Else if (result code = = activity. Result _ Cancel) {

Try {

if (requestCode == PHONE_CALL) {

Long duration = getlastcallduration ();

phone result result = new phone result();

result . set number(phone . this . phone number);

result . set start time(phone . this . start _ time);

result . setendtime(end _ time);

Result.setDuration (duration);

This. callbackcontext. sendpluginresult (new plug-in result (plug-in result. Status.OK,result . tojsonobject()));

}

}

Catch (exception e)

this . callback context . error(e . getmessage());

}

}

Otherwise {

This.callbackContext.error ("Other errors!" );

}

}

long delay time = 0;

Long timeout = 2000;

Long GetLastCallDuration () throws an InterruptedException{

activity activity = this . Cordova . get activity();

cursor cursor = activity . getcontentresolver()。 Inquire (call. Content _URI

The new string [] {is called. Phone number. Date, phone number. Duration, call. Type, call. Date},

"Number =? And type=? " ,

The new string []{this.phonenumber, "2"},

Telephone. Default _ Sort _ Order);

Activity.startManagingCursor (cursor);

boolean has record = cursor . movetofirst();

if (hasRecord) {

Long endtime = cursor.getlong (cursor.getcolumnindex (call. Date));

Date date = new date (end time);

Long duration = cursor.getlong (cursor.getcolumnindex (call. Duration));

long dif = this . start _ time . gettime()-date . gettime();

Long seconds = dif/1000;

If (second & lt2 & second & gt-2){

Return duration;

} Otherwise {

if(delay time & gt; = Timeout) {

Returns 0;

}

thread . sleep(200);

delay time+= 200;

Returns getlastcallduration ();

}

} Otherwise {

if(delay time & gt; = Timeout) {

Returns 0;

}

thread . sleep(200);

delay time+= 200;

Returns getlastcallduration ();

}

}

}

Then call the call method, which is a concrete method to realize the dialing function. In the calling method, we first judge the legality of the phone number and directly call callbackContext.error ("Not a valid phone number." ), this method will trigger the execution of the JS-side error callback function and pass in a string parameter, such as the failureCallback above.

If the phone number is legal, the calling interface will pop up with the following code.

[java] View Plain Text

if(phonenumberutils . isglobalphonenumber(phone number)){

Intent I = new Intent();

I.setAction (intention. ACTION _ CALL);

I . setdata(uri . parse(" tel:"+phone number));

start _ time = new Date();

this . Cordova . startactivityforresult(this,I,PHONE _ CALL);

}

Otherwise {

CallbackContext。 Error (phone number+"is not a valid phone number. );

}

In the code, we record the start time through the variable start_time, and then use this.cordova.startactivityforresult (this, I, phone _ call); Start the activity.

When terminating dialing or hanging up the phone, we will execute the onActivityResult method, in which we will realize the feedback of call status information. Mainly look at this code.

[java] View Plain Text

Else if (result code = = activity. Result _ Cancel) {

Try {

if (requestCode == PHONE_CALL) {

Long duration = getlastcallduration ();

phone result result = new phone result();

result . set number(phone . this . phone number);

result . set start time(phone . this . start _ time);

result . setendtime(end _ time);

Result.setDuration (duration);

This. callbackcontext. sendpluginresult (new plug-in result (plug-in result. Status.OK,result . tojsonobject()));

}

}

Catch (exception e)

this . callback context . error(e . getmessage());

}

}

The result code is always equal to activity whether the phone is connected or not. Result _ Cancelled.

We get the call duration through the GetLastCallDuration () method. The principle is to read the duration field in the call record. Because there may not be a call record at the end of the call, we have to call this method repeatedly until there is a record or a timeout period.

We got the duration through this. Callbackcontext. sendpluginresult (new plug-in result (plug-in result. State. Ok, the result. tojsonobject())); Feedback the result to the JS end.

Attached to PhoneResult.java

[java] View Plain Text

Bao: com. juhuibao. Phonegapulugin;

Import java.text.simpledateformat;

Import java.util.date;

Import org.json.jsonexception;

Import org.json.jsonobject;

Public class PhoneResult {

Private string number = "";

Private date start time;

Private date end time; ;

Private long duration = 0;

Public JSONObject toJSONObject () throws JSONException {

simple date format SDF = new simple date format(" yyyy/MM/DD HH:MM:ss:S ");

Returns a new JSONObject (

" { number:"+JSON object . quote(number)+

",start time:"+JSON object . quote(SDF . format(start time))+

",end time:"+JSON object . quote(SDF . format(end time))+

",duration:"+duration+"}";

}

Public string getNumber() {

Quantity returned;

}

Public void setNumber (string number)

This.number = number;

}

Public date getStartTime() {

Return to startTime

}

public void set start time(Date start time){

this . start time = start time;

}

Public date getEndTime() {

Returns the end time;

}

Public void setEndTime {

this . end time = end time;

}

public long getDuration() {

Return duration;

}

Public void setDuration (

This.duration = duration;

}

}

This completes the local code.

Third, the client calls

[javascript] View Plain Text

Windows. phone . call(function(obj){ alert(JSON . stringify(obj));

},function(err){ alert(err); }, " 1340 1 100000");

Fourth, configure plug-ins

Register the plug-in in the config.xml file.

[html] view plain text

& ltplugin name = " Phone " value = " com . juhui Bao . phonegap plugin . Phone "/& gt;

Add permissions to the AndroidManifest.xml file.

[html] view plain text

& ltuses-permission Android:name = " Android . permission . call _ PHONE "/& gt;

& ltuses-permission Android:name = " Android . permission . read _ CALL _ LOG "/& gt;

& ltuses-permission Android:name = " Android . permission . process _ OUTGOING _ CALLS "/& gt;

& ltuses-permission Android:name = " Android . permission . read _ PHONE _ STATE "/& gt;