2012-07-11 38 views
1

我必须从手机获取calllog并将其作为email发送。我的问题是我不知道如何从脚本格式的calllog插件获取值作为email发送。任何人都可以帮助我解决这个问题??我发布了我的代码也..如何从java脚本中的phonegap插件获取值?

Calllistplugin.java

public class CallListPlugin extends Plugin { 

     /** List Action */ 
     private static final String ACTION = "list"; 
     private static final String CONTACT_ACTION = "contact"; 
     private static final String SHOW_ACTION = "show"; 
     private static final String TAG = "CallListPlugin"; 

     /* 
     * (non-Javadoc) 
     * 
     * @see com.phonegap.api.Plugin#execute(java.lang.String, 
     * org.json.JSONArray, java.lang.String) 
     */ 
     @Override 
     public PluginResult execute(String action, JSONArray data, String callbackId) { 
      Log.d(TAG, "Plugin Called"); 
      PluginResult result = null; 
      if (ACTION.equals(action)) { 
       try { 
        int limit = -1; 
        System.out.println("DATA"+data); 

        //obtain date to limit by 
        if (!data.isNull(0)) { 
         String d = data.getString(0); 
         Log.d(TAG, "Time period is: " + d); 
         if (d.equals("week")) 
          limit = -7; 
         else if (d.equals("month")) 
          limit = -30; 
         else if (d.equals("all")) 
          limit = -1000000; // LOL 
        } 

        //turn this into a date 
        Calendar calendar = Calendar.getInstance(); 
        calendar.setTime(new Date()); 
        calendar.add(Calendar.DAY_OF_YEAR, limit); 
        Date limitDate = calendar.getTime(); 
        String limiter = String.valueOf(limitDate.getTime()); 

        //now do required search 
        JSONObject callInfo = getCallListing(limiter); 
        Log.d(TAG, "Returning " + callInfo.toString()); 
        result = new PluginResult(Status.OK, callInfo); 

       } catch (JSONException jsonEx) { 
        Log.d(TAG, "Got JSON Exception " + jsonEx.getMessage()); 
        result = new PluginResult(Status.JSON_EXCEPTION); 
       } 
      } else if (SHOW_ACTION.equals(action)) { 
       try { 
        if (!data.isNull(0)) { 
         viewContact(data.getString(0)); 
        } 
       } catch (JSONException jsonEx) { 
        Log.d(TAG, "Got JSON Exception " + jsonEx.getMessage()); 
        result = new PluginResult(Status.JSON_EXCEPTION); 
       } catch (Exception e) {} 
      } else if (CONTACT_ACTION.equals(action)) { 
       try { 
        String contactInfo = getContactNameFromNumber(data.getString(0)); 
        Log.d(TAG, "Returning " + contactInfo.toString()); 
        result = new PluginResult(Status.OK, contactInfo); 
       } catch (JSONException jsonEx) { 
        Log.d(TAG, "Got JSON Exception " + jsonEx.getMessage()); 
        result = new PluginResult(Status.JSON_EXCEPTION); 
       } 
      } else { 
       result = new PluginResult(Status.INVALID_ACTION); 
       Log.d(TAG, "Invalid action : " + action + " passed"); 
      } 
      return result; 
     } 

