2014-01-14 33 views
1

我正在建設一個android應用程序使用科爾多瓦和html(js/css等..) 我試圖打開短信應用程序,當用戶點擊鏈接。href sms:|| smsto:不使用HTML +科爾多瓦(電話)

這是html代碼:

<a href="sms:052xxxx808">Send Sms</a> 

tel:mailto:計劃工作時,sms:smsto:不工作。

信息:使用

sms:計劃時,我得到這個錯誤:No Activity found to handle Intent

E/Cordova(28360): Error sending sms sms:052xxxx808:android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW typ=vnd.android-dir/mms-sms (has extras) } 

並採用sms:計劃時,我得到這個錯誤:ERR_UNKNOWN_URL_SCHEME

D/Cordova(27207): CordovaWebViewClient.onReceivedError: Error code=-10 Description=net::ERR_UNKNOWN_URL_SCHEME URL=smsto:052xxxx808 

我使用Nexus 5與Kitkat 4.4.2來測試應用程序..

PS:銀河4 sms:方案的工作......(不奇巧)

UPDATE:

點擊在瀏覽器sms:方案確實可行A HREF鏈接,所以也許我錯過了許可或類似的東西?

+0

我可以確認我看到完全相同的內容,並且在發現您的報告時即將發佈相同內容。您是否嘗試過使用Cordova SMS插件而不是href?我開始走上這條路。 – occasl

回答

0

我能夠通過創建一個SMS插件,結合兩個以前的答案herehere得到這個工作。唯一的問題是你必須檢查是否有構建版本是KitKat的做法與過去不同。

這裏是我的插件代碼:

public class SmsPlugin extends CordovaPlugin { 
    public final String ACTION_SEND_SMS = "SendSMS"; 

    @Override 
    public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException { 
     if (action.equals(ACTION_SEND_SMS)) { 
      try { 
       String phoneNumber = args.getString(0); 
       String message = args.getString(1); 
       String method = args.getString(2); 

       if (method.equalsIgnoreCase("INTENT")) { 
        invokeSMSIntent(phoneNumber, message); 
        callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.NO_RESULT)); 
       } else { 
        sendSMS(phoneNumber, message); 
       } 

       callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK)); 
       return true; 
      } catch (JSONException ex) { 
       callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION)); 
      } 
     } 
     return false; 
    } 

    private void invokeSMSIntent(String phoneNumber, String message) { 
     Intent intent; 
     Activity activity = this.cordova.getActivity(); 

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) // Android 4.4 and up 
     { 
      String defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(activity); 

      intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:" + Uri.encode(phoneNumber))); 
      intent.putExtra("sms_body", message); 

      // Can be null in case that there is no default, then the user would be able to choose any app that supports this intent. 
      if (defaultSmsPackageName != null) { 
       intent.setPackage(defaultSmsPackageName); 
      } 
     } else { 
      intent = new Intent(Intent.ACTION_VIEW); 
      intent.setType("vnd.android-dir/mms-sms"); 
      intent.putExtra("address", phoneNumber); 
      intent.putExtra("sms_body", message); 
     } 

     activity.startActivity(intent); 
    } 

    private void sendSMS(String phoneNumber, String message) { 
     SmsManager manager = SmsManager.getDefault(); 
     PendingIntent sentIntent = PendingIntent.getActivity(this.cordova.getActivity(), 0, new Intent(), 0); 
     manager.sendTextMessage(phoneNumber, null, message, sentIntent, null); 
    } 
} 

這裏是我如何調用它從JavaScript:

var sms = function() { 
    message: function (phnum, callback) { 
     if (Ext.os.is.iOS) { 
      cordova.exec(callback, function (err) { 
       callback('The following error occurred: ' + err); 
      }, "Sms", "send", [ {"recipients": [phnum]} ]); 
     } else if (Ext.os.is.Android) { 
      cordova.exec(callback, function (err) { 
       callback('The following error occurred: ' + err); 
      }, "SmsPlugin", "SendSMS", [phnum, "", "INTENT"]); 
     } else { 
      document.location.href = "sms:" + phnum 
     } 
    } 
}; 
module.exports = sms; 

務必把它添加到你config.xml中:

<feature name="SmsPlugin"> 
    <param name="android-package" value="my.plugin.SmsPlugin" /> 
</feature>