2016-11-14 64 views
2

我有下面的代碼開始意圖撥打電話號碼。 如果此代碼在沒有手機的設備上運行,它將正常運行,而不會引發令我感到意外的異常情況。 就是這樣,我想知道如何編碼,如果沒有電話,那麼它應該嘗試找到一個VOIP應用程序。 如何做到這一點?使用VOIP應用程序代替電話撥號程序

ImageButton btnPhone = (ImageButton) view.findViewById(R.id.phone_image); 
btnPhone.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + employee.getPhone())); 
     try { 
      startActivity(intent); 
     } catch (android.content.ActivityNotFoundException ex) { 
      Toast.makeText(getActivity(), "Can't make phone call", Toast.LENGTH_SHORT).show(); 
     } catch (Exception ex) { 
      Toast.makeText(getActivity(), "Can't make phone call", Toast.LENGTH_SHORT).show(); 
     } 
    } 
}); 

編輯:收回,將一個單獨的問題。

回答

2

嘗試這種方式:

1),用於蜂窩無線模塊存在測試機器人設備(從here):

static public int getSDKVersion() { 
    Class<?> build_versionClass = null; 
    try { 
     build_versionClass = android.os.Build.VERSION.class; 
    } catch (Exception e) { 
    } 

    int retval = -1; 
    try { 
     retval = (Integer) build_versionClass.getField("SDK_INT").get(build_versionClass); 
    } catch (Exception e) { 
    } 

    if (retval == -1) 
     retval = 3; //default 1.5 

    return retval; 
} 

static public boolean hasTelephony(Context context) 
{ 
    TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 
    if (tm == null) 
     return false; 

    //devices below are phones only 
    if (Utils.getSDKVersion() < 5) 
     return true; 

    PackageManager pm = context.getPackageManager(); 

    if (pm == null) 
     return false; 

    boolean retval = false; 
    try 
    { 
     Class<?> [] parameters = new Class[1]; 
     parameters[0] = String.class; 
     Method method = pm.getClass().getMethod("hasSystemFeature", parameters); 
     Object [] parm = new Object[1]; 
     parm[0] = "android.hardware.telephony"; 
     Object retValue = method.invoke(pm, parm); 
     if (retValue instanceof Boolean) 
      retval = ((Boolean) retValue).booleanValue(); 
     else 
      retval = false; 
    } 
    catch (Exception e) 
    { 
     retval = false; 
    } 

    return retval; 
} 

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

許可和

<uses-feature android:name="android.hardware.telephony" android:required="false" /> 

使用功能。

然後,如果設備沒有帶 「電話模塊」 試圖通過一切可能的手段來調用(從here):

public void call(String dialNumber) { 
    try{ 
    Intent callIntent = new Intent("android.intent.action.CALL_PRIVILEGED"); 
    callIntent.setData(Uri.parse("tel:" + dialNumber)); 
    startActivity(callIntent); 
    } 
    catch (Exception e) { 
     Intent callIntent = new Intent(Intent.ACTION_CALL); 
     callIntent.setData(Uri.parse("tel:" + dialNumber)); 
     startActivity(callIntent); 
    } 
} 

或者

2)設法找到安裝VOIP手動(從here

private boolean appInstalledOrNot(String uri) { 
     PackageManager pm = getPackageManager(); 
     boolean app_installed; 
     try { 
      pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES); 
      app_installed = true; 
     } 
     catch (PackageManager.NameNotFoundException e) { 
      app_installed = false; 
     } 
     return app_installed; 
    } 

3)儘量使呼叫經創立於第2頁應用程序,例如通過Skype(從here

public static void skype(String number, Context ctx) { 
     try { 
      //Intent sky = new Intent("android.intent.action.CALL_PRIVILEGED"); 
      //the above line tries to create an intent for which the skype app doesn't supply public api 

       Intent sky = new Intent("android.intent.action.VIEW"); 
      sky.setData(Uri.parse("skype:" + number)); 
      Log.d("UTILS", "tel:" + number); 
      ctx.startActivity(sky); 
     } catch (ActivityNotFoundException e) { 
      Log.e("SKYPE CALL", "Skype failed", e); 
     } 

    } 
+0

什麼是'Hub'(從'Hub.MainContext.getSystemService()'? –

+0

它只是用於'Context'。請參閱最新的答案。 –

+0

明白了。讓我稍微處理一下。 –

相關問題