     /** 
     * Gets the Directory listing for file, in JSON format 
     * 
     * @param file 
     *   The file for which we want to do directory listing 
     * @return JSONObject representation of directory list. e.g 
     *   {"filename":"/sdcard" 
     *   ,"isdir":true,"children":[{"filename":"a.txt" 
     *   ,"isdir":false},{...}]} 
     * @throws JSONException 
     */ 
     private JSONObject getCallListing(String period) throws JSONException { 

      JSONObject callLog = new JSONObject(); 

      String[] strFields = { 
        android.provider.CallLog.Calls.DATE, 
        android.provider.CallLog.Calls.NUMBER, 
        android.provider.CallLog.Calls.TYPE, 
        android.provider.CallLog.Calls.DURATION, 
        android.provider.CallLog.Calls.NEW, 
        android.provider.CallLog.Calls.CACHED_NAME, 
        android.provider.CallLog.Calls.CACHED_NUMBER_TYPE, 
        android.provider.CallLog.Calls.CACHED_NUMBER_LABEL 
        }; 

      try { 
       Cursor callLogCursor = ctx.getContentResolver().query(
         android.provider.CallLog.Calls.CONTENT_URI, 
         strFields, 
         CallLog.Calls.DATE + ">?", 
         new String[] {period}, 
         android.provider.CallLog.Calls.DEFAULT_SORT_ORDER); 

       int callCount = callLogCursor.getCount(); 

       if (callCount > 0) { 
        JSONObject callLogItem = new JSONObject(); 
        JSONArray callLogItems = new JSONArray(); 

        callLogCursor.moveToFirst(); 
        do { 
         callLogItem.put("date", callLogCursor.getLong(0)); 
         callLogItem.put("number", callLogCursor.getString(1)); 
         callLogItem.put("type", callLogCursor.getInt(2)); 
         callLogItem.put("duration", callLogCursor.getLong(3)); 
         callLogItem.put("new", callLogCursor.getInt(4)); 
         callLogItem.put("cachedName", callLogCursor.getString(5)); 
         callLogItem.put("cachedNumberType", callLogCursor.getInt(6)); 
         //callLogItem.put("name", getContactNameFromNumber(callLogCursor.getString(1))); //grab name too 
         callLogItems.put(callLogItem); 
         callLogItem = new JSONObject(); 
        } while (callLogCursor.moveToNext()); 
        callLog.put("rows", callLogItems); 
        System.out.println("CALLLOG"+callLogItems); 
        System.out.println("CALLLOGDETAILS"+callLog); 

       } 

       callLogCursor.close(); 
      } catch (Exception e) { 
       Log.d("CallLog_Plugin", 
         " ERROR : SQL to get cursor: ERROR " + e.getMessage()); 
      } 

      return callLog; 
     } 




     /** 
     * Show contact data based on id 
     * @param number 
     */ 
     private void viewContact(String number) { 
      Intent i = new Intent(ContactsContract.Intents.SHOW_OR_CREATE_CONTACT, 
        Uri.parse(String.format("tel: %s", number))); 
      this.ctx.startActivity(i); 
     } 


     /** 
     * Util method to grab name based on number 
     * 
     */ 
     private String getContactNameFromNumber(String number) { 
      // define the columns I want the query to return 
      String[] projection = new String[] { Contacts.Phones.DISPLAY_NAME, Contacts.Phones.NUMBER }; 

      // encode the phone number and build the filter URI 
      Uri contactUri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL, Uri.encode(number)); 

      // query time 
      Cursor c = ctx.getContentResolver().query(contactUri, projection, null, null, null); 

      // if the query returns 1 or more results 
      // return the first result 
      if (c.moveToFirst()) { 
       String name = c.getString(c.getColumnIndex(Contacts.Phones.DISPLAY_NAME)); 
       c.deactivate(); 
       return name; 
      } 

      // return the original number if no match was found 
      return number; 
     } 
    } 

backup.html

<html> 
<head> 

<center>Phone Log</center> 

<style type="text/css"> 
body { 
    background-image: url(images/background.png); 
} 
</style> 

<script type="text/javascript" charset="utf-8" src="cordova-1.8.0.js"></script> 
<script type="text/javascript" charset="utf-8" src="Calllog.js"></script> 
<script type="text/javascript" charset="utf-8" src="webintent.js"></script> 

</head> 


<script type="text/javascript" charset="utf-8"> 
    function UnCheckAll() { 
     var checkbox = document.getElementById(1); 
     document.getElementById(1).checked = true; 
     document.getElementById(2).checked = false; 

     if (checkbox.checked) { 

      document.addEventListener("deviceready", onDeviceReady, false); 

     } 
    } 

    function onDeviceReady() { 

    window.plugins.CallListPlugin.list('CallListPlugin', successCallback, 
       failureCallback); 



     var extras = {}; 
     extras[WebIntent.EXTRA_SUBJECT] = "subject"; 
     extras[WebIntent.EXTRA_TEXT] = "body"; 

     window.plugins.WebIntent.startActivity({ 
      url : 'mailto', 
      action : WebIntent.ACTION_SEND, 
      type : 'text/plain', 
      extras : extras 
     }, success, fail); 

    } 

    function successCallback(e) { 

     console.log("Success"); 
    } 

    function failureCallback(f) { 
     console.log("Failure"); 
    } 

    function success(e) { 

     console.log("SuccessEMAIL"); 
    } 
    function fail(f) { 
     console.log("FailureEMAIL"); 
    } 
</script> 

<body background="../images/background.png"> 
    <center> 
     <table> 


      <tr> 
       <td>&nbsp;</td> 
      </tr> 
      <tr> 
       <td>&nbsp;</td> 
      </tr> 
      <tr> 
       <td>&nbsp;</td> 
      </tr> 
      <tr> 
       <td>&nbsp;</td> 
      </tr> 
      <tr> 
       <td>&nbsp;</td> 
      </tr> 

      <tr> 
       <td>Call Log:</td> 
       <td><input type="checkbox" id="1" name="check_list"></td> 
      </tr> 
      <tr> 
       <td>Sms Log:</td> 
       <td><input type="checkbox" id="2" name="check_list"></td> 
      </tr> 
      <tr> 
       <td>&nbsp;</td> 
      </tr> 

      <tr> 

       <td><font face="verdana,arial" size=-1><input 
         type="image" src="images/backup.png" name="Un_CheckAll" 
         value="UncheckAll" onClick="UnCheckAll()"></td> 

      </tr> 



     </table> 
    </center> 
    </form> 
    </td> 
    </tr> 
    </table> 
    </td> 
    </tr> 
    </table> 

</body> 
</html> 

个calllog.js

var CallListPlugin = function() { 
    }; 

    CallListPlugin.prototype.list = function(params, successCallback, failureCallback) { 
     return cordova.exec(successCallback, failureCallback, 'CallListPlugin', 'list', 
       [ params ]); 
    }; 

    CallListPlugin.prototype.contact = function(params, successCallback, failureCallback) { 
     return cordova.exec(successCallback, failureCallback, 'CallListPlugin', 'contact', 
       [ params ]); 
    }; 

    CallListPlugin.prototype.show = function(params, successCallback, failureCallback) { 
     return cordova.exec(successCallback, failureCallback, 'CallListPlugin', 'show', 
       [ params ]); 
    }; 

    /*cordova.addConstructor(function() { 
     cordova.addPlugin('CallLog', new CallLog()); 
     PluginManager.addService("CallListPlugin", "com.gsr.imei.CallListPlugin"); 
    });*/ 

    cordova.addConstructor(function() { 
     cordova.addPlugin("CallListPlugin", new CallListPlugin()); 
    }); 

我得从calllistplugin.java文件backup.html文件脚本format.I值不KNW如何实现it.Can任何人给我这个问题的一些想法? ? 感谢您的帮助......

回答

0

这里是你应该怎么做

function onDeviceReady() { 

    window.plugins.CallListPlugin.list('all', successCallback, 
       failureCallback); 
} 

function successCallback(e) { 
console.log("Success"); 

var newArray = new Array(); 
var durationArray = new Array(); 
var numberArray = new Array(); 
var typeArray = new Array(); 
var dateArray = new Array(); 
var cachedNumberTypeArray = new Array(); 

for (i = 0; i < e.rows.length; i++) { 
     newArray.push(e.rows[i].new); 
     durationArray.push(e.rows[i].duration); 
     numberArray.push(e.rows[i].number); 
     typeArray.push(e.rows[i].type); 
     dateArray.push(e.rows[i].date); 
     cachedNumberTypeArray.push(e.rows[i].cachedNumberType); 
    } 
} 
+0

感谢ü先生,感谢乌拉圭回合help..I我得到的错误是07-11 13:38:54.273:我/ Web控制台(722):成功回调中的错误:CallListPlugin2 = TypeError:无法读取文件中未定义的属性“长度”:///android_asset/www/cordova-1.8.0.js:254 – user751828 2012-07-11 08:04:56

+0

哦,是...我忘了...只要将e.Rows.length更改为e.rows.length – 2012-07-11 08:11:28

+0

我得到同样的错误..sir .. – user751828 2012-07-11 08:18:09

相关问题